1

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

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 的错误中一次两次地抛出这个错误。

提前致谢。

4

3 回答 3

3

back_inserter 做什么?

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

但是,您似乎追求的是 a front_readeror container_source

好吧,我没有 Qt,但是我很幸运地使用array_source它来调整您的输入(注意它如何与std::string,std::vectorstd::array什至只是const char []输入同样好用):

#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

于 2014-02-21T21:09:27.760 回答
0

使用@sehe 的答案修复它。

这是生成的代码:(希望它对有相同错误的人有用)

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

    std::vector<char> _data;
    foreach( char ch, data )
        _data.push_back(ch);

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

    QByteArray out;
    while ( in.sgetc() != -1 )
        out += (unsigned char)in.sbumpc();

    std::cout << "out size! :: " << in.in_avail() << std::endl;
    //std::cout << "out size! :: " << _sstream.str().size() << std::endl;
    std::cout << "out size! :: " << out.size() << std::endl;

    return out;
}
于 2014-02-22T10:54:53.850 回答
0

我还使用了一个简单的字符数组,为了提高效率,我不想创建一个向量并复制缓冲区内容。所以,这是我对一个简单的 in-ram 压缩/解压缩实用程序类的看法,它直接从从线路读取的 char 数组解压缩或压缩到另一个 char 数组中:

/*
 * GzipUtil.h
 *
 *  Created on: Nov 3, 2015
 *      Author: tprice
 */

#ifndef GZIPUTIL_H_
#define GZIPUTIL_H_

#include <unistd.h>
#include <stdint.h>
#include <vector>
#include <stdlib.h>

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

namespace BIO = boost::iostreams;

class GzipUtil {
public:
GzipUtil() {}
virtual ~GzipUtil() {}

static ssize_t compress(char *src, char *dest, ssize_t len)
{
    ssize_t retval = 0;

    BIO::filtering_streambuf<BIO::input> in;
    in.push( BIO::gzip_compressor() );
    in.push( BIO::array_source(src, len) );
    for(retval = 0; in.sgetc() != -1; retval++ )
    {
        dest[retval] = (char) in.sbumpc();
    }

    return retval;
}

static ssize_t decompress(char *src, ssize_t srclen, std::vector<char*> & dest, ssize_t destChunkLen)
{
    ssize_t retval = 0;
    char*   chunkPtr = NULL;
    ssize_t chunkUsed = 0;

    BIO::filtering_streambuf<BIO::input> in;
    in.push( BIO::gzip_decompressor() );
    in.push( BIO::array_source(src, srclen) );
    for(retval = 0; in.sgetc() != -1; retval++ )
    {
        if((chunkPtr == NULL) || (chunkUsed >= destChunkLen))
        {
            chunkPtr = (char*) malloc(destChunkLen);
            dest.push_back(chunkPtr);
            chunkUsed = 0;
        }
        chunkPtr[chunkUsed++] = (char) in.sbumpc();
    }

    return retval;
}

};


#endif /* GZIPUTIL_H_ */
于 2015-11-03T15:06:37.857 回答