I received a string from a web service like this:
CAAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyJOp/PqTf6u/X8A1v85+wgAAAA=
The source string was "EchoText
" that compressed with Gzip
then converted to Base64
.
I have to decode Base64
string at first then Unzip
that using Gzip
.
How can I do in Swift
(or Objective-C
)?
Edit:
I found a good library for using Gzip: nicklockwood/Gzip
and it's my code:
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];
but the result is nil.
This answer here covers Base64.
zlib is available directly as a shared library on Mac OS X and iOS. It can decompress gzip streams.
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.)