我需要在rubymotion中下载并解压缩文件。我尝试查找示例,但找不到此过程中的任何一个。

我有一个变量(@file),它是来自请求的所有数据。我需要将数据写入文件,然后解压缩,持续未压缩的数据并删除TMP压缩文件。

这是我到目前为止的内容:

   class LoadResourcesViewController < UIViewController


  def viewDidAppear(animated)
    @loading_bar = retrieve_subview_with_tag(self, 1)
    req=NSURLRequest.requestWithURL(NSURL.URLWithString("#{someurl}"))
    @connection = NSURLConnection.alloc.initWithRequest req, delegate: self, startImmediately: true
  end

  def connection(connection, didFailWithError:error)
    p error
  end

  def connection(connection, didReceiveResponse:response)
    @file = NSMutableData.data
    @response = response
    @download_size = response.expectedContentLength
  end

  def connection(connection, didReceiveData:data)
    @file.appendData data
    @loading_bar.setProgress(@file.length.to_f/@download_size.to_f)
  end

  def connectionDidFinishLoading(connection)       
   #create tmp file
   #uncompress .tar, .tar.gz or .zip
   #presist uncompresssed files and delete original tmp file 

    puts @file.inspect
    @connection.release
    solutionStoryboard = UIStoryboard.storyboardWithName("Master", bundle:nil)
    myVC = solutionStoryboard.instantiateViewControllerWithIdentifier("Main3")
    self.presentModalViewController(myVC, animated:true)
  end

end
.

任何帮助或例子都是很棒的!

有帮助吗?

解决方案

所以我已经解决了这个解除爆发和突出。

解压缩:

#UNZIP given you have a var data that contains the zipped up data.
tmpFilePath = "#{NSTemporaryDirectory()}temp.zip" #Get a temp dir and suggest the filename temp.zip
@fileManager = NSFileManager.defaultManager() #Get a filemanager instance
@fileManager.createFileAtPath(tmpFilePath, contents: data, attributes:nil) #Create the file in a temp directory with the data from the "data" var.

destinationPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, true ).objectAtIndex(0) //get the target home for the unzipped files. This MUST be within your domain in order to persist.

SSZipArchive.unzipFileAtPath(tmpFilePath, toDestination: destinationPath) #Use the SSZipArchive to unzip the file.
@fileManager.removeItemAtPath(tmpFilePath, error: nil) #Cleanup the tmp dir/files
. 请记住,您必须包含SSZiparive Lib。我使用了obj-c lib而不是cocoapod。要执行此操作,请在RakeFile中添加以下行(假设将OBJ-C文件放在供应商/ SSZiparive文件夹中):

app.libs += ['/usr/lib/libz.dylib'] 
app.vendor_project('vendor/SSZipArchive', :static)
.

到untar:

dir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, true) #get target dir for untar'd files
error_ptr = Pointer.new(:object)
NSFileManager.defaultManager.createFilesAndDirectoriesAtPath(dir[0], withTarData: data, error: error_ptr) #Create and untar te data (assumes you have collected some tar'd data in the data var)
.

在这种情况下,您需要光Untar lib(https://github.com/mhausherr/light-tuntar-for-ios/)。要将此页面添加到Rakefile中的添加以下内容(假设文件在供应商/ Untar中):

app.vendor_project('vendor', :static, :headers_dir=>"unTar")
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top