How to unzip gzipstream using zlib

Can someone tell me which function I need to use to unzip a byte array compressed using vb.net gb stream. I would like to use zlib.

I included zlib.h, but I was not able to figure out what functions I should use.

+2
source share
4 answers

You can see 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 unzip line by line:

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

Or the whole file into an array:

 std::vector<char> data( std::istreambuf_iterator<char>(decompressor) , std::istreambuf_iterator<char>() ); 
+6
source

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

There are many code examples in zlib distribution . Also check out this highly documented zlib example . You can change this to use inflateInit2() instead of inflateInit() .

0
source

Here is the C function that does the job using zlib:

 int gzip_inflate(char *compr, int comprLen, char *uncompr, int uncomprLen) { int err; z_stream d_stream; /* decompression stream */ d_stream.zalloc = (alloc_func)0; d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; d_stream.next_in = (unsigned char *)compr; d_stream.avail_in = comprLen; d_stream.next_out = (unsigned char *)uncompr; d_stream.avail_out = uncomprLen; err = inflateInit2(&d_stream, 16+MAX_WBITS); if (err != Z_OK) return err; while (err != Z_STREAM_END) err = inflate(&d_stream, Z_NO_FLUSH); err = inflateEnd(&d_stream); return err; } 

An uncompressed string is returned in uncompr. This is a C line with zero completion, so you can do puts (uncompr). The function above only works if the output is text. I tested it and it works.

0
source

See an example using zlib. http://www.zlib.net/zpipe.c

The function that does the real work is inflated (), but you need inflateInit (), etc.

-1
source

Source: https://habr.com/ru/post/909578/


All Articles