boost gzip decompress byte array

不想你离开。 提交于 2019-12-01 03:15:13

Obviously, you've come across filtering streams and stream buffers. You can use the same method in reverse to get data into a string.

I don't have my own examples handy, so consider this to be somewhat pseudo-code but this should be what you're looking for:

namespace io = boost::iostreams; //<-- good practice
typedef std::vector<char> buffer_t;

void CompressionUtils::Inflate(const buffer_t &compressed,
                               buffer_t &decompressed)
{
    io::filtering_ostream os;

    os.push(io::gzip_decompressor());
    os.push(io::back_inserter(decompressed));

    io::write(os, &compressed[0], compressed.size());
}

So you can use the back inserter provided by Boost.

Basically, what the above code does is define an output stream which you can write to. It is set up so that all content written to it will first be decompressed by gzip, and then appended to the back_inserter which will, as back_inserters do, insert into the back of the decompressed buffer.

Also, as you can see, the buffers are wrapped in std::vector. Let me know if this works for you.

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