2012-10-02 29 views
5

Necesito descargar y descomprimir un archivo en RubyMotion. Intenté buscar ejemplos pero no pude encontrar ninguno de este proceso.Rubymotion: Descargar y extraer archivo Zip en iOS

Tengo una variable (@file) que es todos los datos de la solicitud. Necesito escribir esos datos en un archivo y luego descomprimirlo, persistir los datos sin comprimir y eliminar el archivo comprimido tmp.

Esto es lo que tengo hasta ahora:

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 

Cualquier ayuda o ejemplos serían grandes!

Respuesta

3

Así que he resuelto esto para descomprimir y untar.

para descomprimir:

#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 

Tenga en cuenta que tiene que incluir la lib SSZipArchive. Usé el obj-c lib en lugar de un cocoapod. Para ello añadir las siguientes líneas en su Rakefile (se presupone que poner los archivos obj-c en el proveedor carpeta/SSZipArchive):

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

a Descomprimir:

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) 

En este caso, tendrá Light Untar lib (https://github.com/mhausherr/Light-Untar-for-iOS/). Para incluir este lib en añadir lo siguiente a su Rakefile (asume que los archivos están en el vendedor/untar):

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

me ayudó a resolver un problema relacionado, gracias por publicar su solución! – sbauch

Cuestiones relacionadas