views:

7130

answers:

6

Is there a compression API available for use on the iPhone? We're building some RESTful web services for our iPhone app to talk to, but we want to compress at least some of the conversations for efficiency.

I don't care what the format (ZIP, LHA, whatever) is, and it does not need to be secure.

Some respondents have pointed out that the server can compress its output, and the iPhone can consume that. The scenario we have is exactly the reverse. We'll be POSTing compressed content to the web service. We're not concerned with compression going the other way.

+2  A: 

I believe zlib is available on the phone.

Ben Gottlieb
+4  A: 

zlib and bzip2 are available. And you can always add others, as long as they'll (generally) compile under OS X.

bzip2 is a better choice for smallest file sizes, but requires much more CPU power to compress and decompress.

Also, since you're talking to a web service, you may not have to do much. NSURLRequest accepts gzip encoding transparently in server responses.

Marco
Actually, bzip2 requires more memory, then a little more CPU, which is why I'd go with gzip/deflate (zlib).
Martin Plante
We're not worried about compression coming from the server. We're worried about compression going TO the server. I've edited my question to reflect this.
Gregory Higley
A: 

NSURL says it supports the gzip encoding so you shouldn't have to do anything more than have your RESTful web service return gzip encoded content when appropriate. All the decoding will be done under the covers.

carson
Problem is, we need to encode our POSTs, not our GETs.
Gregory Higley
+11  A: 

If you store the data for the conversations in an NSData object, the folks at the CocoaDev wiki have posted an NSData category that adds gzip and zlib compression / decompression as simple methods. These have worked well for me in my iPhone application.

Brad Larson
Awesome! Thanks!
Gregory Higley
+2  A: 

If you have only compressed data and know uncompressed size you can use:

#import "zlib.h"


int datal = [zipedData length];
Bytef *buffer[uncompressedSize];
Bytef *dataa[datal];

[zipedData getBytes:dataa];

Long *ld;

uLong sl = datal;
*ld = uncompressedSize;
if(uncompress(buffer, ld, dataa, sl) == Z_OK)
{
NSData *uncompressedData = [NSData dataWithBytes:buffer length:uncompressedSize];
NSString *txtFile = [[NSString alloc] initWithData:uncompressedData encoding:NSUTF8StringEncoding];
}
Yogan