6

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.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
bardes
  • 751
  • 2
  • 7
  • 13

1 Answers1

9
#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)

Community
  • 1
  • 1
David Titarenco
  • 32,662
  • 13
  • 66
  • 111
  • 1
    and remember byte ordering when receiving streams from other architectures – sehe May 08 '11 at 20:17
  • the problem is that I don't understand how filtering_streambuf works and I need to inflate a char* not a file... (i know it's possible to put the char* inside the file and read the result later, but I was thinking about something simpler) About the byte order, tiled is little-endian, it already caused me some trouble, but it's ok now... thanks for your attention :) – bardes May 08 '11 at 20:29
  • you can just put the `char*` into a stream, which you can do with `stringstream` http://www.cplusplus.com/reference/iostream/stringstream/. – David Titarenco May 08 '11 at 20:31