Objective-C gzipped NSData to Python gzipped string?

一曲冷凌霜 提交于 2019-12-11 09:46:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!