{"id":3553,"date":"2013-01-11T10:59:22","date_gmt":"2013-01-11T05:59:22","guid":{"rendered":"http:\/\/aasims.wordpress.com\/?p=3553"},"modified":"2025-04-09T12:22:13","modified_gmt":"2025-04-09T12:22:13","slug":"tutorial-how-to-play-with-zip-files-in-ios","status":"publish","type":"post","link":"https:\/\/aasimnaseem.com\/blog\/tutorial-how-to-play-with-zip-files-in-ios\/","title":{"rendered":"Tutorial: How to play with zip files in iOS;"},"content":{"rendered":"<p>Hello Dev mates&#8230;<img loading=\"lazy\" decoding=\"async\" class=\"alignright\" src=\"http:\/\/www.topsofts.com\/pop\/file-compression\/img\/winzip1.gif\" alt=\"\" width=\"88\" height=\"88\" \/><br \/>\nHope you are enjoying your work in\u00a0winters; (:<\/p>\n<p>Few days back I was working on a project where I needed to compress some files to create a zip archive; Although my\u00a0teammate\u00a0has done much of the work, I just refined some business logic and finalized the code to complete this feature; So lets start and see how we achieved this;<\/p>\n<p><strong>1. Setup your project<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/code.google.com\/p\/ziparchive\/downloads\/list\" target=\"_blank\" rel=\"noopener\">Download an open source library from here<\/a>; It is a very handy library for zip\/unzip operations, written by\u00a0Shannon Ai (acsolu@gmail.com)<em><br \/>\n<\/em><\/li>\n<li>Create new project in Xcode (or open your existing one)<\/li>\n<li>Add\u00a0<em><a href=\"http:\/\/stackoverflow.com\/questions\/6932991\/libz-dylib-versus-libz-1-2-3-dylib-versus-libz-1-2-5-dylib\" target=\"_blank\" rel=\"noopener\">libz.1.2.3.dylib<\/a>\u00a0<\/em>into your project\u00a0frameworks;<\/li>\n<li>Drag and drop downloaded library into your project and chose &#8220;copy items&#8221; option from a popup window;<\/li>\n<li>Add the following values in to your Constants file; If you are using .pch file, then declare then using @define macro;<!--more--><\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221; wraplines=&#8221;false&#8221;]<br \/>\n\/\/in .h file<br \/>\nFOUNDATION_EXPORT NSString *const kZipFileName; \/\/name of zip file to be created;<br \/>\nFOUNDATION_EXPORT NSString *const kDirNameToCompress; \/\/name of directory who&#8217;s contents will be compressed;<br \/>\nFOUNDATION_EXPORT NSString *const kTargetDirName;\/\/directory name where zip file will be extracted;<br \/>\nFOUNDATION_EXPORT NSString *kZipFileCreateErrorMessage; \/\/error message string;<br \/>\nFOUNDATION_EXPORT NSString *kErroMessageTitle; \/\/error title;<br \/>\nFOUNDATION_EXPORT NSString *kOKButtonTitle; \/\/Title of cancel button over alert view;<\/p>\n<p>\/\/in .m file<br \/>\nNSString *const kZipFileName = @&#8221;CompressedData.zip&#8221;;<br \/>\nNSString *const kTargetDirName = @&#8221;AnyDirName&#8221;;<br \/>\nNSString *const kDirNameToCompress @&#8221;dirToZip_OR_fileNameToZip&#8221;;<br \/>\nFOUNDATION_EXPORT NSString *kZipFileCreateErrorMessage = @&#8221;Unable to to create zip file. Please try again later&#8221;;<br \/>\nFOUNDATION_EXPORT NSString *kErroMessageTitle = @&#8221;Error&#8221;;<br \/>\nFOUNDATION_EXPORT NSString *kOKButtonTitle = @&#8221;Ok&#8221;;<br \/>\n[\/code]<\/p>\n<p><strong>2. Create a zip file in any directory under\/Document;<\/strong><\/p>\n<ul>\n<li>#import &#8220;ZipArchive.h&#8221; into your class where you want to write zip code;<\/li>\n<li>Use the following code to create a zip file;<\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221; wraplines=&#8221;false&#8221;]<br \/>\n-(void) createZipFile{<br \/>\nNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);<br \/>\nNSString *documentsDirectory = [paths objectAtIndex:0];<br \/>\nNSString *zipFile = [documentsDirectory stringByAppendingPathComponent:kZipFileName];\/\/The place where zip file will be created;<br \/>\nZipArchive *zip = [[ZipArchive alloc] init];<br \/>\nBOOL result = [zip CreateZipFile2:zipFile];\/\/this line will create an empty zip file with 0 KB in size;<br \/>\nif(result){<br \/>\nNSError *error;<br \/>\nNSArray *allFiles = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:kDirNameToCompress error:&amp;error];\/\/get list of all files in directory which need to compress;<br \/>\nfor(int index = 0; index&lt;allFiles.count; index++){<br \/>\nid singleFileName = [allFiles objectAtIndex:index];<br \/>\nNSString *singleFilePath = [kDirNameToCompress stringByAppendingPathComponent:singleFileName];<br \/>\n[zip addFileToZip:singleFilePath newname:singleFileName];\/\/one file has been added into zip archive<br \/>\n}\/\/end for<br \/>\n[zip CloseZipFile2];<br \/>\n[zip release];\/\/ignore this if you are using ARC<br \/>\n}else{<br \/>\n[self showErrorMessage:kZipFileCreateErrorMessage withTitle:kErroMessageTitle];<br \/>\n}\/\/end else<br \/>\n}\/\/end createZipFile<br \/>\n\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n-(void) showErrorMessage:(NSString *)paramErrorMessage withTitle:(NSString*) paramTitle{<br \/>\nUIAlertView *alert = [[UIAlertView alloc] initWithTitle:paramTitle message:paramErrorMessage<br \/>\ndelegate:nil<br \/>\ncancelButtonTitle:kOKButtonTitle otherButtonTitles:nil];<br \/>\n[alert show];<br \/>\n[alert release];\/\/ignore this if you are using ARC;<br \/>\n}\/\/showErrorMessage<br \/>\n[\/code]<\/p>\n<p><strong>3. Decompress a zip archive;<\/strong><\/p>\n<ul>\n<li>#import &#8220;ZipArchive.h&#8221; into your class where you want to write unzip code;<\/li>\n<li>Use the following code to extract a zip file;<\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221; wraplines=&#8221;false&#8221;]<br \/>\n-(void) decompressZipArchive{<br \/>\nNSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];<br \/>\nNSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:kZipFileName];<br \/>\nNSString *targetDirPath = [documentsDirectory stringByAppendingPathComponent:kTargetDirName];<br \/>\nZipArchive* zipArchive = [[ZipArchive alloc] init];<br \/>\nif( [zipArchive UnzipOpenFile:zipFilePath] ) {<br \/>\nif( [zipArchive UnzipFileTo:targetDirPath overWrite:YES] != NO ) {\/\/one magical line<br \/>\n\/\/do whatever you want to do with uncompressed data<br \/>\n\/\/or just leave as it is after handling errors<br \/>\n}\/\/end if<br \/>\n[zipArchive UnzipCloseFile];<br \/>\n}\/\/end if<br \/>\n[zipArchive release];\/\/ignore this if you are using ARC<br \/>\n}\/\/end decompressZipArchive<br \/>\n[\/code]<\/p>\n<p>Thats all; You can use this code as it is into your project; Leave a comment if you find yourself in any trouble;<\/p>\n<p>Happy coding, and happy winters;<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/s11.flagcounter.com\/count\/4UPE\/bg_FFFFFF\/txt_000000\/border_FFFFFF\/columns_8\/maxflags_250\/viewers_0\/labels_1\/pageviews_1\/flags_0\/\" alt=\"Flag Counter\" border=\"0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Dev mates&#8230; Hope you are enjoying your work in\u00a0winters; (: Few days back I was working on a project where I needed to compress some files to create a zip archive; Although my\u00a0teammate\u00a0has done much of the work, I just refined some business logic and finalized the code to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":4549,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-3553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/3553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/comments?post=3553"}],"version-history":[{"count":1,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/3553\/revisions"}],"predecessor-version":[{"id":4550,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/3553\/revisions\/4550"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media\/4549"}],"wp:attachment":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media?parent=3553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/categories?post=3553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/tags?post=3553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}