提升iostream问题

发布于 2024-10-25 20:40:15 字数 1755 浏览 1 评论 0原文

我正在尝试使用以下代码解压缩 boost 中的 gzip 字符串

std::string DecompressString(const std::string &compressedString)
{
    std::stringstream src(compressedString);
    if (src.good())
    {
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
    std::stringstream dst;
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
    in.push(boost::iostreams::zlib_decompressor());

    boost::iostreams::copy(in, out);
    return dst.str();
    }
    return "";

}

,但是,每当我调用此函数(如下所示)时,

string result = DecompressString("H4sIA");
string result = DecompressString("H4sIAAAAAAAAAO2YMQ6DMAxFfZnCXOgK9AA9ACsURuj9N2wpkSIDootxhv+lN2V5sqLIP0T55cEUgdLR48lUgToTjw/5zaRhBuVSKO5yE5c2kDp5zunIaWG6mz3SxLvjeX/hAQ94wAMe8IAHPCwyMS9mdvYYmTfzdfSQ/rQGjx/t92A578l+T057y1Ff6NW51Uy0h+zkLZ33ByuPtB8IuhdcnSMIglgm/r15/rtJctlf4puMt/i/bN16EotQFgAA");

程序总是会在这一行失败

in.push(boost::iostreams::zlib_decompressor());

并生成以下异常,

Unhandled exception at 0x7627b727 in KHMP.exe: Microsoft C++ exception: 
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::logic_error> > at memory location 0x004dd868..

我真的不知道关于这个...有人有什么建议吗?

谢谢

编辑:

根据建议,我将代码切换到

 boost::iostreams::filtering_streambuf<boost::iostreams::input> in;//(src);

    in.push(boost::iostreams::zlib_decompressor());
    in.push(src);
     std::stringstream dst;
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out;//(dst);
    out.push(dst);
    boost::iostreams::copy(in, out);

但是,异常仍然发生,除了现在发生在副本上

I'm trying to decompress a gzip'd string inside boost using the following code

std::string DecompressString(const std::string &compressedString)
{
    std::stringstream src(compressedString);
    if (src.good())
    {
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
    std::stringstream dst;
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
    in.push(boost::iostreams::zlib_decompressor());

    boost::iostreams::copy(in, out);
    return dst.str();
    }
    return "";

}

however, whenever I make a call to this functions (such as below)

string result = DecompressString("H4sIA");
string result = DecompressString("H4sIAAAAAAAAAO2YMQ6DMAxFfZnCXOgK9AA9ACsURuj9N2wpkSIDootxhv+lN2V5sqLIP0T55cEUgdLR48lUgToTjw/5zaRhBuVSKO5yE5c2kDp5zunIaWG6mz3SxLvjeX/hAQ94wAMe8IAHPCwyMS9mdvYYmTfzdfSQ/rQGjx/t92A578l+T057y1Ff6NW51Uy0h+zkLZ33ByuPtB8IuhdcnSMIglgm/r15/rtJctlf4puMt/i/bN16EotQFgAA");

the program will always fail on this line

in.push(boost::iostreams::zlib_decompressor());

and generate the following exception

Unhandled exception at 0x7627b727 in KHMP.exe: Microsoft C++ exception: 
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::logic_error> > at memory location 0x004dd868..

I really have no idea on this one... anyone have any suggestions?

Thanks

EDIT:

Following a suggestion, I switch the code to

 boost::iostreams::filtering_streambuf<boost::iostreams::input> in;//(src);

    in.push(boost::iostreams::zlib_decompressor());
    in.push(src);
     std::stringstream dst;
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out;//(dst);
    out.push(dst);
    boost::iostreams::copy(in, out);

however, the exception still happens, except it now happens on copy

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

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

发布评论

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

评论(2

痴意少年 2024-11-01 20:40:15

遵循 brado86 的建议后,将 zlib_decompressor() 更改为 gzip_decompressor()

After following brado86's advice, change zlib_decompressor() to gzip_decompressor()

无可置疑 2024-11-01 20:40:15

看起来您正在以错误的顺序推送过滤器。

据我从 Boost.Iostreams 文档中了解到的信息,对于输入,数据以与推送过滤器相反的顺序流过过滤器。所以如果您将以下几行更改如下,我认为它应该有效。

更改

boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
in.push(boost::iostreams::zlib_decompressor());

boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(src);        // Note the order of pushing filters into the instream.
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);

欲了解更多信息,请阅读Boost.Iostreams文档。

Looks like you are pushing your filters in the wrong order for in.

From what I can understand from the Boost.Iostreams documentations, for input, data flows through the filters in the reverse order in which you've pushed the filters in. So if you change the following lines as follows, I think it should work.

Change

boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
in.push(boost::iostreams::zlib_decompressor());

to

boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(src);        // Note the order of pushing filters into the instream.
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);

For more info, read Boost.Iostreams Documentation.

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