How can I create a zip file by using Objective C?

I am developing an iOS application, and trying to zip the file I have created in the application, is there any built in function able to do this?


I definitely recommend Objective-Zip. It just recently moved to https://github.com/flyingdolphinstudio/Objective-Zip:

Some examples from their documentation:

Creating a Zip File:

ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeCreate];

Adding a file to a zip file:

ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"abc.txt" compressionLevel:ZipCompressionLevelBest];

[stream writeData:abcData];
[stream finishedWriting];

Reading a file from a zip file:

ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];

[unzipFile goToFirstFileInZip];

ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];

[read finishedReading];

As Alex pointed out, I responded to this question by pointing out the NSData category contributed by users of the Cocoadev wiki. That category includes methods for dealing with zipped and gzipped data in an NSData instance (that could be read from a Zip file or written to one). This should be all you need to implement the file zipping you describe, as long as you can feed your file data into an NSData instance.

For an example of this category in action, please see the source code for my iPhone application, Molecules. I only use the method to extract data from a gzipped file (in SLSMolecule+PDB.m), but you should be able to get the basic concepts from that.


first you download Objective-zip example from http://code.google.com/p/objective-zip/downloads/list

in this example Find and copy three folder Objective-Zip, MiniZip and ZLib drag in to your project

import two class in you .m class "ZipFile.h" and "ZipWriteStream.h"

create method of zip my code is :-

-(IBAction)Zip{
self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);

NSString *ZipLibrary = [paths objectAtIndex:0];


NSString *fullPathToFile = [ZipLibrary stringByAppendingPathComponent:@"backUp.zip"];

//[self.fileManager createDirectoryAtPath:fullPathToFile attributes:nil];

//self.documentsDir = [paths objectAtIndex:0];


ZipFile *zipFile = [[ZipFile alloc]initWithFileName:fullPathToFile mode:ZipFileModeCreate];

NSError *error = nil;
self.fileManager = [NSFileManager defaultManager];
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);

self.documentsDir = [paths1 objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:self.documentsDir error:&error];
//for(NSString *filename in files){
    for(int i = 0;i<files.count;i++){

        id myArrayElement = [files  objectAtIndex:i];


    if([myArrayElement rangeOfString:@".png" ].location !=NSNotFound){
            NSLog(@"add %@", myArrayElement);


    NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
    NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
    NSDate *Date = [attributes objectForKey:NSFileCreationDate];

    ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
    NSData *data = [NSData dataWithContentsOfFile:path];
    //  NSLog(@"%d",data);
    [streem writeData:data];
    [streem finishedWriting];
        }else if([myArrayElement rangeOfString:@".txt" ].location !=NSNotFound)
        {

            NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
            NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
            NSDate *Date = [attributes objectForKey:NSFileCreationDate];

            ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
            NSData *data = [NSData dataWithContentsOfFile:path];
            //  NSLog(@"%d",data);
            [streem writeData:data];
            [streem finishedWriting];
    }
}

[self testcsv];
[zipFile close];

}

your documents directory saved item .png and .txt files zipping in Library folder with backup.zip i hope this is helps