title: Compress/Decompress NSString in objective-c (iphone) using GZIP or deflate
body: <p>I have a web-service running on Windows Azure which returns JSON that I consume in my iPhone app.</p>
  
  <p>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. </p>
  
  <p>e.g</p>
  
  <pre><code>{"Error":null,"IsCompressed":true,"Success":true,"Value":"vWsAAB+LCAAAAAAAB..etc.."}
  </code></pre>
  
  <p>... where value is the compressed string of a complex object represented in JSON.</p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>Thanks!!</p>
  
  <p>Steven</p>
  
  <p><b>Edit 1:</b> Aaah, I see that ASIHTTPRequest is using the following function in it's source code:</p>
  
  <pre><code>//uncompress gzipped data with zlib
  + (NSData *)uncompressZippedData:(NSData*)compressedData;
  </code></pre>
  
  <p>... and I'm aware that I can convert NSString to NSData, so I'll see if this leads me anywhere!</p>
  
  <p><b>Edit 2:</b> Unfortunately, the method described in Edit 1 didn't lead me anywhere.</p>
  
  <p><b>Edit 3:</b> 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.</p>
  
  <pre><code>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);  
  </code></pre>