2012-10-02 14 views
5

Ho bisogno di scaricare e decomprimere un file in RubyMotion. Ho provato a cercare degli esempi ma non ho trovato nessuno di questi processi.Rubymotion: scarica ed estrai file zip in iOS

Ho una variabile (@file) che è tutti i dati della richiesta. Ho bisogno di scrivere quei dati su un file e poi decomprimerlo, mantenere i dati non compressi ed eliminare il file compresso di tmp.

Ecco quello che ho finora:

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 

Qualsiasi aiuto o esempi sarebbe grande!

risposta

3

Quindi ho risolto questo per decomprimere e untaring.

per decomprimere:

#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 

Tenete a mente che è necessario includere il lib SSZipArchive. Ho usato il obj-c lib invece di un cocoapod. Per fare questo aggiungere le seguenti linee nel vostro Rakefile (presuppone che si mette i file obj-c in vendor/cartella SSZipArchive):

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

Per 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 questo caso sarà necessario il Light Untar lib (https://github.com/mhausherr/Light-Untar-for-iOS/). Per includere questo lib nel aggiungere quanto segue al vostro Rakefile (assume i file sono in vendor/untar):

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

ha aiutato a risolvere un problema correlato, grazie per pubblicare la tua soluzione! – sbauch

Problemi correlati