IOS gzip Decompression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.