27

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?

RoShamBo
  • 41
  • 4
Huy Tran
  • 4,371
  • 10
  • 34
  • 38

3 Answers3

51

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];
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
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.

Henry F
  • 4,960
  • 11
  • 55
  • 98
-5

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.

Bob
  • 1
  • 1
  • Doesn't work for me either: NSUnderlyingError=0x8594310 "The operation couldn’t be completed. Not a directory". Including the correct zip library doesn't seem to help. – Rick77 Oct 21 '13 at 08:25