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cKcw5TCr8OLVsKtw5nCrTXDtcKDwrlqwqlaw7fDl8K6LV/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;
}
}
小弟用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
解密后的字符串,应该是内存字节序列化后的字符串吧?你的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]; // 解压缩后的字符串
设置request的shouldCompressRequestBody为YES就可以进行压缩
解压缩
BOOL *dataWasCompressed = [request isResponseCompressed]; // 响应能否被gzip压缩过?
NSData *compressedResponse = [request rawResponseData]; // 压缩的数据
NSData *uncompressedData = [request responseData]; // 解压缩后的数据
NSString *response = [request responseString]; // 解压缩后的字符串