PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

zlib

Is their anyway to read gzipped data from memory using zlib?

I've used zlib when reading from a file but I am loading my gzipped files from a resource file so I need to read the compressed data directly from memory.

How do I do this with zlib?
Thanks.
angedelamort · 26 Nov 2002, 15:46 UTC
you do exactly the same thing as the file. You pass a pointer to the compressed memory buffer and tell the lenght of it. But in the compressed file, there is a header, so you will need to know the uncompressed size to allocate your buffer. If I remember, the function should be like this:

Code:

Uncompress(Byte* dest, unsigned long& destLen, unsigned char* &source, unsigned long& sourceLen);


So, here how it should be in your resource bundle:

[your header][compressed data][your header][compressed data]...

your header should have at least:
compressed size and uncompressed size.
You can add any other parameters like the date, the signature, the version, etc.

Hope it help you.
I've tried using uncompress before but it doesn't support data in gzip format. How do I compress my data so that this function can read it?
Using compress(...)
Just figured that out. :D

Here's the steps I followed incase anyone else runs into the same problem.

Opened my image file and read it into memory, compressed the memory using compress(...), and wrote a compressed file from that memory. Placed this file in my resource file. Loaded the resource into memory and then I just used uncompress(...).

Thanks for the replies.
Malmer wrote:
Using compress(...)


hmm, is'nt there any command line program that compresses files with a zlib compatible algo?

cryo
Yep. gzip

Dan East
Thanks Dan,

i have tried that, but it's not working, i always get a Z_DATA_ERROR.

do you have a code sample?

cryo
Sample to do what, compress or decompress? I just open the file with gzopen and then gzwrite or gzread (and of course stick a .gz on the end if I'm creating).

Dan East
i was trying to store compressed (i.e. gzipped) files as resources, get a pointer to them at run time and decompress using zlib, but i get different errors.

I'm using the inflateInit2, inflate and inflateEnd functions as described in the examples. Also, i am giving inflateInit2 a -15 as second parameter to skip the .gz headers and go straight to the deflated data.

The problem is, none of this works. Has anyone done something like this before?

thanks

cryo
Just write your own little utility that does the compress using zLib's compress. Gzip puts headers and stuff to it, but you're easier of with just compressing the raw buffer and save it as a file.
cryo wrote:
i was trying to store compressed (i.e. gzipped) files as resources, get a pointer to them at run time and decompress using zlib, but i get different errors.


This was exactly what I was trying to do and I got it to work using the compress() and uncompress() functions.

Just do what Malmer described above.
i know it works that way, it's just that the fact that it seems to be impossible to do proper in-memory decompression of a gzipped file with zlib, even if it says it can do so, triggered the sportsman in me.

Anyway, i don't have time to play around with that, so i'll go with writing my own compress prog. as you suggested.

Thanks!

cryo
Yea that really drove me crazy too! I read many other posts about people trying to decompress gzipped files in memory with zlib on Google Groups and no one seemed to have a solution which is why I originally posted. It seems that zlib can't do this...
I'm not sure this helps, but I gzip (.gz) my tga picture files, and load them using the (easyce) function called tgazload(), I'm sure if you got a copy of easyce, looked at the function it should have the required information.

-B
I imagine that it probably just uses gzopen and gzread(). This thread was about reading the same type of data from memory rather than from a file, which zlib doesn't apparently support.

Thanks for the suggestion though. :)
i finally gave up on uncompressing using InflateInit2, and ended up writing a command-line app. that calls compress, and calling uncompress in my prog.

cryo