How to gzip NSData with zlib?

2019-05-27 05:02发布

问题:

  1. I want to use zlib because I'm assuming it's the best & fastest way to gzip NSData. But, is there a better way?

  2. If not, which version of the zlib library should I link to in Xcode: libz.dylib, libz.1.dylib, libz.1.1.3.dylib, or libz.1.2.5.dylib?

  3. Please provide a code example of how to us zlib to convert NSData *normalHTTPBody into NSData gzippedHTTPBody

回答1:

  1. Yes, zlib is what is used to gzip data. I know of no better way. As for speed, you can select the compression level to optimize speed vs. compression for your application.
  2. You will likely find that libz.dylib and libz.1.dylib are symbolic links to libz.1.2.5.dylib. You should be using libz.1.2.5.dylib as opposed to 1.1.3 due to many bug fixes and performance enhancements between the two.
  3. This link from your answer is a very well commented example. You can also look at this heavily commented example of zlib use (in C).


回答2:

3. Here are some solutions I found on the web:

  • http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMNSData%2Bzlib.m
  • http://www.cocoadev.com/index.pl?NSDataCategory
  • http://code.google.com/p/polkit/source/browse/trunk/Extensions/NSData%2BGZip.m?r=135
  • https://github.com/st3fan/cocoa-utils/blob/master/src/NSDataGZipAdditions.m
  • http://www.clintharris.net/2009/how-to-gzip-data-in-memory-using-objective-c/
  • http://deusty.blogspot.com/2007/07/gzip-compressiondecompression.html