iPhone和GZip [英] iPhone and GZip

查看:225
本文介绍了iPhone和GZip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我知道xCode会自动为你做GZip decrompession:

Now I know that xCode automaticly does the GZip decrompession for you within:

NSData *data = [NSData dataWithContentsOfURL:URL];

如果我指向服务器上的Gzip文件,它确实有效。但由于我的内容是动态的,我有一个PHP脚本,而不是像这样创建一个gzip文件:

And it does work if I point to a Gzip file on my server. But since my content is dynamic, I have a PHP script that rather then create a gzip file like so:

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

我用以下代码编码自己的数据:

I encode my own 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");

当我浏览到URL时,我的浏览器想要自动下载文件并且我能够解压缩它在我的Mac上,并查看它的内容。但是,当我尝试通过xCode读取它时,它将无法工作。

When I browse to the URL, my browser wants to download the file automatically and I am able to unzip it on my Mac and view it's content. However when I try to read it through xCode it won't 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

我忘了什么?

推荐答案

如果您下载内容类型为application / x-gzip的内容,则网址加载系统不会为您解压缩。我认为您收到的数据仍然是gzip编码。

If you download something with content type application/x-gzip, the url loading system will not decompress it for you. I think the data that you received is still gzip encoded.

您可以使用我的NSData添加来处理此问题。请参见 http://github.com/st3fan/cocoa-utils/ blob / master / src / NSDataGZipAdditions.m

You can use my NSData additions to deal with this. See http://github.com/st3fan/cocoa-utils/blob/master/src/NSDataGZipAdditions.m

这篇关于iPhone和GZip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆