C++ Decompress a gzip array of bytes
Asked Answered
T

1

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.

Trilingual answered 8/5, 2011 at 20:4 Comment(0)
M
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)

Mongol answered 8/5, 2011 at 20:15 Comment(3)
and remember byte ordering when receiving streams from other architecturesAforesaid
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 :)Trilingual
you can just put the char* into a stream, which you can do with stringstream cplusplus.com/reference/iostream/stringstream.Mongol

© 2022 - 2024 — McMap. All rights reserved.