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 564997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:49:31+00:00 2026-05-13T12:49:31+00:00

I’m using zlib to perform gzip compression. zlib writes the data directly to an

  • 0

I’m using zlib to perform gzip compression. zlib writes the data directly to an open TCP socket after compressing it.

/* socket_fd is a file descriptor for an open TCP socket */
gzFile gzf = gzdopen(socket_fd, "wb");
int uncompressed_bytes_consumed = gzwrite(gzf, buffer, 1024);

(of course all error handling is removed)

The question is: how do you determine how many bytes were written to the socket? All the gz* functions in zlib deal with byte counts/offsets in the uncompressed domain, and tell (seek) doesn’t work for sockets.

The zlib.h header says “This library can optionally read and write gzip streams in memory as well.” Writing to a buffer would work (then I can write the buffer to the socket subsequently), but I can’t see how to do that with the interface.

  • 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-13T12:49:32+00:00Added an answer on May 13, 2026 at 12:49 pm

    zlib can, in fact, write gzip formatted data to a buffer in memory.

    This zlib faq entry defers to comments in zlib.h. In the header file, the comment for deflateInit2() mentions that you should (arbitrarily?) add 16 to the 4th parameter (windowBits) in order to cause the library to format the deflate stream with the gzip format (instead of the default “zlib” format).

    This code gets the zlib state set up properly to encode gzip to a buffer:

    #include <zlib.h>
    z_stream stream;
    stream.zalloc = Z_NULL;
    stream.zfree = Z_NULL;
    stream.opaque = Z_NULL;
    int level = Z_DEFAULT_COMPRESSION;
    int method = Z_DEFLATED;  /* mandatory */
    int windowBits = 15 + 16; /* 15 is default as if deflateInit */
                              /* were used, add 16 to enable gzip format */
    int memLevel = 8;         /* default */
    int strategy = Z_DEFAULT_STRATEGY;
    if(deflateInit2(&stream, level, method, windowBits, memLevel, strategy) != Z_OK)
    {
        fprintf(stderr, "deflateInit failed\n");
        exit(EXIT_FAILURE);
    }
    
    /* now use the deflate function as usual to gzip compress */
    /* from one buffer to another. */
    

    I confirmed that this procedure yields the exact same binary output as the gzopen/gzwrite/gzclose interface.

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

Sidebar

Related Questions

No related questions found

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.