Search code examples
ioscocoa-touchzipnskeyedarchiver

Archiving on iOS using NSKeyedArchiver


I'd like my app to decompress and handle the contents of zip files.

I'm trying to do this the Cocoa way, without using external libraries, so I'm turning to NSKeyedUnarchiver and here's my code:

-(void) unarchive{

    NSString *outputDirectory = [[NSHomeDirectory() stringByAppendingString:@"/Documents/"] stringByAppendingString:@"TheNewFolderName/"];
    NSLog(@"Output Directory: %@", outputDirectory);

    //MyArchive.zip is the filename of the zip file I placed in Xcode
    NSData *theArchive = [NSKeyedUnarchiver unarchiveObjectWithFile:@"MyArchive.zip"];

    NSError *error;
    [theArchive writeToFile:outputDirectory options:NSDataWritingAtomic error:&error];

    NSLog(@"theArchive is %@", theArchive);

And this prints "theArchive is null"! What am I doing wrong..?


Solution

  • NSKeyedArchiver and NSKeyedUnarchiver are used for archiving and unarchiving objects (NSObject subclasses, with certain restrictions) in your application. I'm afraid you cannot use it with zip files.