boost gzip decompress byte array

后端 未结 1 1962
野的像风
野的像风 2021-01-11 18:54

I implemented the gzip/zlib decompression of files as shown in their examples on the boost site.

void CompressionUtils::Inflate(std::ifstream& inputFile,         


        
1条回答
  •  隐瞒了意图╮
    2021-01-11 19:15

    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 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.

    0 讨论(0)
提交回复
热议问题