RubyMotion:iOSでzipファイルをダウンロードして抽出します
-
12-12-2019 - |
質問
ルベルモーションでファイルをダウンロードして解凍する必要があります。例を探してみましたが、このプロセスのいずれかを見つけることができませんでした。
私は要求からのすべてのデータである変数(@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
.
SSZiparchive Libを含める必要があります。Cocoapodの代わりにobj-c libを使用しました。これを行うには、RakeFileに次の行を追加します(Vendor / SSZiparchiveフォルダにOBJ-Cファイルを入れると仮定します):
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)
.
この場合は、Light Untar Lib(https://github.com/mhausherr/light-untar-forios/)が必要です。このlibを含めるには、RakeFileに次のものを追加します(ファイルがベンダー/ untarにあると仮定します)。
app.vendor_project('vendor', :static, :headers_dir=>"unTar")
. 所属していません StackOverflow