在 iOS 中解压下载的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11333399/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:08:02  来源:igfitidea点击:

Unzipping downloaded files in iOS

objective-cioszipunzip

提问by Huy Tran

Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files. I tried to unzip the file with SSZipArchiveand ZipArchive, which are both based on minizip.

使用 ASIHTTPRequest,我下载了一个包含多个音频文件的文件夹的 zip 文件。我尝试使用SSZipArchive和解压缩文件ZipArchive,它们都基于minizip.

When I compile the code, I get this error: Undefined symbols for architecture i386: "_OBJC_CLASS_$_ZipArchive", referenced from: objc-class-ref in AppDelegate.o.

当我编译代码时,我收到此错误:Undefined symbols for architecture i386: "_OBJC_CLASS_$_ZipArchive", referenced from: objc-class-ref in AppDelegate.o.

How do I unzip this file in iOS?

如何在 iOS 中解压缩此文件?

回答by Srikar Appalaraju

I've used ZipArchivewith success in the past. It's pretty ligthweight and simple to use, supports password protection, multiple files inside a ZIP, as well as compress & decompress.

我过去曾成功地使用过ZipArchive。它非常轻巧且易于使用,支持密码保护、ZIP 中的多个文件以及压缩和解压缩。

The basic usage is:

基本用法是:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"];
ZipArchive *zipArchive = [[ZipArchive alloc] init];
[zipArchive UnzipOpenFile:filepath Password:@"xxxxxx"];
[zipArchive UnzipFileTo:{pathToDirectory} overWrite:YES];
[zipArchive UnzipCloseFile];
[zipArchive release];

more examples about this package here

关于这个包的更多例子在这里

I have also tried SSZipArchivein some projects. Below line would unzip your zip file.

我也在一些项目中尝试过SSZipArchive。下面的行将解压缩您的 zip 文件。

[SSZipArchive unzipFileAtPath:path toDestination:destination];

回答by Henry F

To start, in iOS 7, Apple introduced the ability to natively zip / unzip files. You also have the option so send the zip files through Mail, Messages, Airdrop and "Open in". The key is: The zip file has to be supported by iOSSome are, and some are not. The first step: Find out if your file is supported. Do this by a simple check of your newly saved file. Try to open it. If it is stuck of "Waiting..." it is probablynot supported. Try a different library or use a workaround if this is the case.

首先,在 iOS 7 中,Apple 引入了本地压缩/解压缩文件的功能。您还可以选择通过邮件、消息、空投和“打开方式”发送 zip 文件。关键是:iOS 必须支持 zip 文件有些支持,有些不支持。第一步:确定您的文件是否受支持。通过简单检查新保存的文件来完成此操作。尝试打开它。如果卡在“等待中...”,则可能不支持。如果是这种情况,请尝试不同的库或使用解决方法。

Now, doing this programmatically Currently requiresthe use of a third party library to save zip files and extract them. This is not ideal, since a lot of people / companies avoid using them. That being said, the answer marked correct, ZipArchive is a great third party tool, especially since it now supports Swift. I would recommend using it until Apple introduces a native library.

现在,以编程方式执行此操作目前需要使用第三方库来保存 zip 文件并提取它们。这并不理想,因为很多人/公司都避免使用它们。话虽如此,答案标记为正确,ZipArchive 是一个很棒的第三方工具,特别是因为它现在支持 Swift。我建议在 Apple 推出本机库之前使用它。

回答by Bob

On iOS6 a ZIP file seems to be handled like a folder. I had success by simply doing the following:

在 iOS6 上,ZIP 文件似乎像文件夹一样处理。我通过简单地执行以下操作获得了成功:

[[NSFileManager defaultManager] copyItemAtPath:[NSURL URLForCachesDirectoryWithAppendedPath:@"ZIP_FILE.zip/Contents/Test.m4a"].path
                                        toPath:[NSURL URLForCachesDirectoryWithAppendedPath:@"Test.m4a"].path
                                         error:nil];

There might be a chance you can even make a directory listing to unzip files with unknown content. Don't know about password protected ZIP files though.

您甚至可以制作一个目录列表来解压缩内容未知的文件。虽然不知道受密码保护的 ZIP 文件。