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

  • SEARCH
  • Home
  • 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 3623084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:20:23+00:00 2026-05-18T23:20:23+00:00

There is a usage example at the zlib website: http://www.zlib.net/zlib_how.html However in the example

  • 0

There is a usage example at the zlib website: http://www.zlib.net/zlib_how.html

However in the example they are compressing a file. I would like to compress a binary data stored in a buffer in memory. I don’t want to save the compressed buffer to disk either.

Basically here is my buffer:

fIplImageHeader->imageData = (char*)imageIn->getFrame();

How can I compress it with zlib?

I would appreciate some code example of how to do that.

  • 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-05-18T23:20:24+00:00Added an answer on May 18, 2026 at 11:20 pm

    This is an example to pack a buffer with zlib and save the compressed contents in a vector.

    void compress_memory(void *in_data, size_t in_data_size, std::vector<uint8_t> &out_data)
    {
     std::vector<uint8_t> buffer;
    
     const size_t BUFSIZE = 128 * 1024;
     uint8_t temp_buffer[BUFSIZE];
    
     z_stream strm;
     strm.zalloc = 0;
     strm.zfree = 0;
     strm.next_in = reinterpret_cast<uint8_t *>(in_data);
     strm.avail_in = in_data_size;
     strm.next_out = temp_buffer;
     strm.avail_out = BUFSIZE;
    
     deflateInit(&strm, Z_BEST_COMPRESSION);
    
     while (strm.avail_in != 0)
     {
      int res = deflate(&strm, Z_NO_FLUSH);
      assert(res == Z_OK);
      if (strm.avail_out == 0)
      {
       buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE);
       strm.next_out = temp_buffer;
       strm.avail_out = BUFSIZE;
      }
     }
    
     int deflate_res = Z_OK;
     while (deflate_res == Z_OK)
     {
      if (strm.avail_out == 0)
      {
       buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE);
       strm.next_out = temp_buffer;
       strm.avail_out = BUFSIZE;
      }
      deflate_res = deflate(&strm, Z_FINISH);
     }
    
     assert(deflate_res == Z_STREAM_END);
     buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out);
     deflateEnd(&strm);
    
     out_data.swap(buffer);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a common way to express the usage of arguments in C++? I
Is there a tool/plugin/function for Firefox that'll dump out a memory usage of Javascript
Given a postscript file that has the following header %!PS-Adobe-3.0 I would like to
I would like to know if there is any functionality in the Gecko SDK/NPAPI
I would like to remove/delete a migration file. How would I go about doing
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There is previous little on the google on this subject other than people asking
There seem to be many ways to define singletons in Python. Is there a

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.