rubymotion : iOS에서 zip 파일을 다운로드하고 추출하십시오
-
12-12-2019 - |
문제
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
.
도움이나 예제는 훌륭합니다!
해결책
그래서 나는이를 압축 해제하고 압축 해제로 해결했다.
압축을 풉니 다 :
#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)
.
이 경우 lib (https://github.com/mhausherr/light-tunar-for-ios/)가 켜져 있으면됩니다.이 lib를 rakefile에 추가하십시오 (파일이 공급 업체 / untar에있는 파일이 있음) :
app.vendor_project('vendor', :static, :headers_dir=>"unTar")
. 제휴하지 않습니다 StackOverflow