Unzip the files in IOS using ZipArchive

Posted by Unknown Senin, 25 Februari 2013 0 komentar

this tutorial I will show you how to compress and decompress files from their iOS apps. We will use a third party library called ZipArchive to achieve this. While there are some solutions out there to compress and decompress files, I think ZipArchive library was the quickest and easiest way to get started.


Why do I unzip files?

‘s a big problem. There are a number of reasons why you may want to support compression and decompression of files from within your applications. Here are some examples:

Apple App Store download 50MB Cap

Apple introduced a download limit of 50MB via 3G in apps downloaded to appease carriers and not monopolize your entire band. One way around this is to keep your binaries very small, and transfer resources to their application needs. The best way to package these resources is obviously a zip file. Thus, the process (as you will see demoed below), is to open your application, check the updates of Resources to download the zip file and unzip it. Now you can send smaller applications, which recovers its resources dynamically.

dynamic updates to content

I talked about this above. When your application needs updating assets is common to present an update to the App Store. This may take up to a week to Apple for review and publishing. A faster method is to close their assets and put them on a server somewhere (even your Dropbox) and have your app download and unzip them. That way when you want to do asset-based changes in the program, you need not submit a copy of it to the store.

Download Zip files from the Web

A big fall Safari and Mail app is, they are not open zipped files. It would be nice to offer some kind of support to show zipped file on the device. Many of the “download” apps in the store to support this process, and you can too with a little help from ZipArchive.


configure projects class = “code”> svn checkout http : / / ziparchive.googlecode. com / svn / trunk / ZIPARCHIVE-read-only groups to all folders added.” Also, make sure your target is selected.

Note: No support ARC

If you use an ARC enabled project, you must tell the compiler not to use ARC to ZipArchive. To do this, click the project in the left column. Then click on your target in the middle column and select “Build phases” tab.

Expand “Gather Supplies” area, locate ZipArchive.mm and double click it. In the box that appears, type -FNO-objc-arc and click Done.

link libz

The last step is to link your project to libz.1.2.5.dylib . Construction screen you sailed above link expand binary library and click the “+” button to add a new library. Look for libz.1.2.5.dylib list, select it and click Add.

Now compile the project and he would succeed without errors. One thing to note is ZipArchive can produce some warnings, they are no big deal, but if you’re a Nazi warning (which should be), dig into the code and see if you can solve them alone.


download and extract files

The sample project has a vision like this:.

It is basically a UIImageView and UILabel. Inside View Controller IBOutlets raised for these two items. Be sure and download the sample project to see the implementation details. We will download a zip file from the web that contains an image and a text file which you can see displayed in the image above. When unzipped, the image is set to the visible image of our textual content UIImageView and i txt file will be displayed inside the UILabel.

** 1. Import headers ZIPARCHIVE **

class = “code”> # import "ZipArchive.h" class = “code”> style = / / 1 = dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ) , dispatch_async ( queue, ^ { NSURL * url = [ NSURL URLWithString @ " http://www . icodeblog.com / wp-content/uploads/2012/08/zipfile.zip " ] NSError * = null , / / 2 NSData # * data = [ NSData : url options 0 wrong: & #] if ( wrong) { / / 3 NSArray = ( NSCachesDirectory, NSUserDomainMask ) , NSString * path = [ 0 ] NSString * [ stringByAppendingPathComponent Path: @ "zipfile.zip" ] , [ : zipPath options : 0 wrong: & error ] , if ( wrong) { / / TODO: Extract } a61390; “cor: # else { ( ” Error saving the file% @ “ style = “color: “> else { ( ” Error downloading the zip file :% @ “, 002.200, “color: #) , } } 002200; “cor: #) Creates a transmission queue to run our code on the default priority. quick and dirty way to get data from the web. Resolve the path to the cache directory and print the transferred data to a zip file location

Now that you have

downloads the file to disk, it is time packaging store and use the content.

third Unzip the downloaded file

The last step here is to unzip the file you just downloaded. To be clear, it saves the path / Library / Caches / zipfile.zip and when extracted, the content to be included in the folder caches too.

Change / / TODO: Pack in the above code with the following code:

class = ZipArchive * = [ [ Alloc ZipArchive ] ] / / 1 if ( [ : ] 002200; "cor: #) { / / 2 BOOL = [ UnzipFileTo za: Path Replace ] , if ( NO == ) { } [ ] / / 3 NSString * [ stringByAppendingPathComponent Path: @ ] , NSString * = [ stringByAppendingPathComponent Path: @ "text.txt" ] NSData * = [ NSData : : 0 null ] , * = [ ] , NSString * # string text = [ NSString : text encoding file path NSASCIIStringEncoding wrong: null ] , / / 4 ( ( ) ^ { = img; self. . = string text, } ) . Opens file and unpack it in memory Writes the contents extracted to a certain path ( caches folder) uses the extracted files Update UI (the wire, of course) with new data downloaded.

It's really very simple.

closing files Now you will see how to go the other way and closing some files on the hard disk. Again, this can be especially useful when you want your users to share groups of files via the web or email.

If you have completed the steps above, your cache folder has some files out there that we can just close it again and send away. Let's close the two files that you are pre-packaged, stuff them into a new zip file and print it to the file directory.

In my sample project, I created a new button at the top that says "zip" that links to a IBAction called zipFilesButtonPressed: when tapped. This is where I will do zip:

class = "code"> - ( ) : ( ID ) sender { / / 1 NSArray * = ( NSDocumentDirectory, NSUserDomainMask, Yes ) , NSString * = [ paths will 0 ] , / / 2 = ( NSCachesDirectory, NSUserDomainMask, YES ) style = NSString * cache path = [ : 2400d9; "color: # 0 ] / / 3 NSString * zipfile = style = # [ " newzipfile.zip " ] "> / / 4 * [ [ #] ] , [ ] , "> / / 5 NSString * = [ : @ " photo.png " 002200, "color: #] NSString * path = text "style 002200; "> @ " text.txt " ] "> / / 6 [ : @ "NewPhotoName.png" ] style [ # : # @ "NewTextName.txt" ] "> / / 7 BOOL = style = # [ ] , NSLog ( @ "Compressed% d result" , the success ) }

Here's what's happening:

resolve the path to the documents directory. We will need this in determining the road to write our new zip file. resolve the path to the cache directory. This is used to retrieve the files to be zipped. Zip file determines the way we print. ZipArchive instance object and tell it to create a new "in memory" zip file. Note that the file is not printed until you call the method corresponding CloseZipFile2. The zip resolve the path to the files to be zipped. add the file archives. You can add as many files as you want here. You can also add directories (ie you could have put the entire cache directory if you wanted). Print zip file and close it. Just to test, we recorded the results to ensure that zipping managed.

After running through the program, tap "zip" button, look for the Documents folder of the application (located ~ / Library / Application Support / iPhone Simulator / [iOS version] / Applications / [Unique ID] / Documents]. should contain a single zip file called newzipfile.zip. If you unzip it, you should see the two files that you put in it is extracted. Conclusion

Congratulations iCoding

Download sample projects XTZ

::: http://www.icodeblog.com/2012/ 08/13/unzipping-files-using-zip-archive / :::

TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Unzip the files in IOS using ZipArchive
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke http://apk-zippy.blogspot.com/2013/02/unzip-files-in-ios-using-ziparchive.html. Terima kasih sudah singgah membaca artikel ini.

0 komentar:

Posting Komentar

Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of apk zippy.