问题
I'm using +[NSData gtm_dataByGzippingData:] to gzip the UTF-8 encoded data returned by +[NSJSONSerialization dataWithJSONObject:options:error:]
.
How do I convert this gzipped NSData
to a string that I can send as a parameter to a Python server so that the server can decompress it with zlib.decompress()?
回答1:
Use a method like -[NSData base64EncodedString] to base64-encode the gzipped NSData
before you send it to the Python server.
Then, the Python server can base64-decode it and then unzip it like so:
contacts_data = zlib.decompress(base64.b64decode(contacts_base64), 16)
回答2:
As it says in the header file that you referenced in your question, deflate is NOT gzip. Gzip is a file structure, where as zlib is more of a compression stream.
First, I'd recommend that you change your code to use...
+ (NSData *)gtm_dataByDeflatingData:(NSData *)data;
So, now you have the compressed data. How are you sending it to the server? Are you pushing this over HTTP? Is it a custom service with a direct socket connection? Can you send 8-bit bytes, are are you restricted to sending 7-bit bytes? ...are you using NURLConnection? ...are you trying to upload the (compressed) data as a file via HTTP?
来源:https://stackoverflow.com/questions/10559971/objective-c-gzipped-nsdata-to-python-gzipped-string