Mr.Anonymous
Mr.Anonymous

Reputation: 820

How to zip a folder using ZipKit?

Id like to zip a folder using ZipKit??

I cant seem to locate a good documentation for the usage of functions in the ZipKit library. Can some one explain the method for folder zipping?

ZKFileArchive *archive = [ZKFileArchive archiveWithArchivePath:filePath];
[archive deflateDirectory:param1 relativeToPath:param2 usingResourceFork:NO];

What needs to be passed in param1 and param2??I dont understand the function call here?

It would be great if some one could post an example for it?

Thank you!

Upvotes: 0

Views: 1028

Answers (2)

rmaddy
rmaddy

Reputation: 318794

I created the following category method for NSFileManager using ZipKit.

- (BOOL)zipContentsOfDirectoryAtPath:(NSString *)directory toPath:(NSString *)filename recursive:(BOOL)recursive {
    // If there is already a file at the destination, delete it
    if ([self fileExistsAtPath:filename]) {
        [self removeItemAtPath:filename error:nil];
    }

    @try {
        ZKFileArchive *archive = [ZKFileArchive archiveWithArchivePath:filename];
        NSInteger result = [archive deflateDirectory:directory relativeToPath:directory usingResourceFork:NO];

        return result == zkSucceeded;
    }
    @catch (NSException *exception) {
        if ([self fileExistsAtPath:filename]) {
            [self removeItemAtPath:filename error:nil];
        }
    }

    return NO;
}

The directory parameter is a path to the directory (and its contents) that you wish to the zip up. The filename parameter is a path to the resulting zip file you want as a result.

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89509

Looking at the answer in this related question, here's a good example to work with.

Your param1 is the folder (with it's path) to be archived, and the relative path could be the parent folder.

NSString *zipFilePath = @"/Documents/zipped.zip";
ZKFileArchive *archive = [ZKFileArchive archiveWithArchivePath:zipFilePath];
NSInteger result = [archive deflateDirectory:@"/Documents/myfolder" relativeToPath:@"/Documents" usingResourceFork:NO];

It would be nice if ZipKit had better documentation than the limited info it has.

Upvotes: 2

Related Questions