Easiest way to zip and unzip files in your iOS app (Xcode)


If you're searching Google for a solution to zipping and unzipping files in your app then you're likely to encounter ZipArchive first of all. However, there is a simplified version called SSZipArchive that won't throw ARC-related warnings at you when you try and run it.

Notes

  1. To enable the use of SSZipArchive, you first download the files from GitHub.
  2. Add the files from the SSZipArchive folder (i.e. SSZipArchive.mSSZipArchive.h and the whole of the minizip folder) to your project
  3. Next click on the blue project file in Xcode and go to the Library and Frameworks section
  4. Add the libz.dylib library to your app (it is always present in Xcode, you don't need to download anything extra)
  5. Finally, wherever you use the SSZipArchive class remember to import the header file
All will now work as expected when you follow the usage instructions provided on GitHub.

EPUB Warning

If you are working with EPUB files in Xcode and want to unzip them, then dragging and dropping them into your project and doing this sort of thing:

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"MyBook" withExtension:@"epub"];

will not allow you to build a path or url to the file. Unlike the zip file where you can do this, I've found that EPUB files work best if they are situated in the Documents or Library directory, and have a path built to them using the NSFileManager class.

What to do about EPUBs

The above will mean you can't experiment with EPUBs quite so easily as zip files, but instead will likely need to add the "Open In" file handling to your app.

In order to register the EPUB UTI, you need to know that the document Type is as follows:

org.idpf.epub-container

and for more information on "Open In" and registering UTIs for your app, see here.

Links

SSZipArchive (GitHub)

ZipArchive (GitHub)

NSFileManager class (Apple, Developer)

What is the uniform type identifier for ePub files? (StackOverflow)

Endorse on Coderwall

Comments

  1. Hi! I am just getting an error saying: Reference to 'unz_file_info' is ambiguous. Any idea? Thanks!

    ReplyDelete
  2. How can i read EPUB file in Documents?. It's seem only works with Bundle file.

    ReplyDelete
    Replies
    1. You'll need to follow the instructions under the heading "What to do about EPUBs"

      Delete

Post a Comment