boost::iostreams::zlib::default_noheader 似乎被忽略

发布于 2024-09-10 01:17:55 字数 1339 浏览 7 评论 0原文

我无法让 boost::iostreams 的 zlib 过滤器忽略 gzip 标头...似乎将 zlib_param 的 default_noheader 设置为 true 然后调用 zlib_decompressor() 会产生“data_error”错误(标头检查不正确)。这告诉我 zlib 仍然期望找到标头。 有没有人获得 boost::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() ;它已成功解压......所以我很困惑为什么这不起作用。

提前致谢。

-冰

I'm having trouble getting boost::iostreams's zlib filter to ignore gzip headers ... It seems that setting zlib_param's default_noheader to true and then calling zlib_decompressor() produces the 'data_error' error (incorrect header check). This tells me zlib is still expecting to find headers.
Has anyone gotten boost::iostreams::zlib to decompress data without headers? I need to be able to read and decompress files/streams that do not have the two-byte header. Any assistance will be greatly appreciated.

Here's a modified version of the sample program provided by the boost::iostreams::zlib documentation:

#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;
}

I know my test data is not bad; I wrote a small program to call gzread() on the test file; it is successfully decompressed ... so I'm confused as to why this does not work.

Thanks in advance.

-Ice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唐婉 2024-09-17 01:17:55

我认为你想做的是这里描述的事情 是调整window bits参数。

例如

zlib_params p;
p.window_bits = 16 + MAX_WBITS;

in.push(zlib_decompressor(p));
in.push(ifs);

我认为 MAX_WBITS 是在 zlib.h 中定义的。

I think what you want to do is something that's described here which is to adjust the window bits parameter.

e.g

zlib_params p;
p.window_bits = 16 + MAX_WBITS;

in.push(zlib_decompressor(p));
in.push(ifs);

MAX_WBITS is defined in zlib.h I think.

梦断已成空 2024-09-17 01:17:55

很简单,试试这个:

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 );

Link with zlib and include zlib.h
您可以通过使用 fileno( stdout ) 使用 STDOUT 而不是文件

Much simple, try this:

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 );

Link with zlib and include zlib.h
You can use STDOUT instead of a file by using fileno( stdout )

哆兒滾 2024-09-17 01:17:55

只需使用 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);

Just use the boost::iostreams::gzip_decompressor for decompressing gzip files.

For example:

#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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文