1

I have a personal project where I have to use zlib to decompress some data. I store the binary data into a std::vector where byte_t isn't anything else than unsigned char.

So I modified the zlib decompress function from their site to look like: http://pastebin.com/spXcRyxa

Everything went fine for about 90% of test, but then it crash the application(some bug in addToVector - couldn't find it).

Then I remember that boost have a nice wrapper for zlib. I compiled boost with zlib and searched how to do it with a std::vector (almost all examples were for std::string). But I found an example for bzip(Uncompress data in memory using Boost gzip_decompressor), then I changed some things to make it run;

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

    boost::iostreams::filtering_ostream os;

    os.push(boost::iostreams::zlib_decompressor());
    os.push(std::back_inserter(decompressed));

    boost::iostreams::write(os, reinterpret_cast<const char*>(&compressed[0]), compressed.size());

    return decompressed;
}

My changes fails to all tests. The decompress data from boost zlib is about 10 000 characters shorter than normal zlib library.

What am I doing wrong in this boost zlib implementation ? Thanks you !

Community
  • 1
  • 1
Andrei Damian
  • 396
  • 2
  • 6
  • 16

1 Answers1

1

Really your sample should be self-contained and reproducible (easy, just include a tiny compressed sample datum).

In the absense of that, here's my two cents: Make sure it's flushed. Simplest way is to close the ostream before using the sunk data:

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

    {
        boost::iostreams::filtering_ostream os;

        os.push(boost::iostreams::zlib_decompressor());
        os.push(std::back_inserter(decompressed));

        boost::iostreams::write(os, reinterpret_cast<const char*>(&compressed[0]), compressed.size());
    }

    return decompressed;
}

PS. Note how I take the compressed data by const&. Your function would copy the input before decompressing...

sehe
  • 374,641
  • 47
  • 450
  • 633