In this tutorial, I am going to demonstrate how you can zip and unzip files from within your iOS applications. We will be using a third party library called ZipArchive to achieve this. While there are a couple solutions out there to zip and unzip files, I feel that the ZipArchive library was the fastest and easiest way to get up and running.
Why Would I want To Unzip Files?
That’s a great question. There are a number of reasons why you might want to support zipping and unzipping files inside of your applications. Here are just a few:
Apple’s 50 MB App Store Download Cap
Apple has imposed a 50MB download limit over 3G on applications downloaded to appease carriers and not hog all of their bandwidth. One way to circumvent this is to keep your binaries very small, and download the resources your application needs. The best way to package these resources is of course, a zip file. So the process (as you will see demoed below), is to open your app, check for resource updates, download the zip file, and unzip it. Now you can ship smaller applications, that fetch their resources dynamically.
Dynamic Updates To Content
I touched on this above. When your application needs updated assets, the common practice is to submit an update to the App Store. This could take up to a week for Apple to Review and publish. A faster approach is to zip your assets up and stick them on a server somewhere (even your Dropbox) and have your app download and unzip them. This way, whenever you want to make asset-based changes to your application, you won’t need to submit another copy of it to the store.
Downloading Zip Files From The Web
One huge downfall of Safari and the Mail app is, they are unable to open zipped files. It would be nice to offer some sort of support to view zipped archives on your device. Many of the “download” apps in the store support this, and you can too with some help from ZipArchive.
Setting Up Your Project
Head over to http://code.google.com/p/ziparchive/ and checkout a copy of ZipArchive. Or you can just type this into your terminal.
|
Once you have cloned the repository, delete the MakeFile file from the minizip folder. We will let XCode build our files. Now, drag this minizip folder as well as the ZipArchive.h and ZipArchive.mm files into your project, making sure to check “Create groups for any added folders”. Also, be sure your target is checked.
Note: No ARC Support
If you are using an ARC enabled project, you will need to tell the compiler not to use ARC for ZipArchive. To do this, click on the project in the left hand column. Then click on your target in the middle column and select the “Build Phases” tab.
Expand the “Compile Sources” area, locate ZipArchive.mm and double click on it. In the box that pops up, type in -fno-objc-arc and click Done.
Linking libz
The last step is to link your project against libz.1.2.5.dylib. From the Build Phases screen you navigated to above, expand the Link Binary With Librariessection and click the “+” button to add a new library. Search the list for libz.1.2.5.dylib, select it and click Add.
Now, compile your project and it should succeed with no errors. One thing to note is ZipArchive might produce some warnings, they are not a big deal, but if you are a warning Nazi (you should be), dig into the code and see if you can solve them yourself.
Downloading And Unzipping Files
I will now show you how easy it is to download a zip file from the web, unzip its contents, and use them in your project. The method of which we will download the files is very basic and most likely wouldn’t be used in production without further error reporting and checking.
The sample project has a view that looks like this:
It’s basically a UIImageView and a UILabel. Inside of the view controller I have set up IBOutlets for these two items. Make sure and download the sample project to see the implementation details. We will be downloading a zip file from the web that contains an image and a text file which you can see displayed in the screenshot above. Once unzipped, the image will be set as the viewable image in our UIImageView and the textual contents of the .txt file will be displayed inside of the UILabel.
**1. Import the ZipArchive headers **
|
2. Download the the zip file
|
This preliminary code downloads a zip file from iCodeBlog and saves it to the caches directory for the application.
- Creates a dispatch queue in which to run our code on with default priority.
- Quick and dirty way of fetching data from the web.
- Resolves the path to the caches directory and writes out the downloaded data to a local zip file
Now that you have it downloading the file to disk, it’s time to unzip that file and make use of it’s contents.
3. Unzipping the downloaded file
The last step here is to unzip the file you just downloaded. To be clear, it was save to the path /Library/Caches/zipfile.zip and once extracted, it’s contents will be inside of the Caches folder as well.
Replace the //TODO: Unzip in the code above, with the following code:
|
Here is an explanation of what’s going on.
- Opens the file and unzips it in memory
- Writes the unzipped contents out to a given path (Caches folder)
- Makes use of the unzipped files
- Updates the UI (on the main thread of course) with the newly fetched data.
It really is that simple.
Zipping Files
Now you are going to see how to go the other way and zip up some files on disk. Again, this could be particularly handy when you want to allow your users to share groups of files via the web or email.
If you completed the steps above, your caches folder should have some files lying around that we can just zip up again and send off. We are going to zip up the two files you previously unzipped, stuff them into a new zip file, and write that out to the documents directory.
In my sample project, I have created a new button at the top that says “Zip Files” which links to an IBAction called zipFilesButtonPressed: when tapped. That is where I will be doing the zipping:
|
Here is what’s going on:
- Resolve the path to the documents directory. We will need this when determining the path to write out our new zip file.
- Resolve the path to the caches directory. This is used to fetch the files that will be zipped up.
- Determines the path the zip file we will be writing out.
- Instantiates the ZipArchive object and tells it to create a new “in-memory” zip file. Note, the file won’t be written out until you call the corresponding CloseZipFile2 method.
- Resolve the path to the files that will be zipped up.
- Add the files to the zip archive. You can add as many files as you would like here. You can even add directories (ie you could have added the entire caches directory if you wanted to).
- Write out the zip file and close it. Just to test, we log the result to ensure that the zipping was successful.
should
Conclusion
You have now seen how to zip and unzip files on iOS devices using the ZipArchive library. You can download the sample project at the bottom of this post. As always, if you have any questions or comments, feel free to write them in the comments of this post or write them to me on Twitter @brandontreb.
Happy iCoding!
Download The Sample Project
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
加减法 #include int main(){ int a =3; int b=5; a=a+b; b=a-b; a=a-b; printf(“%dn%d”,a,b); return 0; } 利用^(按位异或) #include// int main(){…