提升iostream问题
我正在尝试使用以下代码解压缩 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遵循 brado86 的建议后,将
zlib_decompressor()
更改为gzip_decompressor()
After following brado86's advice, change
zlib_decompressor()
togzip_decompressor()
看起来您正在以错误的顺序推送过滤器。
据我从 Boost.Iostreams 文档中了解到的信息,对于输入,数据以与推送过滤器相反的顺序流过过滤器。所以如果您将以下几行更改如下,我认为它应该有效。
更改
为
欲了解更多信息,请阅读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
to
For more info, read Boost.Iostreams Documentation.