Question:
Development target: Mac OS X Lion, Xcode
I have a NSFileWrapper that I want to zip up. If it is a regular file, I can zip it fine by passing [newZip AppendData:[thisWrapper preferredFilename] data:[thisWrapper regularFileContents]]
After WriteZipandClose, the file is a perfect zip and I can open it.
However, if the FileWrapper is a directory ([thisWrapper isDirectory]), I'm having some problems. I initially tried passing -serializedRepresentation into AppendData: [newZip AppendData:[thisWrapper preferredFilename] data:[thisWrapper serializedRepresentation]];
But this does not create a valid file. The format is corrupt somehow. Honestly, I would have been surprised if it had worked, so no worries there.
So, I wrote a recursive routine that walks down the FileWrapper tree (with [thisWrapper fileWrappers]). However, when I try to add zips together, I get a non-unzippable file. I suspect it's because the resulting zip has multiple headers. Here's my current code:
- (NSData *)zipDirectory:(NSFileWrapper *)directoryWrapper;
{
BOOL success;
MGGZip *newZip = [[MGGZip alloc] init]; //MGGZip subclass of CKOzip. init handles 'unlock component'
success = [newZip NewZip:[NSString stringWithFormat:@"~/ziptest/%@.zip", [directoryWrapper preferredFilename]]];
if (success != YES) {
NSLog(@"Failure: %@", newZip.LastErrorText);
return nil;
}
[newZip setPathPrefix:[NSString stringWithFormat:@"%@/",[directoryWrapper preferredFilename]]];
NSDictionary *fileWrappers = [directoryWrapper fileWrappers];
for (id wrapperKey in fileWrappers) {
NSFileWrapper *thisWrapper = [fileWrappers objectForKey:wrapperKey];
if ([thisWrapper isDirectory]) {
NSData *returnedData = [self zipDirectory:thisWrapper];
[newZip AppendData:[thisWrapper preferredFilename] data:returnedData];
} else {
[newZip AppendData:[thisWrapper preferredFilename] data:[thisWrapper regularFileContents]];
}
}
return [newZip WriteToMemory];
}
Called from:
MGGZip *newZip = [[MGGZip alloc] init];
success = [newZip NewZip:[NSString stringWithFormat:@"~/ziptest/%@.zip", [thisWrapper preferredFilename]]];
if (success != YES) {
NSLog(@"Failure: %@", newZip.LastErrorText);
return;
}
NSData *returnedData = [self zipDirectory:thisWrapper];
[newZip AppendData:[thisWrapper preferredFilename] data:returnedData];
[newZip WriteZipAndClose];
Any suggested ways to make this happen? I was going to play with CkoZipEntry, but there's no way to add the folder structure?
Thanks.