IPhone and GZip

Now I know that xCode automatically does the GZip decomposition for you inside:

NSData *data = [NSData dataWithContentsOfURL:URL]; 

And it works if I point to the Gzip file on my server. But since my content is dynamic, I have a PHP script, and then create a gzip file like this:

 $zp = gzopen($file, "r"); $data = gzread($zp, $filesize); gzclose($zp); 

I encode my data with:

 echo gzencode($data, 9); 

With this, I add the following headers:

 header("Content-Type: application/x-gzip"); header("Content-Encoding: gzip"); header("Accepts-Encoding: gzip"); 

When I look at the URL, my browser wants to download the file automatically, and I can unzip it on my Mac and view its contents. However, when I try to read it through xCode, it will not work.

 NSData *data = [NSData dataWithContentsOfURL:URL]; NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog (content); //returns only data when pointed directly to a Gzip file 

Did I forget something?

+6
iphone gzip nsdata
source share
1 answer

If you download something with an application like content / x-gzip, the URL loading system will not unpack it for you. I think the data you received is still gzip encoded.

You can use my NSData add-ons to handle this. See http://github.com/st3fan/cocoa-utils/blob/master/src/NSDataGZipAdditions.m

+25
source share

All Articles