Uncompress data in memory using Boost gzip_decompressor

拜拜、爱过 提交于 2019-12-01 23:27:21

Your code works for me with this simple test program:

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

Are you sure your input was compressed with gzip and not some other method (e.g. raw deflate)? gzip compressed data begins with bytes 1f 8b.

I generally use reset() or put the stream and filters in their own block to make sure that output is complete. I did both for compression above, just as an example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!