2

我正在尝试使用以下代码在 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);

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

4

2 回答 2

2

按照 brado86 的建议后,更改zlib_decompressor()gzip_decompressor()

于 2013-02-17T05:39:14.283 回答
1

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

根据我从 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 文档

于 2011-03-22T04:48:10.237 回答