Code Bye

高手在否-iOS gzip无法解压缩

服务端请求到的xml是(服务端是用java写的):<ns1:out>H8KLCAAAAAAAAADCrcOUTUsCQRgHw7DDr8KyZ8KJeXnDpsONw5vCksKIwrAeRDsoESHCtcKEwqAmaQcRL8KVwpHCh8KowqBTeSo6wrYRRMOSC8O1ZcOaXcO8FsKNSMKQMGMsecKcZwZ+PA/Dv2d6w47CjsOfccOrw7XDlcO9dmfCt8Ohw6/CtcKdw7R6w4/DmcO6OW3DlsK2wp3CtMODJUdMAVDChcKcw5TCr8OLVsKtw5nCrTXDtcKDwrlqwqlaw7fDl8K6LV/Dl8Opw5xFwrPDmsKYFjEmwoRSAMKnwp/CsksSJ8KTwrBJYkAJMGrCkQAxQcKAc0tPwq7DqybDgsKmw68NDmPClGPCgsKkw4AWJ8KTw7dyNsKIwpjCoMKvwrfDm8O4w7w4DG7CosKTwrHCsTPChcO0w7zCgHHCoBbCscKYwqjCscOww6E9HMOcMcKCw4wYwpYcwpjDokDClsKKETNGwrjCkkoyDEvDhSg2aQJjRQTDksOXcsKJGl/DgcOmPArCqTDCgMKewqPCsGleLlkiZ8OgLCh/ZMOTwrJtwoXCrD3Cm8OGw53CjsKGL8Ohw5nDhcKCbErCoCApEsOCIlYqw6VMw4J9wohGw4PDiUEQB8KPw7HDlcORTDbChwcoV8KMcMKmwpjCmcO2Cl7CtlhONMOhw4lgHD19TMORw5FpfHgfwr9+LhwzwrIEKV/CssK3bHbCr8Kfw4PDi8OAPmYBWMOpUcOrL8OOw7IFw7xLw5TDm8Kyw5HDvwbDisOuw6tWOQYAAA==</ns1:out>
小弟用xml解析后得到:H8KLCAAAAAAAAADCrcOUTUsCQRgHw7DDr8KyZ8KJeXnDpsONw5vCksKIwrAeRDsoESHCtcKEwqAmaQcRL8KVwpHCh8KowqBTeSo6wrYRRMOSC8O1ZcOaXcO8FsKNSMKQMGMsecKcZwZ+PA/Dv2d6w47CjsOfccOrw7XDlcO9dmfCt8Ohw6/CtcKdw7R6w4/DmcO6OW3DlsK2wp3CtMODJUdMAVDChcKcw5TCr8OLVsKtw5nCrTXDtcKDwrlqwqlaw7fDl8K6LV/Dl8Opw5xFwrPDmsKYFjEmwoRSAMKnwp/CsksSJ8KTwrBJYkAJMGrCkQAxQcKAc0tPwq7DqybDgsKmw68NDmPClGPCgsKkw4AWJ8KTw7dyNsKIwpjCoMKvwrfDm8O4w7w4DG7CosKTwrHCsTPChcO0w7zCgHHCoBbCscKYwqjCscOww6E9HMOcMcKCw4wYwpYcwpjDokDClsKKETNGwrjCkkoyDEvDhSg2aQJjRQTDksOXcsKJGl/DgcOmPArCqTDCgMKewqPCsGleLlkiZ8OgLCh/ZMOTwrJtwoXCrD3Cm8OGw53CjsKGL8Ohw5nDhcKCbErCoCApEsOCIlYqw6VMw4J9wohGw4PDiUEQB8KPw7HDlcORTDbChwcoV8KMcMKmwpjCmcO2Cl7CtlhONMOhw4lgHD19TMORw5FpfHgfwr9+LhwzwrIEKV/CssK3bHbCr8Kfw4PDi8OAPmYBWMOpUcOrL8OOw7IFw7xLw5TDm8Kyw5HDvwbDisOuw6tWOQYAAA==
在用base64 解密得到:<1fc28b08 00000000 000000c2 adc3944d 4b024118 07c3b0c3 afc2b267 c2897979 c3a6c38d c39bc292 c288c2b0 1e443b28 1121c2b5 c284c2a0 26690711 2fc295c2 91c287c2 a8c2a053 792a3ac2 b61144c3 920bc3b5 65c39a5d c3bc16c2 8d48c290 30632c79 c29c6706 7e3c0fc3 bf677ac3 8ec28ec3 9f71c3ab 。
然后就是用gzip解压缩了代码如下:
————————————问题来了 ————————————————————完美分割————————————————————————————求关注——————————————————————————
-(NSData *)uncompressZippedData:(NSData *)compressedData  {
if ([compressedData length] == 0) return compressedData;
unsigned full_length = [compressedData length];
unsigned half_length = [compressedData length] / 2;
NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
BOOL done = NO;
int status;
z_stream strm;
strm.next_in = (Bytef *)[compressedData bytes];
strm.avail_in = [compressedData length];
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
if (inflateInit2(&strm, (15+32)) != Z_OK) return nil;
while (!done) {
// Make sure we have enough room and reset the lengths.
if (strm.total_out >= [decompressed length]) {
[decompressed increaseLengthBy: half_length];
}
strm.next_out = [decompressed mutableBytes] + strm.total_out;
strm.avail_out = [decompressed length] – strm.total_out;
// Inflate another chunk.
  status = inflate (&strm, Z_SYNC_FLUSH);
//这里 一直返回-3 errordata  ,旧是这问题了,怎么弄啊
if (status == Z_STREAM_END) {
done = YES;
} else if (status != Z_OK) {
break;
}
}
if (inflateEnd (&strm) != Z_OK) return nil;
// Set real length.
if (done) {
[decompressed setLength: strm.total_out];
return [NSData dataWithData: decompressed];
} else {
return nil;
}
}
解决方案

2

网上有很多 第三方的解压库,很简单的。

50

引用:

有没有人帮看下,附上base解密后的完整字符窜 编码格式utf-8
<1fc28b08 00000000 000000c2 adc3944d 4b024118 07c3b0c3 afc2b267 c2897979 c3a6c38d c39bc292 c288c2b0 1e443b28 1121c2b5 c284c2a0 26690711 2fc295c2 91c287c2 a8c2a053 792a3ac2 b61144c3 920bc3b5 65c39a5d c3bc16c2 8d48c290 30632c79 c29c6706 7e3c0fc3 bf677ac3 8ec28ec3 9f71c3ab c3b5c395 c3bd7667 c2b7c3a1 c3afc2b5 c29dc3b4 7ac38fc3 99c3ba39 6dc396c2 b6c29dc2 b4c38325 474c0150 c285c29c c394c2af c38b56c2 adc399c2 ad35c3b5 c283c2b9 6ac2a95a c3b7c397 c2ba2d5f c397c3a9 c39c45c2 b3c39ac2 98163126 c2845200 c2a7c29f c2b24b12 27c293c2 b0496240 09306ac2 91003141 c280734b 4fc2aec3 ab26c382 c2a6c3af 0d0e63c2 9463c282 c2a4c380 1627c293 c3b77236 c288c298 c2a0c2af c2b7c39b c3b8c3bc 380c6ec2 a2c293c2 b1c2b133 c285c3b4 c3bcc280 71c2a016 c2b1c298 c2a8c2b1 c3b0c3a1 3d1cc39c 31c282c3 8c18c296 1cc298c3 a240c296 c28a1133 46c2b8c2 924a320c 4bc38528 36690263 4504c392 c39772c2 891a5fc3 81c3a63c 0ac2a930 c280c29e c2a3c2b0 695e2e59 2267c3a0 2c287f64 c393c2b2 6dc285c2 ac3dc29b c386c39d c28ec286 2fc3a1c3 99c385c2 826c4ac2 a0202912 c3822256 2ac3a54c c3827dc2 8846c383 c3894110 07c28fc3 b1c395c3 914c36c2 87072857 c28c70c2 a6c298c2 99c3b60a 5ec2b658 4e34c3a1 c389601c 3d7d4cc3 91c39169 7c781fc2 bf7e2e1c 33c2b204 295fc2b2 c2b76c76 c2afc29f c383c38b c3803e66 0158c3a9 51c3ab2f c38ec3b2 05c3bc4b c394c39b c2b2c391 c3bf06c3 8ac3aec3 ab563906 0000>

解密后的字符串,应该是内存字节序列化后的字符串吧?你的NSData里存储的需要是对应的字节信息,而不是这个字符串…不知道你的(NSData *)compressedData 是怎么填充的。

28

ASI带GZIP压缩的方法
设置request的shouldCompressRequestBody为YES就可以进行压缩
解压缩
BOOL *dataWasCompressed = [request isResponseCompressed]; // 响应能否被gzip压缩过?
NSData *compressedResponse = [request rawResponseData]; // 压缩的数据
NSData *uncompressedData = [request responseData]; // 解压缩后的数据
NSString *response = [request responseString]; // 解压缩后的字符串

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明高手在否-iOS gzip无法解压缩