C++ Decompress a gzip array of bytes

bardes picture bardes · May 8, 2011 · Viewed 8.1k times · Source

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.

Answer

David Titarenco picture David Titarenco · May 8, 2011
#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)