boost gzip_compressor和gzip_decompressor

时间:2013-11-20 15:30:29

标签: c++ boost gzip

我正在使用boost的{​​{1}}和'gzip_decompressor'来压缩某些字符串。 我正在压缩像这样的字符串

gzip_compressor

它工作正常,但我不能解压缩它。这是我的代码

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());
}

我做错了什么?

提前谢谢!

1 个答案:

答案 0 :(得分:5)

我找到了解决这个问题的方法。这是工作代码。

压缩部分

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);
}