Zip/Unzip files on iPhone/iPad/iOS


  1. Download ZipArchive from http://code.google.com/p/ziparchive/downloads/list.
  2. Drag into project.
  3. Add to project existing framework libz.1.2.3.dylib (last version).
  4. Import to project:
    #import "ZipArchive.h"
    
  5. Uncompress zip file example:
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"myZipFileName.zip"];
    
    NSString *output = [documentsDirectory stringByAppendingPathComponent:@"unZipDirName"];
    
    ZipArchive* za = [[ZipArchive alloc] init];
    
    if( [za UnzipOpenFile:zipFilePath] ) {
        if( [za UnzipFileTo:output overWrite:YES] != NO ) {
            //unzip data success
            //do something
        }
    
        [za UnzipCloseFile];
    }
    
    [za release];
    
  6. Compress directory or file example:
    BOOL isDir=NO;
    
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSArray *subpaths;
    
    NSString *toCompress = [NSString stringWithString:@"dirToZip_OR_fileNameToZip"];
    NSString *pathToCompress = [documentsDirectory stringByAppendingPathComponent:toCompress];
    
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:pathToCompress isDirectory:&isDir] && isDir){
        subpaths = [fileManager subpathsAtPath:pathToCompress];
    } else if ([fileManager fileExistsAtPath:pathToCompress]) {
        subpaths = [NSArray arrayWithObject:pathToCompress];
    }
    
    NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"myZipFileName.zip"];
      
    ZipArchive *za = [[ZipArchive alloc] init];
    [za CreateZipFile2:zipFilePath];
    if (isDir) {
        for(NSString *path in subpaths){  
            NSString *fullPath = [pathToCompress stringByAppendingPathComponent:path];
            if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir){
                [za addFileToZip:fullPath newname:path];  
            }
        }
    } else {
        [za addFileToZip:pathToCompress newname:toCompress];
    }
    
    BOOL successCompressing = [archiver CloseZipFile2];
    
    

8 comments:

  1. Thanks you sir,

    It helped me a lot.

    _MS

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hello everyone, I'm sviluppadso an application for iPad using xcode4.2. Using these instructions I am to have this error

      Undefined symbols for architecture i386:
         "_OBJC_CLASS_ $ _ZipArchive", Referenced from:
             objc-class-ref in MainView.o
      ld: symbol (s) not found for architecture i386

      Can you help me pls do not know how to do ...
      please note that I am using the version libz1.2.5 the last exit and development for ios 5.1

      Delete
    2. When you drag the ZipArchive folder into your project, you have to choose "Create groups for any added folders" and not "Create folder references for any added folders."

      Delete
  3. great tutorial helped me a lot

    ReplyDelete
  4. I can't make this work today. Lots of errors. maybe because xcode version 7.x.x

    ReplyDelete