有人可以告诉我应该使用哪个函数来解压已使用vb.net的gzipstream压缩的字节数组吗?我想使用zlib。
我已经包含了zlib.h文件,但是我还没有找出应该使用哪个函数。
有人可以告诉我应该使用哪个函数来解压已使用vb.net的gzipstream压缩的字节数组吗?我想使用zlib。
我已经包含了zlib.h文件,但是我还没有找出应该使用哪个函数。
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
std::ifstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
file.open(filename, std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_stream<boost::iostreams::input> decompressor;
decompressor.push(boost::iostreams::gzip_decompressor());
decompressor.push(file);
然后逐行解压缩:
for(std::string line; getline(decompressor, line);) {
// decompressed a line
}
或将整个文件转换为数组:
std::vector<char> data(
std::istreambuf_iterator<char>(decompressor)
, std::istreambuf_iterator<char>()
);
:
int gzip_inflate(char *compr, int comprLen, char *uncompr, int uncomprLen)
{
int err;
z_stream d_stream; /* decompression stream */
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
d_stream.next_in = (unsigned char *)compr;
d_stream.avail_in = comprLen;
d_stream.next_out = (unsigned char *)uncompr;
d_stream.avail_out = uncomprLen;
err = inflateInit2(&d_stream, 16+MAX_WBITS);
if (err != Z_OK) return err;
while (err != Z_STREAM_END) err = inflate(&d_stream, Z_NO_FLUSH);
err = inflateEnd(&d_stream);
return err;
}
uncompr
和uncomprLen
是否在gzip_inflate
方法中预先知道或修改?如何预先知道未压缩流的长度?谢谢。 - user7256215您需要使用inflateInit2()
来请求gzip解码。请阅读zlib.h中的文档。
zlib发行版中有很多示例代码。还可以查看这个详细记录的zlib使用示例。您可以修改它,以使用inflateInit2()
而不是inflateInit()
。
char *
转换为正确的流以调用inflateInit2()
?并且如何将输出作为char *
获取?难道没有任何简单的示例代码吗? - Lotzki
char *
中的字节数组来实现这个? - Lotzkivector
,请查看http://en.cppreference.com/w/cpp/container/vector - Maxim Egorushkin