1

Is it possible to create .zip files in objective C ?

Any libraries available or suggestions ?

sth
  • 222,467
  • 53
  • 283
  • 367
Biranchi
  • 16,120
  • 23
  • 124
  • 161
  • Dup: creating-a-zip-archive-from-a-cocoa-application: http://stackoverflow.com/questions/1928162/creating-a-zip-archive-from-a-cocoa-application – miku Jan 29 '10 at 10:52
  • Also a duplicate of http://stackoverflow.com/questions/286496/how-can-i-create-a-zip-file-by-using-objective-c – Brad Larson Jan 29 '10 at 18:24
  • Do you really want to create a zip file or just compress some data? You can compress data using the existing [zlib](http://www.zlib.net) libraries very easily. – dk. Jan 29 '10 at 17:16
  • Maybe check out this built-in way to create a ZIP file: https://stackoverflow.com/a/32723162 – Bruno Bieri Dec 17 '20 at 19:32

3 Answers3

5

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

nlg
  • 161
  • 1
  • 8
4

BEfore someone mentions http://code.google.com/p/ziparchive/ .. I evaluated that code and it is pretty terrible. I ended up using it for a quick demo hack that had to do but I would never use it in production. ZipKit http://bitbucket.org/kolpanic/zipkit/wiki/Home seems to be in much better shape.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
0
NSString *stringPath1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]];
NSString *FileName=[stringPath1 stringByAppendingPathComponent:@"Your file name"];


NSString *stringPath=[stringPath1 stringByAppendingPathComponent:[@"Your file name" stringByAppendingFormat:@".zip"]];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:FileName error:&error];
ZipFile *zipFile = [[ZipFile alloc]initWithFileName:stringPath mode:ZipFileModeCreate];

for(int i = 0;i<files.count;i++){

    id myArrayElement = [files  objectAtIndex:i];
    NSLog(@"add %@", myArrayElement);

    NSString *path = [FileName 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];
    [streem writeData:data];
    [streem finishedWriting];
}

[zipFile close];
codercat
  • 22,873
  • 9
  • 61
  • 85