Jump to content
  • Advertisement
Sign in to follow this  

ZLib Uncompress String

This topic is 3073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folks,

I'm not sure if this is right forum to ask, but "general programming" is where I thought it would be best suited.

I'm having a few issues using ZLib to uncompress data held as a std::string.

Edit: I'm using C++.

I think that I have to use the uncompress() method in zlib.h but I'm not sure how to use it as I have no idea what Bytef* and such structs are.

Basically I have a TilED map file which looks like http://pastebin.com/dzit3Bj3 and I've extracted the data in each "layer" which looks like:


This is base 64 encoded, so I have decoded it and got a string object to work with, which I need to decompress to read the data.

If somebody can help me out, that would be great. Note that I don't want any examples that use CString as I'm not using this class, just std::string and char arrays please.

Let's start with:

std::string compressed = ""; // Let's pretend this is some compressed data string.

Share this post

Link to post
Share on other sites

ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
const Bytef *source, uLong sourceLen));
Decompresses the source buffer into the destination buffer. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be large enough to hold the entire uncompressed data. (The size of the uncompressed data must have been saved previously by the compressor and transmitted to the decompressor by some mechanism outside the scope of this compression library.) Upon exit, destLen is the actual size of the compressed buffer.
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.

This is copied from the Zlib manual.

The Bytef pointers are just pointers to any byte sized data type. Zlib can compress anything, including non textual data. This is somewhere you would use "reinterpret_cast".

std::string compressed = loadCompressedData();
uLongf outputSize = /* calculate or guess, whatever */
std::vector<Bytef> output(outputSize);
const Bytef *input = reinterpret_cast<const Bytef*>(compressed.c_str());

int result;
while((result = uncompress(&output.front(), &outputSize, input, compressed.length())) == Z_BUF_ERROR)
outputSize *= 2;

// Test "result" for success and continue...

Something like that. You can build a string from a vector of bytes quite easily.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!