3

I have a vector that contains zlib-compressed (deflated) data. I would like to decompress it with Boost's filtering_istream. There is only one example on their site, which operates on a stream of data (as opposed to a vector that I have).

vector<char> compressed_buffer;
compressed_buffer.resize(cdh.length);
file.read(&compressed_buffer[0], cdh.length);

filtering_istream in;
in.push(zlib_decompressor());
in.push(something(compressed_data)); // what should "something" be?

I would like to get the uncompressed data as a vector as well. How can I do this?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180

2 Answers2

4

How about an array_source?

in.push(array_source(&*compressed_data.begin(), &*compressed_data.end()));

Then use boost::iostreams::copy with a std::insert_iterator to push the result characters into a new vector.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

The accepted answer suggests &*compressed_data.end() which is undefined behavior because you dereference a past-the-end iterator. It only works by accident. The correct answer should use data() and data() + size() instead of begin() and end().

in.push(array_source(compressed_data.data(), compressed_data.data() + compressed_data.size()));