Question

I need to download and uncompress a file in RubyMotion. I tried looking for examples but could not find any of this process.

I have a variable (@file) that is all of the data from the request. I need to write that data to a file and then uncompress it, persist the uncompressed data and delete the tmp compressed file.

Here is what I have so far:

   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

Any help or examples would be great!

Was it helpful?

Solution

So I have resolved this for unzipping and untaring.

TO Unzip:

#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

Keep in mind you have to include the SSZipArchive lib. I used the obj-c lib instead of a cocoapod. To do this add the following lines in your Rakefile (assumes that you put the obj-c files in vendor/SSZipArchive folder):

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

To 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)

In this case you will need the Light Untar lib (https://github.com/mhausherr/Light-Untar-for-iOS/). To include this lib in add the following to your Rakefile (assumes the files are in vendor/unTar):

app.vendor_project('vendor', :static, :headers_dir=>"unTar")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top