views:

353

answers:

3

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  A: 

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.

Joshua Nozzi
Thank you for your reply. I wasn't aware of the ziparchive project.Yes they changed this in iWork'09. Now native files are zipped archives. You can unzip them and still see the xml and the resources.At the moment I'm considering writing the NSFileWrapper to a temp directory, zipping it (ditto), reading it back in and returning it in above mentioned method. This appears to be the best way as NSDocument will make sure to apply all file meta-data from the previous file to the new file.
Markus Müller
+1  A: 

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

Ben Stiglitz
+1  A: 

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/

NSResponder
Nice link! I'd say that if all you want is compression, you could use the NSData category on CocoaDev ( http://www.cocoadev.com/index.pl?NSDataCategory ) and remove anything you don't need.
Joshua Nozzi