# ZLib Uncompress String

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

## 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:

H4sIAAAAAAAAAO2UPRMBQQyGt0CNAgU6X9eh8E2Nn3D8BPz/ThQ3MpmsZFfcMeOdeZrbm9133ySbOudOEZzdUz2gH8EA7TEGJgJT9P8G2BJ2gqeBsB7q+bHHBbhGcDP2wuXxKp89cCAc0TpXD5y/VX4WmgHzCBZoDy6PV/lIiunPovLT3P3dPCw8Sf2pYUr2sFDq3n9D85bmvZDekI2RF4s3NG9ZzMw+R79//fXLKgFlQqVQR7+lKlAj1At19J3CfYb7qw10CN3c3T2V+Qz9/9MzhPsM99cQGBEShT8toffIfGrOfezdAJoMrcBzJeE+w/0l5cfNdwi4VppeyXz6cqEZcf6tegD7wucknu/cOjffIeBa+WrBzYMvF+pzCawY1mhPTS2kHo7NT3MP6Y6ZfLXg5uEO9/7EuMASAAA=

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.

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

##### Share on other sites
Quote:
 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;   output.resize(outputSize);}// Test "result" for success and continue...

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

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 18
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007475
• ### Who's Online (See full list)

There are no registered users currently online

×