Gzip 分块数据在 C++ 中分解
Gzip chunked data decompose in C++
我正在尝试对压缩数据块进行 Gzip 压缩,但在给定时间我并没有全部可用。我看到很多文件示例,但我无法根据需要进行转换。
我知道它应该在一个循环中完成,但为了简化问题,我尝试分为两个部分。
z_stream bid_strm;
bid_strm.zalloc = Z_NULL;
bid_strm.zfree = Z_NULL;
bid_strm.opaque = Z_NULL;
bid_strm.avail_in = 0;
bid_strm.next_in = Z_NULL;
bid_strm.total_in = 0;
if (inflateInit2(&bid_strm, 16) != Z_OK)
cout << " inflateInit failed" << endl;
size_t out_size = 64000;
unsigned char decompressed[64000];
//FIRST Chunk
u_char compressed[part1_comp.size()];
memcpy(compressed, part1_comp.c_str(), part1_comp.size());
size_t compressedSize = part1_comp.size();
bid_strm.avail_in = part1_comp.size();
bid_strm.next_in = compressed;
bid_strm.avail_out = out_size;
bid_strm.next_out = decompressed;
int ret = inflate(&bid_strm, Z_NO_FLUSH);
size_t have = out_size - bid_strm.avail_out;
cout << "HAVE: " << have << endl;
cout << "RET: " << ret << endl;
//SECOND Chunk
u_char compressed2[part2_comp.size()];
memcpy(compressed2, part2_comp.c_str(), part2_comp.size());
bid_strm.avail_in = part2_comp.size();
bid_strm.next_in = compressed2;
bid_strm.avail_out = out_size;
bid_strm.next_out = decompressed + have;
ret = inflate(&bid_strm, Z_NO_FLUSH);
have = out_size - bid_strm.avail_out;
cout << "HAVE: " << have << endl;
cout << "RET: " << ret << endl;
(void)inflateEnd(&bid_strm);
第一个块解压正确,但我找不到解压第二个块的方法。
收到 Z_DATA_ERROR(-3) 的 Return 值,但如果我将两个块设置在同一个数组中并同时膨胀,一切正常。
在文档中找到了解决方案。问题是 windowBits。
inflateInit2(&bid_strm, MAX_WBITS | 16)
解决了问题。
选择窗口位:
- deflate 格式:使用 wbits = -zlib.MAX_WBITS
- zlib 格式:使用 wbits = zlib.MAX_WBITS
- gzip 格式:使用 wbits = zlib.MAX_WBITS | 16
我正在尝试对压缩数据块进行 Gzip 压缩,但在给定时间我并没有全部可用。我看到很多文件示例,但我无法根据需要进行转换。
我知道它应该在一个循环中完成,但为了简化问题,我尝试分为两个部分。
z_stream bid_strm;
bid_strm.zalloc = Z_NULL;
bid_strm.zfree = Z_NULL;
bid_strm.opaque = Z_NULL;
bid_strm.avail_in = 0;
bid_strm.next_in = Z_NULL;
bid_strm.total_in = 0;
if (inflateInit2(&bid_strm, 16) != Z_OK)
cout << " inflateInit failed" << endl;
size_t out_size = 64000;
unsigned char decompressed[64000];
//FIRST Chunk
u_char compressed[part1_comp.size()];
memcpy(compressed, part1_comp.c_str(), part1_comp.size());
size_t compressedSize = part1_comp.size();
bid_strm.avail_in = part1_comp.size();
bid_strm.next_in = compressed;
bid_strm.avail_out = out_size;
bid_strm.next_out = decompressed;
int ret = inflate(&bid_strm, Z_NO_FLUSH);
size_t have = out_size - bid_strm.avail_out;
cout << "HAVE: " << have << endl;
cout << "RET: " << ret << endl;
//SECOND Chunk
u_char compressed2[part2_comp.size()];
memcpy(compressed2, part2_comp.c_str(), part2_comp.size());
bid_strm.avail_in = part2_comp.size();
bid_strm.next_in = compressed2;
bid_strm.avail_out = out_size;
bid_strm.next_out = decompressed + have;
ret = inflate(&bid_strm, Z_NO_FLUSH);
have = out_size - bid_strm.avail_out;
cout << "HAVE: " << have << endl;
cout << "RET: " << ret << endl;
(void)inflateEnd(&bid_strm);
第一个块解压正确,但我找不到解压第二个块的方法。 收到 Z_DATA_ERROR(-3) 的 Return 值,但如果我将两个块设置在同一个数组中并同时膨胀,一切正常。
在文档中找到了解决方案。问题是 windowBits。
inflateInit2(&bid_strm, MAX_WBITS | 16)
解决了问题。
选择窗口位:
- deflate 格式:使用 wbits = -zlib.MAX_WBITS
- zlib 格式:使用 wbits = zlib.MAX_WBITS
- gzip 格式:使用 wbits = zlib.MAX_WBITS | 16