使用GZIP或deflate在objective-c(iphone)中压缩/解压缩NSString [英] Compress/Decompress NSString in objective-c (iphone) using GZIP or deflate

查看:201
本文介绍了使用GZIP或deflate在objective-c(iphone)中压缩/解压缩NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Azure上运行了一个Web服务,它返回我在iPhone应用程序中使用的JSON。

I have a web-service running on Windows Azure which returns JSON that I consume in my iPhone app.

不幸的是,Windows Azure似乎不支持压缩动态响应(长篇故事)所以我决定通过返回一个未压缩的JSON包来解决它,它包含一个压缩的(使用GZIP)字符串。

Unfortunately, Windows Azure doesn't seem to support the compression of dynamic responses yet (long story) so I decided to get around it by returning an uncompressed JSON package, which contains a compressed (using GZIP) string.

例如

{"Error":null,"IsCompressed":true,"Success":true,"Value":"vWsAAB+LCAAAAAAAB..etc.."}

...其中value是以JSON表示的复杂对象的压缩字符串。

... where value is the compressed string of a complex object represented in JSON.

这很容易在服务器上实现,但是在生命周期中我无法弄清楚如何将压缩的NSS​​tring解压缩为未压缩的NSS​​tring,我可以为zlib等找到的所有示例都处理文件等。

This was really easy to implement on the server, but for the life of me I can't figure out how to decompress a gzipped NSString into an uncompressed NSString, all the examples I can find for zlib etc are dealing with files etc.

任何人都可以给我任何关于如何做到这一点的线索? (我也很乐意使用deflate的解决方案,因为我可以更改服务器端实现以使用deflate)。

Can anyone give me any clues on how to do this? (I'd also be happy for a solution that used deflate as I could change the server-side implementation to use deflate too).

谢谢!!

史蒂文

编辑1: Aaah,我看到ASIHTTPRequest正在使用以下函数源代码:

Edit 1: Aaah, I see that ASIHTTPRequest is using the following function in it's source code:

//uncompress gzipped data with zlib
+ (NSData *)uncompressZippedData:(NSData*)compressedData;

...我知道我可以将NSString转换为NSData,所以我会看到如果这导致我到任何地方!

... and I'm aware that I can convert NSString to NSData, so I'll see if this leads me anywhere!

编辑2:不幸的是,编辑1中描述的方法并没有把我带到任何地方。

Edit 2: Unfortunately, the method described in Edit 1 didn't lead me anywhere.

编辑3:按照以下有关base64编码/解码的建议,我想出了以下代码。您可以猜测编码的GzippedString是一个字符串Hello,我的名字是Steven Elliott,它被gzip压缩然后转换为base64字符串。不幸的是,打印出一幅使用的NSLog结果却空白。

Edit 3: Following the advice below regarding base64 encoding/decoding, I came up with the following code. The encodedGzippedString is as you can guess, a string "Hello, my name is Steven Elliott" which is gzipped and then converted to a base64 string. Unfortunately, the result that prints using NSLog is just blank.

NSString *encodedGzippedString = @"GgAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyK+uE6X2SJPiyZ93eaX+TI9Lcuiatvx/wOwYc0HGgAAAA==";
NSData *decodedGzippedData = [NSData dataFromBase64String:encodedGzippedString];
NSData* unGzippedJsonData = [ASIHTTPRequest uncompressZippedData:decodedGzippedData];   
NSString* unGzippedJsonString = [[NSString alloc] initWithData:unGzippedJsonData encoding:NSASCIIStringEncoding];       
NSLog(@"Result: %@", unGzippedJsonString);  


推荐答案

经过这么久,我终于找到了一个解决方案这个问题!

After all this time, I finally found a solution to this problem!

上面的答案都没有对我有所帮助,因为他们看起来都很有希望。最后,我能够使用chilkat框架为.net解压缩服务器上的字符串...然后使用chilkat框架在iOS上解压缩它(尚未发布,但如果您通过电子邮件发送,则可用直接)。

None of the answers above helped me, as promising as they all looked. In the end, I was able to compress the string on the server with gzip using the chilkat framework for .net ... and then decompress it on the iphone using the chilkat framework for iOS (not yet released, but available if you email the guy directly).

chilkat框架让这个非常容易对开发人员大加赞赏!

The chilkat framework made this super easy to do so big thumbs up to the developer!

这篇关于使用GZIP或deflate在objective-c(iphone)中压缩/解压缩NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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