Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decompress gzipstream with zlib

Can someone tell me which function I need to use in order to decompress a byte array that has been compressed with vb.net's gzipstream. I would like to use zlib.

I've included the zlib.h but I haven't been able to figure out what function(s) I should use.

like image 833
Lotzki Avatar asked Feb 04 '13 13:02

Lotzki


People also ask

How do I decompress a zlib?

With the help of zlib. decompress(s) method, we can decompress the compressed bytes of string into original string by using zlib. decompress(s) method. Return : Return decompressed string.

Is zlib compression lossless?

zlib is a free, open source software library for lossless data compression and decompression . It was written by Jean-loup Gailly (compression) and Mark Adler (decompression), in C language. The first version of zlib was released in May 1995.

What does zlib compression do?

zlib compressed data are typically written with a gzip or a zlib wrapper. The wrapper encapsulates the raw DEFLATE data by adding a header and trailer. This provides stream identification and error detection that are not provided by the raw DEFLATE data.


2 Answers

You can take a look at The Boost Iostreams Library:

#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>

std::ifstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
file.open(filename, std::ios_base::in | std::ios_base::binary);

boost::iostreams::filtering_stream<boost::iostreams::input> decompressor;
decompressor.push(boost::iostreams::gzip_decompressor());
decompressor.push(file);

And then to decompress line by line:

for(std::string line; getline(decompressor, line);) {
    // decompressed a line
}

Or entire file into an array:

std::vector<char> data(
      std::istreambuf_iterator<char>(decompressor)
    , std::istreambuf_iterator<char>()
    );
like image 193
Maxim Egorushkin Avatar answered Sep 22 '22 07:09

Maxim Egorushkin


You need to use inflateInit2() to request gzip decoding. Read the documentation in zlib.h.

There is a lot of sample code in the zlib distribution. Also take a look at this heavily documented example of zlib usage. You can modify that one to use inflateInit2() instead of inflateInit().

like image 28
Mark Adler Avatar answered Sep 22 '22 07:09

Mark Adler