我正在尝试使用以下代码在 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);
但是,异常仍然发生,除了它现在发生在副本上