1. Import Libz Library (for example: Libz 1.2.5.dylib)
2. Introduction of header Files
#import "Zlib.h"
3. Implement decompression (input and output are NSData objects)
-(NSData *) Uncompresszippeddata: (NSData *) compresseddata{if ([compresseddata length] = = 0) return compresseddata; unsigned full_length = [compresseddata length]; unsigned half_length = [Compresseddata length]/2; Nsmutabledata *decompressed = [Nsmutabledata datawithlength:full_length + half_length]; BOOL done = NO; int status; Z_stream STRM; strm.next_in = (BYTEF *) [compresseddata bytes]; strm.avail_in = [Compresseddata length]; strm.total_out = 0; Strm.zalloc = Z_null; Strm.zfree = Z_null; if (InflateInit2 (&STRM, (15+32)) = Z_OK) return nil; while (!done) {//Do sure we have enough and reset the lengths. if (strm.total_out >= [decompressed length]) {[decompressed increaselengthby:half_length]; }//Chadeltu plus (BYTEF *) Strm.next_out = (BYTEF *) [decompressed mutablebytes] + strm.total_out; Strm.avail_out = [decompressed length]-strm.total_out; InflateAnother chunk. Status = Inflate (&STRM, Z_sync_flush); if (status = = Z_stream_end) {done = YES; } else if (status! = Z_OK) {break; }} if (Inflateend (&STRM)! = Z_OK) return nil; Set real length. if (done) {[decompressed setLength:strm.total_out]; return [NSData datawithdata:decompressed]; } else {return nil; }}
Use the sample:
Gzip Unzip, Mapfilepath for the file path to be unzipped nsdata *datainitial = [NSData datawithcontentsoffile:mapfilepath];//before extracting NSData * datauncompressed = [Self uncompresszippeddata:datainitial];//after decompression
Reference blog:
Extracting gzip in iphone development
IOS gzip Decompression