c++ - Uncompress data in memory using Boost gzip_decompressor -


i'm trying decompress binary data in memory using boost gzip_decompressor. this answer, adapted following code:

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

however, returned vector has 0 length. doing wrong? tried calling flush() on os stream, did not make difference

your code works me 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 sure input compressed gzip , not other method (e.g. raw deflate)? gzip compressed data begins bytes 1f 8b.

i use reset() or put stream , filters in own block make sure output complete. did both compression above, example.


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -