• C

zlib inflate c example

Hi,

Given an array that contains zlib compressed data, can you give me the code to decompress the data and store it in another array?
PMembreyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pgnatyukCommented:
0
Infinity08Commented:
The official web page of zlib contains a nice overview of how to use the deflate and inflate functionality :

        http://www.zlib.net/zlib_how.html
0
PMembreyAuthor Commented:
That one is where I started - but it's written on the assumption that the data is being read directly from a file. I tried going through lib.h and looking at the documentation there, but it didn't take much for me to get very stuck :-)
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

PMembreyAuthor Commented:
That's the example I was working through but it makes calls to fread() whereas I just have an array.
0
Infinity08Commented:
Reading from a file is not very different than reading from a buffer in memory.

Just leave out the fread calls, and instead, simply point to your memory buffer that holds the compressed data.

Similarly, you can leave out the fwrite calls, and use a memory buffer for output.
0
Infinity08Commented:
>> simply point to your memory buffer that holds the compressed data.

You'll note that in the example code, the two buffers used are called 'in' and 'out'. Just replace those with yours, and you're all set.
0
PMembreyAuthor Commented:
So given two arrays, one containing the compressed version and one empty for the decompressed version - what would an example code fragment be?

0
pgnatyukCommented:
This is my working code:
uLongf destLen = buffSize;
int err = uncompress( (Bytef *)pBuff, &destLen, (const Bytef *)pData, pSize);
pBuff is a memory block (void*) - the destination
pData - the source.
 
0
PMembreyAuthor Commented:
pgnatyuk, you've got the points as I've got the example to work - but could you add to your example how to find out how much data was decompressed?  i.e. if I want to write that out to a file, I need to know the length of the decompressed data :-)
0
Infinity08Commented:
>> what would an example code fragment be?

Something like :
unsigned char in_buffer[IN_SIZE];             /* compressed data : IN_SIZE bytes */
unsigned char out_buffer[OUT_SIZE];           /* make sure OUT_SIZE is big enough to hold the decompressed data */

z_stream strm;

strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;

strm.avail_in = 0;
strm.next_in = Z_NULL;

ret = inflateInit(&strm);
if (ret != Z_OK) {
    /* oops : handle this as an error, and stop */
}

strm.avail_in = IN_SIZE;
strm.next_in = in_buffer;
strm.avail_out = OUT_SIZE;
strm.next_out = out_buffer;

ret = inflate(&strm, Z_NO_FLUSH);
if (ret != Z_STREAM_END) {
    /* oops : the data wasn't completely processed - handle this */
}

inflateEnd(&strm);

Open in new window

0
Infinity08Commented:
>> I need to know the length of the decompressed data :-)

strm.total_out will hold that value.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMembreyAuthor Commented:
hmm, looks like I will split the points :-)

But I'd still like to know how to update the first example so that it provides the decompressed data size....
0
Infinity08Commented:
>> But I'd still like to know how to update the first example so that it provides the decompressed data size....

When using uncompress, destLen will hold the size of the decompressed data.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.