Search code examples
iosobjective-cphp-ziparchive

Unzip Files in iOS


I have a .zip folder which contains some images. I want it to unzip into Documents directory in iOS device. I tried with the following link,

download and unzip file in iOS-Stack Overflow Here is the code that I tried in my app,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask,
                                                     YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filepath = [documentsDirectory stringByAppendingPathComponent:@"Obj.zip"];
ZipArchive *zipArchive = [[ZipArchive alloc] init];
[zipArchive UnzipOpenFile:filepath Password:@""];
[zipArchive UnzipFileTo:[documentsDirectory stringByAppendingPathComponent:@"Obj"] overWrite:YES];
[zipArchive UnzipCloseFile];

And then I printed the all files in Document Directory, but the Obj.zip is the only file(Obj.zip is the file that wants to unzip) that exist in the Documents Directory. How can I done this?

Thanks in Advance!


Solution

  • Use SSZipArchive. Here is a sample code. https://github.com/soffes/ssziparchive

    Also I believe the unzipping would create a folder in documents directory which would contain the files. Please check that

    NSString *zipPath = [[NSBundle mainBundle] pathForResource:zipFileName ofType:@"zip"];
    NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
    

    Update : Get zip file from documents directory

    NSString *filename = @"MyZipFile.zip";
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString * zipPath = [documentsDirectory stringByAppendingPathComponent:filename];
    
    NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];