Huy Tran
Huy Tran

Reputation: 4431

Unzipping downloaded files in iOS

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?

Upvotes: 27

Views: 33501

Answers (3)

Henry F
Henry F

Reputation: 4980

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.

Upvotes: 2

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73618

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];

Upvotes: 51

Bob
Bob

Reputation: 1

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.

Upvotes: -5

Related Questions