Skip to content

Latest commit

 

History

History
152 lines (98 loc) · 3.63 KB

File metadata and controls

152 lines (98 loc) · 3.63 KB

zlib.h - Data Compression Library

Category: 📂 zlib/ (Third-Party Ecosystem) Header: <zlib.h> Scope: DEFLATE Compression

The zlib.h header provides functions for in-memory compression and decompression using the DEFLATE algorithm (LZ77 + Huffman coding). It is the standard compression library for PNG, HTTP (gzip), and many other formats.

Facilities Overview

Facility Category Key Symbols Description
Stream Mgmt deflateInit, inflateInit Initializing the compression/decompression state.
Processing deflate, inflate Compressing or decompressing data chunks.
Cleanup deflateEnd, inflateEnd Releasing internal buffers.
Utility compress, uncompress Simple, single-buffer convenience functions.

Types & Variables

z_stream

typedef struct z_stream_s {
    z_const Bytef *next_in;     /* next input byte */
    uInt     avail_in;          /* bytes available at next_in */
    uLong    total_in;          /* total number of input bytes read so far */
    
    Bytef    *next_out;         /* next output byte should be put there */
    uInt     avail_out;         /* remaining free space at next_out */
    uLong    total_out;         /* total number of bytes output so far */
    
    char     *msg;              /* last error message, NULL if no error */
    // ... internal state ...
} z_stream;

The core structure for communicating with zlib, containing buffers for input and output.


Functions

Stream Management

deflateInit

int deflateInit(z_streamp strm, int level)

Initializes the internal stream state for compression.

  • level: 0 (no compression) to 9 (best compression), or Z_DEFAULT_COMPRESSION (-1).

Returns: Z_OK on success. Z_MEM_ERROR if memory could not be allocated.

Example
#include <zlib.h>
#include <stdio.h>
#include <string.h>

int main(void) {
    z_stream strm = {0};
    // Initialize for default compression
    if (deflateInit(&strm, Z_DEFAULT_COMPRESSION) != Z_OK) {
        fprintf(stderr, "deflateInit failed\n");
        return 1;
    }
    
    deflateEnd(&strm);
    return 0;
}

inflateInit

int inflateInit(z_streamp strm)

Initializes the internal stream state for decompression.

Returns: Z_OK on success.


Processing

deflate

int deflate(z_streamp strm, int flush)

Compresses as much data as possible, and stops when the input buffer becomes empty or the output buffer becomes full.

  • flush: Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, etc.

Returns: Z_OK if some progress has been made. Z_STREAM_END if all input has been consumed and all output has been produced (only when flush is Z_FINISH).

inflate

int inflate(z_streamp strm, int flush)

Decompresses data.

Returns: Z_OK on progress. Z_STREAM_END if the end of the compressed data has been reached.


Cleanup

deflateEnd

int deflateEnd(z_streamp strm)

Frees all dynamically allocated data structures for this stream.

Returns: Z_OK on success.

inflateEnd

int inflateEnd(z_streamp strm)

Frees all dynamically allocated data structures for this stream.

Returns: Z_OK on success.


Utility

compress

int compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)

Compresses the source buffer into the destination buffer.

Returns: Z_OK on success.