Decompressing a gzip data stream in Objective-C

2019-07-20 08:53发布

问题:

Is there a simple, standard way to decompress a gzip raw data buffer in Objective-C? I could not find anything useful in the Apple Developer documentation.

If not, can you point me to a code walk-through, a library, anything that will make my life easier? I am not afraid to write C code.

回答1:

There is a NSData category available at cocoadev that can handle zlib and gzip data: http://www.cocoadev.com/index.pl?NSDataCategory

If you just need gzip decompression, you can remove the zlib & hash related methods.
Don't forget to add a link libz.dylib (OS X standard library) build phase.

Update
As pointed out by marcos1490 in the comments, the original CocoaDev entry disappeared, but someone extended that NSData category and wrote about it: http://deusty.blogspot.de/2007/07/gzip-compressiondecompression.html



回答2:

You can try Ziparchive for decompression. I've tried compressing files using it, hope the other would work perfectly.

Here is the code snippet

ZipArchive *zip = [ZipArchive alloc];
[zip UnzipOpenFile:Zip-File-Path];
[zip UnzipFileTo:Unzip-Folder-Path overWrite:As-You-Wish];

Also include libz.dylib in build phase.