iOS and server-side gzip compression issues

Source: Internet
Author: User
Tags create zip create zip file ziparchive

Yesterday did a day of gzip compression, tried three ways (Libz library, ziparchive,asihttprequest), did not succeed at first.
Theoretically, three of them are supposed to work, but I can't. When I try to find the third way, it's not my problem, it's the back-end problem (the Java-side output is a way to go).
Let's summarize today and write about how iOS and Java servers get compressed data.
One, client-server data compression decompression process (asihttprequest) client generated request,
Set the header to allow compression ("accept-encoding", "gzip"), which is to tell the server,
The client supports compression, any server that can be compressed, though come! The server receives this header,
If it supports compression, you can output data in a compressed manner,
Then write the response header ("content-encoding", "gzip")
1. Take ASIHTTPRequest as an example, the code is as follows:

nsurl* Requesturl = [Nsurl Urlwithstring:_listurl]; ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:requesturl];


The default is YES, you can set it to No to disable gzip compression [request Setallowcompressedresponse:yes];

  [Request Setdelegate:self];   

  

If it is an ordinary urlrequest, as long as: Request.setheader ("accept-encoding", "gzip");

2. Server-side return: Response.setheader ("content-encoding", "gzip");

3. Client response, also take asihttprequest as an example (this example transmits JSON data, I also use Sbjson parsing):

  -(void) requestfinished: (ASIHTTPRequest *) request{nsstring *jsonstring = @ "";        sbjsonparser* jsonparser = [[Sbjsonparser alloc] init];         Nsmutabledictionary *jsondictionary = nil; BOOL datawascompressed = [request isresponsecompressed];        Is the response compressed by gzip?                 if (datawascompressed) {NSData *uncompresseddata = [request ResponseData]; decompressed data nsstringencoding enc = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000                 );                 jsonstring = [[NSString alloc]initwithdata:uncompresseddata Encoding:enc];                 Jsondictionary = [Jsonparser objectwithstring:jsonstring error:nil];            [Jsonstring release];                 }else {jsonstring = [request responsestring];             Jsondictionary = [Jsonparser objectwithstring:jsonstring error:nil];                 } self._tabledict = Jsondictionary; [JsonparseR Release];                 [Self loadtabledict]; [Self release];  }

  

Attach a very detailed tutorial on ASIHTTPRequest request JSON data (no gzip related content):
http://ios-blog.co.uk/articles/tutorials/parsing-json-on-ios-with-asihttprequest-and-sbjson/
LIBZ Library LIBZ Library is an official library, it seems asihttprequest is also used in this library decompression, when we get compressed data data (the method is similar to the above,
Just get the normal data data response), you can use this method to extract the information, the decompression method is as follows (if it is only used under the current class, pass the data parameter in,
Then change self to Variable name):

#include @implementation NSData (dddata)-(NSData *) gzipinflate {if ([self length] = = 0) return self;          unsigned full_length = [self length];         unsigned half_length = [self length]/2; Nsmutabledata *decompressed = [Nsmutabledata datawithlength:full_length + half_length];          BOOL done = NO;          int status;          Z_stream STRM;          strm.next_in = (BYTEF *) [self bytes];          strm.avail_in = [self 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 = [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;  }

  

Attach a very detailed Libz library compression tutorial http://www.clintharris.net/2009/how-to-gzip-data-in-memory-using-objective-c/

and compression decompression tutorial (the code is copied from here): http://deusty.blogspot.com/2007/07/gzip-compressiondecompression.html

Ziparchive above is the memory compression and decompression, ziparchive mainly to the document processing. Yesterday, in the unsuccessful case of the above method, I took the data obtained
Save to file, and then processing, theoretically feasible, but because the server is wrong, the data obtained is not correct, so I do not unzip the success!!!!
Examples are as follows:

objective-c class used to Zip/unzip compressed zip file. Usage:add all the files to you project, and and framework libz.1.2.3.dylib. Include ZipArchive.h using #import "ziparchive/ziparchive.h" * Create zip file ziparchive* zipfile = [[Ziparchive alloc] I NIT]; [ZipFile createzipfile2:@ "Zipfilename"]; A OR [[ZipFile createzipfile2:@ "Zipfilename" password:@ "Your Password"];//if Password needed,//empty Password would ge  T same result as A [ZipFile addfiletozip:@ "FullPath of the file" newname:@ "new name of the file without path"];//.... Add Any number of the files here [ZipFile CloseZipFile2]; [ZipFile release];//Remember to release the object * Unzip compressed file ziparchive* ZipFile = [[Ziparchive alloc] Init ]; [ZipFile unzipopenfile:@ "ZIP file name"]; B (The zip got no password) OR [zipfile unzipopenfile:@ "ZIP file name" password:@ "password"]; [ZipFile unzipfileto:@ "Output path" overwrite:yes]; [ZipFile Unzipclosefile]; [ZipFile release]; 

  

iOS and server-side gzip compression issues

Related Article

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.