How to zip folders in iPhone SDK?

14,292

Solution 1

I have used this code to create a zip file of the documents directory of my app and it worked

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
BOOL isDir=NO;
NSArray *subpaths;
NSString *exportPath = docDirectory;
NSFileManager *fileManager = [NSFileManager defaultManager];    
if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){
    subpaths = [fileManager subpathsAtPath:exportPath];
}

NSString *archivePath = [docDirectory stringByAppendingString:@"/test.zip"];

ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
for(NSString *path in subpaths)
{
    NSString *longPath = [exportPath stringByAppendingPathComponent:path];
    if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir)
    {
        [archiver addFileToZip:longPath newname:path];      
    }
}

if([archiver CloseZipFile2])
    NSLog(@"Success");
else
    NSLog(@"Fail");

Solution 2

ZipArchive is an Objective-C class to compress or uncompress zip files, which is base on open source code "MiniZip".

It can be used for iPhone application development, and cocoa on Mac OSX as well.

see this : http://code.google.com/p/ziparchive/downloads/list

Solution 3

To create a zip file you can use ZipArchive Download the source code and add it to your project, add also libz.x.dylib (take a look at wiki).

Then in your header file add: #import "ZipArchive/ZipArchive.h"

To create a zip file is simple, just use the following code:

BOOL ret = [zip CreateZipFile2:l_zipfile];
// OR
BOOL ret = [zip CreateZipFile2:l_zipfile Password:@"your password"]; //
//if the Password is empty, will get the same effect as [zip CreateZipFile2:l_zipfile];
ret = [zip addFileToZip:l_photo newname:@"photo.jpg"];
      if( ![zip CloseZipFile2] )
      {
      // error handler here
      }
      [zip release];

Solution 4

Easy mode: add ZipArchive to your project then:

NSString* zip = ...;
NSString* dir = ...;
[SSZipArchive createZipFileAtPath: zip withContentsOfDirectory: dir];

Solution 5

I've used ZipArchive with success in the past.

It's pretty ligthweight and simple to use, supports password protection, multiple files inside a ZIP, as well as compress & decompress.

The basic usage is:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"];
ZipArchive *zipArchive = [[ZipArchive alloc] init];
[zipArchive UnzipOpenFile:filepath Password:@"xxxxxx"];
[zipArchive UnzipFileTo:{pathToDirectory} overWrite:YES];
[zipArchive UnzipCloseFile];
[zipArchive release];

This is for unzipping a folder/file. To zip folders is equally easy. To zip a file (or a fodler)

           BOOL ret = [zip CreateZipFile2:l_zipfile];
            // OR
            BOOL ret = [zip CreateZipFile2:l_zipfile Password:@"your password"]; //
            //if the Password is empty, will get the same effect as [zip CreateZipFile2:l_zipfile];

            ret = [zip addFileToZip:l_photo newname:@"photo.jpg"];
            if( ![zip CloseZipFile2] )
            {
                    // error handler here
            }
            [zip release];

I have heard about ObjectiveC-Zip also.

Share:
14,292
Yogi
Author by

Yogi

an iPhoner SOreadytohelp

Updated on June 17, 2022

Comments

  • Yogi
    Yogi almost 2 years

    In my Application,I am taking screenshots of image View and then I am saving those screen shots in document folder of the application.Now I want to Email all those images with the same folder structure they are in.Zipping all the folders containing the images and then attaching the zip file to the mail will solve the problem but how can I zip these folders and then attach them to the mail?

    Any help is appreciated!

  • Yogi
    Yogi over 12 years
    Thanks for your response.I got how to unzip the contents of a Zip file and I read the documentation on google.But I am not getting how to create a new zip file and add items into it.
  • Yogi
    Yogi over 12 years
    Thanks...Can we add two files in same zip file.i.e.can we call [zip addFileToZip:l_photo newname:@"photo.jpg"]; for multiple files?When I tried that it ended up with only one file in zip.
  • Srikar Appalaraju
    Srikar Appalaraju over 12 years
    instead of giving a single file each time. put all your files in a folder & give the directory path so the entire dir with images is zipped.
  • Yogi
    Yogi over 12 years
    I think it doesn't work on folders.Does it?I tried it on folder with no luck.Really wanted help...
  • Kirtikumar A.
    Kirtikumar A. over 10 years
    I am not able to zip files from iphone device, i don't know why its happing, same coding workes in iphone simulator well but not in iphone device, please guide me
  • nacho4d
    nacho4d over 10 years
    The biggest difference between the simulator and device is that in the device all paths are case sensitive, in the simulator no. Maybe that is the reason why it works in one and not in the other.
  • Nico
    Nico over 10 years
    it only form all document contains in one zip formate file but it don't reduce the size of zip file. how can we reduce the size of zip file ? please guide. Thanks in advance.
  • Vish_iOS
    Vish_iOS about 10 years
    Thanks..It worked for me.. but If I encrypt my Xml file with RNEncryptor before zipping it, this doesn't work.. any solution.???