Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ZIP-archives to store NSDocument data

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.

like image 855
Markus Müller-Simhofer Avatar asked Nov 16 '09 09:11

Markus Müller-Simhofer


2 Answers

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.

like image 57
Joshua Nozzi Avatar answered Oct 12 '22 11:10

Joshua Nozzi


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

like image 36
Ben Stiglitz Avatar answered Oct 12 '22 11:10

Ben Stiglitz