Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Decompress a gzip array of bytes

Here is the complete situation: I'm working on a map reader for .tmx files, from tiled. Most times the tiles are saved in a base64 string, which contains an array of bytes compressed by gzip. Right now I can read the array of compressed bytes, but I have no idea how to decompress it. I read some docs about zlib and boost, but both were about file streams and very complicated...

I'm very new to the data compression area, so if anyone knows a kinda solution or some helpful documentation I would really apreciate.

like image 443
bardes Avatar asked May 08 '11 20:05

bardes


People also ask

Can you compress byte array?

Compressing a byte array is a matter of recognizing repeating patterns within a byte sequence and devising a method that can represent the same underlying information to take advantage of these discovered repetitions.

What is gzip decompression?

gzip is a file format and a software application used for file compression and decompression. The program was created by Jean-loup Gailly and Mark Adler as a free software replacement for the compress program used in early Unix systems, and intended for use by GNU (from where the "g" of gzip is derived).

What is gzip deflate?

The browser sends a header telling to the server it accepts compressed content (gzip and deflate are two compression schemes): Accept-Encoding: gzip, deflate. The server sends a response to the browser if the content is actually compressed: Content-Encoding: gzip.

How much does gzip reduce file size?

Gzip, the most popular compression method, is used by web servers and browsers to seamlessly compress and decompress content as it's transmitted over the Internet. Used mostly on code and text files, gzip can reduce the size of JavaScript, CSS, and HTML files by up to 90%.


1 Answers

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>

int main() 
{
    using namespace std;

    ifstream file("hello.gz", ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(gzip_decompressor());
    in.push(file);
    boost::iostreams::copy(in, cout);
}

I'm not sure what's difficult or complex when looking at the above example taken from http://www.boost.org/doc/libs/1_36_0/libs/iostreams/doc/classes/gzip.html. The decompression is very straightforward. Before you decompress, make sure you decode the base64. ( How do I base64 encode (decode) in C? should help you)

like image 94
David Titarenco Avatar answered Oct 03 '22 02:10

David Titarenco