IOS data compression and decompression

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/dkq972958298/article/details/53321804

In the actual development, we often have to compare the large data compression and then upload the server, the following is the compression method I used to compress the data in the project:

To compress the data:

Zipandunzip *zipand = [[Zipandunzip alloc] init];

NSString *str = @ "6666 or the German Embassy and the vulgar chemical and third party to restore the health of the very good design Harvard's number of the hair of the Sakai Phoenix Health is a very healthy sofa";
NSData *data = [str datausingencoding:nsutf8stringencoding];
NSData *datadeflate = [Zipand gzipdeflate:data];


Unzip the data:

NSData *datainflate = [Zipand gzipinflate:datadeflate];
NSString *astring = [[NSString alloc] initwithdata:datainflate encoding:nsutf8stringencoding];


The method of compression is:

    Compression-(NSData *) Gzipdeflate: (nsdata*) data {if ([data length] = = 0) return data;                    Z_stream STRM;          Strm.zalloc = Z_null;          Strm.zfree = Z_null;          Strm.opaque = Z_null;          strm.total_out = 0;          strm.next_in= (BYTEF *) [data bytes];                    strm.avail_in = (uInt) [data length];          Compresssion levels://z_no_compression//z_best_speed//Z_best_compression  Z_default_compression if (DeflateInit2 (&STRM, Z_default_compression, z_deflated, (15+16), 8,                    Z_default_strategy)! = Z_OK) return nil;  Nsmutabledata *compressed = [Nsmutabledata datawithlength:16384]; 16K chunks for expansion do {if (strm.total_out >= [compressed length                            ]) [Compressed increaselengthby:16384]; Strm.next_out = [Compressed mutablebytes] + strm.total_out;                            Strm.avail_out = (uInt) ([compressed length]-strm.total_out);                        Deflate (&STRM, z_finish);                    } while (strm.avail_out = = 0);                    Deflateend (&STRM);          [Compressed setLength:strm.total_out];      return [NSData datawithdata:compressed];   }

The Decompression method is:

    Extract-(NSData *) Gzipinflate: (nsdata*) data {if ([data length] = = 0) return data;          unsigned long full_length = [data length];                    unsigned long half_length = [data length]/2;          Nsmutabledata *decompressed = [Nsmutabledata datawithlength:full_length + half_length];          BOOL done = NO;                    int status;          Z_stream STRM;          strm.next_in = (BYTEF *) [data bytes];          strm.avail_in = (uInt) [data 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];   Strm.next_out = [decompressed mutablebytes] + strm.total_out;           Strm.avail_out = (uInt) ([decompressed length]-strm.total_out);              Inflate another 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;   }

This compression is essentially a binary compressed file that can be compressed for any binary file.

IOS data compression and 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.