Solved

zlib inflate c example

Posted on 2010-08-30
13
987 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:PMembrey
  • 6
  • 5
  • 2
13 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33564671
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33564676
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
 

Author Comment

by:PMembrey
ID: 33564677
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:PMembrey
ID: 33564702
That's the example I was working through but it makes calls to fread() whereas I just have an array.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33564703
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33564706
>> 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
 

Author Comment

by:PMembrey
ID: 33564713
So given two arrays, one containing the compressed version and one empty for the decompressed version - what would an example code fragment be?

0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 200 total points
ID: 33564791
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
 

Author Comment

by:PMembrey
ID: 33564910
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33564911
>> 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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 300 total points
ID: 33564928
>> I need to know the length of the decompressed data :-)

strm.total_out will hold that value.
0
 

Author Comment

by:PMembrey
ID: 33564936
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33564977
>> 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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now