Boost :: iostreams :: zlib :: default_noheader似乎被忽略了
-
15-09-2020 - |
题
我遇到了麻烦:: iostreams的zlib过滤器忽略gzip标题......似乎将zlib_param的default_noheader设置为true,然后调用zlib_decompressor()生成'data_error'错误(不正确的标题检查)。这告诉我zlib仍然期望找到标题。 有没有人得到推动:: iostreams :: zlib要没有标题的数据解压缩数据?我需要能够读取和解压缩没有双字节标题的文件/流。任何援助都将非常感谢。
这是Boost :: iostreams :: zlib文档提供的示例程序的修改版本:
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
int main(int argc, char** argv)
{
using namespace std;
using namespace boost::iostreams;
ifstream ifs(argv[1]);
ofstream ofs("out");
boost::iostreams::filtering_istreambuf in;
zlib_params p(
zlib::default_compression,
zlib::deflated,
zlib::default_window_bits,
zlib::default_mem_level,
zlib::default_strategy,
true
);
try
{
in.push(zlib_decompressor(p));
in.push(ifs);
boost::iostreams::copy(in, ofs);
ofs.close();
ifs.close();
}
catch(zlib_error& e)
{
cout << "zlib_error num: " << e.error() << endl;
}
return 0;
}
.
我知道我的测试数据不坏;我写了一个小程序来调用测试文件上的gzread();它成功地压缩......所以我很困惑为什么这不起作用。
提前感谢。
-ice
解决方案
我想你想做什么是描述的东西这里,它是调整window bits
参数。
e.g
zlib_params p;
p.window_bits = 16 + MAX_WBITS;
in.push(zlib_decompressor(p));
in.push(ifs);
.
MAX_WBITS
在zlib.h中定义。我认为。
其他提示
很简单,试试这个:
FILE* fp = fopen("abc.gz", "w+");
int dupfd = dup( fileno( fp ) );
int zfp = gzdopen( dupfd, "ab" )
gzwrite( zfp, YOUR_DATA, YOUR_DATA_LEN );
gzclose( zfp );
fclose( fp );
.
与zlib链接,包括zlib.h 您可以使用fileno(stdout)
使用stdout而不是文件。
刚使用
boost::iostreams::gzip_decompressor
用于解压缩gzip文件。
例如:
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
// ...
boost::iostreams::filtering_istream stream;
stream.push(boost::iostreams::gzip_decompressor());
ifstream file(filename, std::ios_base::in | std::ios_base::binary);
stream.push(file);
. 不隶属于 StackOverflow