如何在 iOS 上解压缩 .zip 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16105072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to unzip a .zip file on iOS?
提问by openfrog
After StoreKit downloads the IAP content package it returns an NSURL to me which looks like this:
StoreKit 下载 IAP 内容包后,它会向我返回一个 NSURL,如下所示:
file://localhost/private/var/mobile/Applications/45EF2B3A-3CAB-5A44-4B4A-631A122A4299/Library/Caches/BA32BC55-55DD-3AA4-B4AC-C2A456622229.zip/
file://localhost/private/var/mobile/Applications/45EF2B3A-3CAB-5A44-4B4A-631A122A4299/Library/Caches/BA32BC55-55DD-3AA4-B4AC-C2A456622229.zip/
Despite all sources I found claiming that StoreKit unzips the content package once downloaded, it hands me over a ZIP. This ZIP probably contains the file structure of the content package. But how do I unzip this?
尽管我发现所有消息来源都声称 StoreKit 会在下载后解压缩内容包,但它还是将一个 ZIP 交给了我。此 ZIP 可能包含内容包的文件结构。但是我如何解压缩这个?
回答by Nishant Tyagi
Use SSZipArchive
You can unzip using this
您可以使用此解压缩
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:@"/ImagesFolder"];
NSString *zipPath = Your zip file path;
[SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath delegate:self];
Hope it helps you.
希望对你有帮助。
回答by AdamM
There is a great 3rd party tool for zipping/unzipping files for iPhone
有一个很棒的 3rd 方工具可以为 iPhone 压缩/解压缩文件
https://github.com/soffes/ssziparchive
https://github.com/soffes/ssziparchive
Very simple to use. Hope that helps!!
使用起来非常简单。希望有帮助!!
Edit:
编辑:
Quick method I created which takes url, downloads the zip and unzips it
我创建的快速方法需要 url,下载 zip 并解压缩它
-(void)downloadAndUnzip : (NSString *)sURL_p : (NSString *)sFolderName_p
{
dispatch_queue_t q = dispatch_get_global_queue(0, 0);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(q, ^{
//Path info
NSURL *url = [NSURL URLWithString:sURL_p];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *fileName = [[url path] lastPathComponent];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
[data writeToFile:filePath atomically:YES];
dispatch_async(main, ^
{
//Write To
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:sFolderName_p];
[SSZipArchive unzipFileAtPath:filePath toDestination:dataPath];
});
});
}