Format:DEFLATE
| Developer | Phil Katz (PKWARE); specified by Peter Deutsch in RFC 1951 (1996) |
|---|---|
| Uses | Library:Zlib |
| Extensions | none of its own (embedded in .zip, .gz, .png, zlib streams) |
| Endianness | little (bit stream LSB first; Huffman codes packed MSB first) |
| On this wiki | Facts · Code · Notes · Topics |
The LZ77 + Huffman compressed data format of RFC 1951 (PKZIP 2, 1993), used inside zlib streams, gzip, ZIP, PNG and many game archives. A stream is a sequence of blocks, each stored, compressed with the fixed Huffman codes or with dynamic codes described at its start; bits are read least significant first.
Overview
Raw DEFLATE has no header, magic or checksum; it is normally wrapped in a Format:Zlib stream (2-byte header, Adler-32) or Format:Gzip (10-byte header, CRC-32), or stored bare inside ZIP entries (method 8). Code that calls zlib's inflateInit2(strm, -15) reads raw deflate.
Block structure
Each block starts with 3 bits: BFINAL (1 = last block) and BTYPE (2 bits): 0 stored, 1 fixed Huffman codes, 2 dynamic Huffman codes, 3 reserved (error). A stored block skips to the next byte boundary, then has LEN and NLEN (one's complement of LEN) as little-endian 16-bit values and LEN raw bytes. Compressed blocks code literals 0-255, end-of-block 256 and lengths 257-285 in one alphabet, followed by a distance code 0-29 after each length, both with extra bits (see the enum facts). Distances reach back up to 32 KiB, lengths 3-258.
Recognising raw deflate in a file: the first byte's low 3 bits are BFINAL + BTYPE, so a final fixed-code block has low bits 011 (hello at level 6 is CB 48 CD C9 C9 07 00), a final stored block starts with 01; an empty final fixed block is the two bytes 03 00.
Code & repositories
- {{Repo|url=https://github.com/madler/zlib%7Ccommit=767c4c947852e143f582c85f14cf573411df1b35%7Clicense=Zlib (LICENSE file; GitHub's detector reports none)|description=The official zlib source (develop branch, after release 1.3.2): inflate/deflate, the gzip file functions, CRC-32 and Adler-32, plus contrib/ (minizip, infback9 for Deflate64, puff, a small reference inflater). Release tags v1.0.x to v1.3.2 let you diff any version that turns up in a binary.|tested_on=Read at this commit and at every release tag (inftrees.c lext pairs, inflate.h modes, deflate.h states, zlib.h API); behaviour checked with Python 3.11 (zlib 1.3.2) and ctypes calls into a MinGW Win64 zlib1.dll 1.3.1; not built (no C compiler on the test machine).|checked=checked 2026-10-17; last activity 2026-09-21; Note: the latest release ships binaries; the wiki links only the source, never the binaries}}
Pitfalls
- Bits are consumed LSB first, but Huffman codes are defined MSB first, so decoders reverse code bits; extra-bit fields are read LSB first like the header bits.
- Lengths 286, 287 and distances 30, 31 are invalid in compressed data but exist in the fixed code tables.
- Deflate64 (ZIP method 9) uses a 64 KiB window and changes length code 285; it is not this format.
Evidence & references
- RFC 1951; zlib inftrees.c/inffixed.h/trees.h at commit 767c4c9; byte sequences produced with Python's zlib 1.3.2 (
compressobj(wbits=-15)).