2

我正在尝试使用 Boost 解压缩内存中的二进制数据gzip_decompressor。从这个答案,我改编了以下代码:

vector<char> unzip(const vector<char> compressed)
{
    vector<char> decompressed = vector<char>();

    boost::iostreams::filtering_ostream os;

    os.push(boost::iostreams::gzip_decompressor());
    os.push(boost::iostreams::back_inserter(decompressed));

    boost::iostreams::write(os, &compressed[0], compressed.size());

    return decompressed;
}

但是,返回的向量长度为​​零。我究竟做错了什么?我尝试调用flush()os 流,但没有任何区别

4

1 回答 1

3

你的代码适用于我这个简单的测试程序:

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

std::vector<char> unzip(const std::vector<char> compressed)
{
   std::vector<char> decompressed = std::vector<char>();

   boost::iostreams::filtering_ostream os;

   os.push(boost::iostreams::gzip_decompressor());
   os.push(boost::iostreams::back_inserter(decompressed));

   boost::iostreams::write(os, &compressed[0], compressed.size());

   return decompressed;
}

int main() {
   std::vector<char> compressed;
   {
      boost::iostreams::filtering_ostream os;
      os.push(boost::iostreams::gzip_compressor());
      os.push(boost::iostreams::back_inserter(compressed));
      os << "hello\n";
      os.reset();
   }
   std::cout << "Compressed size: " << compressed.size() << '\n';

   const std::vector<char> decompressed = unzip(compressed);
   std::cout << std::string(decompressed.begin(), decompressed.end());

   return 0;
}

你确定你的输入是用 gzip 压缩的,而不是其他方法(例如原始放气)吗?gzip 压缩数据以字节开头1f 8b

我通常使用reset()或将流和过滤器放在自己的块中,以确保输出完整。只是作为一个例子,我为上面的压缩做了两个。

于 2013-05-22T14:00:45.153 回答