I had occasion today to compress some data (in the form of an NSData object). Thanks to many people who have gone before me, this is fairly easy to do:
- Add “libz.dylib” to your project’s frameworks.
- Unfortunately this library only comes with some low-level functions, e.g.
deflate. However, the good people at CocoaDev have created a category on NSData which hides all this. I only copied the two short methods
gzipDeflate. I can’t work out their formatting – I copied the text from the View Source page, and had to go through it afterwards to re-insert asterisks and take out square brackets.
- These two functions work! I wrote out the data to a file using two path extensions, my original one and “gzip”, and this seems to work; the Finder recognises these files as compressed and does the right thing by them.