我正在使用boost
的gzip_compressor
和'gzip_decompressor'来压缩某些字符串。
我正在像这样压缩字符串
string compressedString;
{
filtering_ostream out;
out.push(gzip_compressor(gzip_params(gzip::best_compression)));
out.push(boost::iostreams::back_inserter(compressedString));
out.write(stringToBeCompressed.c_str(), stringToBeCompressed.size());
}
它工作正常,但我无法解压缩。这是我的代码
std::string decompressedString;
{
filtering_ostream out;
out.push(gzip_decompressor());
out.push(boost::iostreams::back_inserter(decompressedString));
out.write(compressedString.c_str(), compressedString.size());
}
我究竟做错了什么?
先感谢您!
最佳答案
我找到了解决此问题的方法。这是工作代码。
压缩部分
std::string compressedString;
{
filtering_ostream compressingStream;
compressingStream.push(boost::iostreams::gzip_compressor(gzip_params(gzip::best_compression)));
compressingStream.push(boost::iostreams::back_inserter(compressedString));
compressingStream << stringToBeCompressed;
boost::iostreams::close(compressingStream);
}
解压部分
std::string decompressedString;
{
boost::iostreams::filtering_ostream decompressingStream;
decompressingStream.push(boost::iostreams::gzip_decompressor());
decompressingStream.push(boost::iostreams::back_inserter(decompressedString));
decompressingStream << stringToBeDecompressed;
boost::iostreams::close(decompressingStream);
}