Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8902191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:33:02+00:00 2026-06-15T01:33:02+00:00

After understood (with some help…) how work the compress and uncompress functions of zlib

  • 0

After understood (with some help…) how work the compress and uncompress functions of zlib library, I’m now trying to understand how deflate and inflate work. As far as i understand, compress is used in a single call, whereas deflate can be called several time.

Having a simple program with a Particle struct (coordinate x, y, z), I can deflate my datas without errors (getting a Z_STREAM_END response) and then inflate them with another z_stream object (Z_STREAM_END response too). But when I tried to display back my datas from the inflate response, I can get the x and y coordinate of my struct by not the third one (z).

I think it’s due to a wrong parameters i gave to my z_stream object for inflate, but I can’t find which one. As far as i understand reading docs and example, that’s how I think z_stream works (this is just an example) :

// Here i give a total memory size for the output buffer used by deflate func
#define CHUNK 16384

struct Particle
{
    float x;
    float y;
    float z;
};

...

// An element to get a single particule and give it to deflate func
Bytef *dataOriginal = (Bytef*)malloc( sizeof(Particle) );

// This var will be used to pass compressed data
Bytef *dataCompressed = (Bytef*)malloc( CHUNK );

z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
deflateInit(&strm, Z_DEFAULT_COMPRESSION);

strm.avail_out = CHUNK;
strm.next_out = dataCompressed;

int nbrLoop = 2;
int spaceUsed = 0;
int flush;
Particle p;

for (var i = 0; i<nbrLoop; i++){
    // set all values equals to 0
    memset( &p, 0, sizeof(Particle) );

    // insert some random values
    p.x = (i+1) * 1;
    p.y = (i+1) * 3;
    p.z = (i+1) * 7;

    //copy this values in a Bytef* elements
    memcpy( dataOriginal, &p, sizeof(Particle) );


    strm.avail_in = sizeof(dataOriginal);
    strm.next_in = dataOriginal;

    // If it's the last particle :
    if(i == nbrLoop - 1){
    flush = Z_FINISH;
    }
    else{
        flush = Z_NO_FLUSH;
    }

    int response = deflate(&strm, flush);

    // I don't get any errors here
    // EDIT : Get Z_OK at first loop, the Z_STREAM_END at second (last)
    if( res == Z_STREAM_END ){
        spaceUsed = CHUNK - strm.avail_out;
    }
}

deflateEnd(&strm);

// Trying to get back my datas
Bytef *decomp = (Bytef*)malloc( sizeof(Particle) );

z_stream strmInflate;
strmInflate.zalloc = Z_NULL;
strmInflate.zfree = Z_NULL;
strmInflate.opaque = Z_NULL;
inflateInit(&strmInflate);

// datas i want to get at the next inflate
strmInflate.avail_in = sizeof(Particle);
strmInflate.next_in = dataCompressed;

// Two particles were compressed, so i need to get back two
strmInflate.avail_out = sizeof(Particle) * 2;
strmInflate.next_out = decomp;

int response = inflate( &strmInflate, Z_NO_FLUSH );
// No error here,
// EDIT : Get Z_OK

inflateEnd( &strmInflate );

Particle testP;
memset( &testP, 0, sizeof(Particle) );
memcpy( &testP, decomp, sizeof(Particle) );

std::cout << testP.x << std::endl; // display 1 OK
std::cout << testP.y << std::endl; // display 3 OK
std::cout << testP.z << std::endl; // display 0 NOT OK

Moreover, i thought that calling inflate a second time will allow me to recover datas of my second particle that was created in my for loop but i can’t retrieve it.

Thanks in advance for any help !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:33:04+00:00Added an answer on June 15, 2026 at 1:33 am

    strmInflate.avail_in = sizeof(Particle); needs to be strmInflate.avail_in = spaceUsed; You have to provide inflate all of the data produced by deflate.

    At the end you want to get Z_STREAM_END from inflate(), not Z_OK. Otherwise you have not decompressed the entire generated stream.

    Note that per the documentation in zlib.h, you need to also set next_in and avail_in (to Z_NULL and 0 if you like) before calling inflateInit()

    Depending on the size of the input and output buffers you will be using in the final application, you may need more loops to assure that deflate() and inflate() can finish their jobs. Please see the example of how to use zlib.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how ID3 tags work, so, after reading some documentation, I
I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
i am self-studying asp.net and was trying some small projects to help understand the
After some painful experiences, I understand the problem of dangling pointers and double free.
After I thought that I've understood how they work, I tried this: NSString *str1
I am trying to understand why after cropping an image in .NET i end
I need some help to understand how can I track the data to insert
I need some help! :) I am trying to put a header png image
I thought I understood the basics of pointers, but after checking out some documentation
I can use some help to understand jQuery and jQuery UI dialog a bit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.