Tag Archives: NSData

Compressing data

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:

  1. Add “libz.dylib” to your project’s frameworks.
  2. Unfortunately this library only comes with some low-level functions, e.g.inflateInit2 and deflate.  However, the good people at CocoaDev have created a category on NSData which hides all this.  I only copied the two short methods gzipInflate and 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.
  3. 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.