【问题标题】:Rubymotion: Download and Extract Zip File in iOSRubymotion:在 iOS 中下载和提取 Zip 文件
【发布时间】:2012-10-02 20:36:30
【问题描述】:

我需要在 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

任何帮助或示例都会很棒!

【问题讨论】:

    标签: ios file rubymotion


    【解决方案1】:

    所以我已经解决了这个问题,用于解压和解压。

    解压:

    #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
    

    请记住,您必须包含 SSZipArchive 库。我使用了 obj-c lib 而不是 cocoapod。为此,在您的 Rakefile 中添加以下行(假设您将 obj-c 文件放在 vendor/SSZipArchive 文件夹中):

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

    解压:

    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)
    

    在这种情况下,您将需要 Light Untar 库 (https://github.com/mhausherr/Light-Untar-for-iOS/)。要包含此库,请将以下内容添加到您的 Rakefile(假设文件位于 vendor/unTar 中):

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

    【讨论】:

    • 帮我解决了一个相关问题,感谢发布您的解决方案!
    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2021-10-01
    • 2015-07-21
    • 2015-08-28
    相关资源
    最近更新 更多