问题描述
我从这样的Web服务接收到一个字符串:
I received a string from a web service like this:
CAAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyJOp/PqTf6u/X8A1v85+wgAAAA=
源字符串为"EchoText
",并用Gzip
进行压缩,然后转换为Base64
.
The source string was "EchoText
" that compressed with Gzip
then converted to Base64
.
我必须先解码Base64
字符串,然后再使用Gzip
解码Unzip
.
I have to decode Base64
string at first then Unzip
that using Gzip
.
如何在Swift
(或Objective-C
)中执行操作?
How can I do in Swift
(or Objective-C
)?
我找到了一个使用Gzip的很好的库: nicklockwood/Gzip
I found a good library for using Gzip: nicklockwood/Gzip
这是我的代码:
NSString* base64String = @"CAAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyJOp/PqTf6u/X8A1v85+wgAAAA=";
NSData* compressedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSData* unCompressedData = [compressedData gunzippedData];
NSString *result = [[NSString alloc] initWithData:unCompressedData encoding:NSUTF8StringEncoding];
但结果为零.
推荐答案
此处的答案涵盖了Base64.
This answer here covers Base64.
zlib 可直接在Mac OS X和iOS上作为共享库使用.它可以解压缩gzip流.
zlib is available directly as a shared library on Mac OS X and iOS. It can decompress gzip streams.
请注意,在进入gzip流之前,必须删除示例的前四个字节.它们似乎包含小尾数形式的未压缩数据的大小. (在您的示例中为08 00 00 00
.)
Note that the first four bytes of your example have to be stripped before you get to the gzip stream. They appear to contain the size of the uncompressed data in little-endian form. (They are 08 00 00 00
in your example.)
这篇关于转换Base64 GZipped字符串Objective-c或Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!