login about faq

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.

asked Mar 08 '13 at 18:52

mark's gravatar image

mark
3236

Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×87
×8

Asked: Mar 08 '13 at 18:52

Seen: 1,180 times

Last updated: Mar 08 '13 at 18:52

powered by OSQA