c++ - 使用boost将gzip压缩/解压缩到内存中

标签 c++ boost gzip boost-iostreams

我会很简短:我有这段代码:

QByteArray MyNBT::decompressData(QByteArray data)
{
    filtering_streambuf<input> in;

    std::string _data = data.data();

    in.push( gzip_decompressor() );
    in.push( boost::iostreams::back_inserter(_data) );
    //in.push( std::back_inserter(_data) );

    std::stringstream _sstream;
    boost::iostreams::copy(in, _sstream);

    QByteArray out = _sstream.rdbuf()->str().c_str();

    return out;
}

它在这一行给出错误:

in.push( boost::iostreams::back_inserter(_data) );
//in.push( std::back_inserter(_data) );

错误是:

/usr/include/boost/iostreams/chain.hpp:244: error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>'
     BOOST_STATIC_ASSERT((is_convertible<category, Mode>::value));
     ^

编译器在 std::back_inserter(_data) 中抛出此错误一次,在 boost 中抛出两次。

提前致谢。

最佳答案

back_inserter 的作用是什么?

的确如此。它在容器的后面插入元素。

不过,您似乎想要的是 front_readercontainer_source

好吧,我没有 Qt,但我很幸运地使用 array_source 来调整你的输入(注意它如何与 std::string 一起工作,std::vector, std::array 或者甚至只是 const char [] input):

#include <fstream>
#include <iostream>

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/copy.hpp>
#include <sstream>

int main()  {
    using namespace boost::iostreams;
    filtering_streambuf<input> in;

#if 0
    std::string _data { 
#else
    std::vector<char> _data { 
#endif
        char(0x1f), char(0x8b), char(0x08), char(0x00), char(0xca), char(0xb5),
        char(0x07), char(0x53), char(0x00), char(0x03), char(0xcb), char(0x48),
        char(0xcd), char(0xc9), char(0xc9), char(0x57), char(0x28), char(0xcf),
        char(0x2f), char(0xca), char(0x49), char(0xe1), char(0x02), char(0x00),
        char(0x2d), char(0x3b), char(0x08), char(0xaf), char(0x0c), char(0x00),
        char(0x00), char(0x00)
    };

    in.push( gzip_decompressor() );
    in.push( boost::iostreams::array_source(_data.data(), _data.size()) );

    std::stringstream _sstream;
    boost::iostreams::copy(in, _sstream);

    std::cout << _sstream.rdbuf();
}

程序的输出当然是hello world

关于c++ - 使用boost将gzip压缩/解压缩到内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21943730/

相关文章:

c++ - 组合范围适配器和 std::source_location 得到了奇怪的结果

c++ - 在 Visual Studio 6 中从 VB 调用 VS2010 C++ dll

c++ - 使用 boost::shared_ptr<> 处理管理

express - axios在gzip中不解压获取响应体

c++ - 我的 wxWidgets 基于框架的应用程序需要一个顶级的 sizer 吗?

c++ - AWSStringStream.h : No such file or directory

android - 使用 GZIPOutputStream 压缩字符串

c++将ubuntu下的boost库与cmake : undefined reference to `boost::iostreams::zlib::okay' 链接

java - DLL 到 LIB - 无法使用 JNI 和 System.load 调用打开 LIB 文件

python - 在 Mac OS X Yosemite 上安装 PyGMO - 缺少 boost-python3?