大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
1、首先说为什么采用zlib解压,因为zlib可以解压内存数据。
2、下载zlib库后,自己在projcts目录里面根据需要编译lib库
3、静态链接只需要zlib.h ;zconf.h;zlib.lib三个文件即可(动态的不介绍了)
4、解压函数:
- /* HTTP gzip decompress */
- int httpgzdecompress(Byte *zdata, uLong nzdata,
- Byte *data, uLong *ndata)
- {
- int err = 0;
- z_stream d_stream = { 0 }; /* decompression stream */
- static char dummy_head[2] =
- {
- 0x8 + 0x7 * 0x10,
- (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
- };
- d_stream.zalloc = (alloc_func)0;
- d_stream.zfree = (free_func)0;
- d_stream.opaque = (voidpf)0;
- d_stream.next_in = zdata;
- d_stream.avail_in = 0;
- d_stream.next_out = data;
- //if (inflateInit2(&d_stream, -MAX_WBITS) != Z_OK) return -1;
- if (inflateInit2(&d_stream, 47) != Z_OK) return -1;
- while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
- d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
- if ((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
- if (err != Z_OK)
- {
- if (err == Z_DATA_ERROR)
- {
- d_stream.next_in = (Bytef*)dummy_head;
- d_stream.avail_in = sizeof(dummy_head);
- if ((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK)
- {
- return -1;
- }
- }
- else return -1;
- }
- }
- if (inflateEnd(&d_stream) != Z_OK) return -1;
- *ndata = d_stream.total_out;
- return 0;
- }
/* HTTP gzip decompress */
int httpgzdecompress(Byte *zdata, uLong nzdata,
Byte *data, uLong *ndata)
{
int err = 0;
z_stream d_stream = { 0 }; /* decompression stream */
static char dummy_head[2] =
{
0x8 + 0x7 * 0x10,
(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
};
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
d_stream.next_in = zdata;
d_stream.avail_in = 0;
d_stream.next_out = data;
//if (inflateInit2(&d_stream, -MAX_WBITS) != Z_OK) return -1;
if (inflateInit2(&d_stream, 47) != Z_OK) return -1;
while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
if ((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
if (err != Z_OK)
{
if (err == Z_DATA_ERROR)
{
d_stream.next_in = (Bytef*)dummy_head;
d_stream.avail_in = sizeof(dummy_head);
if ((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK)
{
return -1;
}
}
else return -1;
}
}
if (inflateEnd(&d_stream) != Z_OK) return -1;
*ndata = d_stream.total_out;
return 0;
}
直接调用就OK了 注意传入的buffer数据和长度都要赋值
eg:
char* pDest = new char[1024 * 16];
uLong ulLength = 1024*16;
gzdecompress((Byte*)precvBuff, nCount, (Byte*)pDest, &ulLength);
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/196279.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...