pixelglow / ZipZap

zip file I/O library for iOS, macOS and tvOS
BSD 2-Clause "Simplified" License
1.22k stars 199 forks source link

How to zip a folder? #139

Closed Heisenbean closed 8 years ago

Heisenbean commented 8 years ago

my folder has some files,i want to zip the folder,and i zip it like wiki https://github.com/pixelglow/ZipZap/wiki/Recipes but it has a problem,the files in the folder is not zip,it's create new files in the folder. simply,zipzap not zip my folder,it create a new folder and new files to zip. it's my code:

ZZArchive* newArchive = [[ZZArchive alloc] initWithURL:
                             [NSURL fileURLWithPath:[documentPath stringByAppendingPathComponent:@"hahaha.zip"]]
                                                   options: @{ZZOpenOptionsCreateIfMissingKey: @YES}
                                                     error:nil];

    NSArray *zips = @[[ZZArchiveEntry archiveEntryWithDirectoryName:@"default_20160307224822/"],
                      [ZZArchiveEntry archiveEntryWithFileName:[documentPath stringByAppendingPathComponent:@"default_20160307224822/default_20160307224822.db"]compress:YES dataBlock:^(NSError** error){
                          return [@"hello, world" dataUsingEncoding:NSUTF8StringEncoding];}],
                      [ZZArchiveEntry archiveEntryWithFileName:@"default_20160307224822/default_20160307224822.db-journal"compress:YES dataBlock:^(NSError** error){
                          return [@"hello, world" dataUsingEncoding:NSUTF8StringEncoding];}]
                      ];

    [newArchive updateEntries:zips error:nil];

so,the right way to zip a folder is?

pixelglow commented 8 years ago

To zip actual files, you'll need to open an NSStream or NSData from the file you want and pass that to the ZZArchiveEntry initializer. Something like:

[ZZArchiveEntry archiveEntryWithFileName:fileName
                                compress:YES
                               dataBlock:^(NSError** error)
    {
        return [NSData dataWithContentsOfFile:file
                                      options:0
                                        error:error];
    }]
Heisenbean commented 8 years ago

thanks~