Unzipping downloaded files in iOS

醉酒当歌 提交于 2019-11-26 05:28:25

问题


Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files. I tried to unzip the file with SSZipArchive and ZipArchive, which are both based on 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.

How do I unzip this file in iOS?


回答1:


I've used ZipArchive with 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.

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 SSZipArchive in some projects. Below line would unzip your zip file.

[SSZipArchive unzipFileAtPath:path toDestination:destination];



回答2:


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 iOS Some 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 probably not supported. Try a different library or use a workaround if this is the case.

Now, doing this programmatically Currently requires the 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.




回答3:


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

[[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.



来源:https://stackoverflow.com/questions/11333399/unzipping-downloaded-files-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!