5

I have an iPhone application that I have developed using ARC. I have a folder that contains a heap of images in my documents directory that I need to zip up and e-mail. My project uses ARC.

Does anyone have any example code/links to a resource that would be helpful to me?

I've been raking around online and what I can find is not compatible with ARC - even when it claims to be.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Jack Nutkins
  • 1,555
  • 5
  • 36
  • 71
  • 6
    You know that you can turn off ARC on a file-by-file basis, correct? So if you have non-ARC code you like, you can still use that in an ARC project. ARC is not all-or-nothing; it's a per-file compiler option. http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project – Rob Napier Jul 25 '12 at 12:17
  • And to further clarify, if you have a Xcode project that builds such an Objective-C library, and its not ARC, you can include that project in your project and use the library, regardless if its ARC or not. – David H Jul 25 '12 at 13:00
  • Oh, no I wasn't aware of that, cheers. – Jack Nutkins Jul 25 '12 at 13:03
  • 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

1 Answers1

8

Download and drag the Objective-Zip, MiniZip and ZLib drag in to your project from this link http://code.google.com/p/objective-zip/downloads/list (Objective-zip). import the files: ZipFile.h, ZipException.h, FileInZipInfo.h, ZipWriteStream.h, ZipReadStream.h, zlib.h

Use this code. Please see below:

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];
knut
  • 27,320
  • 6
  • 84
  • 112
Tripti Kumar
  • 1,559
  • 14
  • 28