Use ZIP-archives to store NSDocument data

2.1k views Asked by At

I noticed that Apple started using zip archives to replace document packages (folders appearing as a single file in Finder) in the iWork applications. I'm considering doing the same as I keep getting support emails related to my document packages getting corrupted when copying them to a windows fileserver.

My questions is what would be the best way to do this in a NSDocument-based application?

I guess the easiest way would be to create a directory file wrapper, create an archive of it and return it in NSDocument's

- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError **)outError

But I fail to understand how to create a zip archive of the NSFileWrapper.

4

There are 4 answers

1
Joshua Nozzi On BEST ANSWER

If you just want to make a zip file your format (ie, "mydoc.myextension" is actually a zip file), there's no convenient, built-in Cocoa mechanism for creating zip archives with code. Take a look at this Google Code project: ziparchive I don't believe a file wrapper will help in that case, though.

Since you cited iWork, I don't own iWork 09, but previous versions use a package format (ie, NSFileWrapper would be ideal) but zip the XML that describes the document's structure, while keeping attachments (like embedded media, images, etc.) in a resource folder, all within the package. I assume they do this because XML can be quite large for large, complicated documents, but compresses very well because it's text. This results in an overall smaller document.

If indeed Apple has moved to making the entire document one big zip archive (which I would find odd), they'd either be extracting necessary resources to a temp folder somewhere or loading the whole thing into memory (a step backward from their package-based approach, IMO). These are considerations you'll need to take into account as well.

1
NSResponder On

Pierre-Olivier Latour has written an extension to NSData that deals with zip compression. You can get it here: http://code.google.com/p/polkit/

0
Ben Stiglitz On

You’ll want to take the data from the file wrapper and feed it into something like ziparchive.

0
dandax On

I know this is a little late to the party but I thought I'd offer up another link that could help anyone that comes across this post.

Looks like the ZipBrowser sample from Apple would be a good start http://developer.apple.com/library/mac/#samplecode/ZipBrowser/Introduction/Intro.html

HTH