A ZIP archive, a gzip file and a PNG image look like three unrelated things. Yet the compression inside all three can come from one method, DEFLATE. If you have wondered how that single method works, and why three formats can share it, this is the short tour.
You will see the two ideas inside DEFLATE, a worked example of the first idea, and how the formats wrap the same compressed data in different packaging. The description follows the specifications themselves: RFC 1951 for DEFLATE, RFC 1950 for zlib and RFC 1952 for gzip.
One method, several wrappers
DEFLATE was written down in RFC 1951 in 1996. The RFC says it compresses data using a combination of the LZ77 algorithm and Huffman coding. The surrounding formats add labels and checks, not new compression.
| Name | Defined in | What it is | Framing around the DEFLATE data |
|---|---|---|---|
| DEFLATE | RFC 1951 | The compressed block format | None of its own beyond block headers |
| zlib | RFC 1950 | A wrapper for one DEFLATE stream | 2-byte header, 4-byte Adler-32 checksum at the end |
| gzip | RFC 1952 | A file format of one or more members | 10-byte fixed header, then an 8-byte trailer holding a CRC-32 and the original size |
| PNG | W3C specification | An image format | Its image data chunks together form one zlib stream |
| ZIP | PKWARE APPNOTE | An archive format | Each file records its own method; method 8 means Deflated |
Step one: point back instead of repeating
DEFLATE output is a series of two kinds of items. A literal is a single byte that has not appeared recently. A pointer is a pair of numbers, a length and a backward distance, meaning "go back this many bytes in the output and copy this many bytes." This is the LZ77 idea (Lempel and Ziv, 1977). The format allows lengths of 3 to 258 bytes and distances of 1 to 32,768 bytes, so a pointer can reach back at most 32 KiB (1 KiB is 1,024 bytes, so 32 KiB is 32,768 bytes).
Worked example. Example: the 25-character text "red car, red car, red car". The first 9 characters, "red car, ", have no earlier copy, so they are 9 literals. The remaining 16 characters repeat the start, so one pointer with length 16 and distance 9 covers them. A decoder copies byte by byte, and the copy may run into bytes it has just written, which the RFC allows. Total: 9 + 16 = 25 characters from 10 items. In the RFC's tables, length 16 falls under length code 267 with 1 extra bit, and distance 9 under distance code 6 with 2 extra bits.
Ten items are not ten bytes. That count only shows how much repeated text was replaced. The next step decides how many bits each item takes.
Step two: give common items short codes
DEFLATE writes the items with Huffman codes, where frequent values get shorter bit patterns. It uses one code tree for literals and lengths together, and a separate tree for distances. Literal bytes are values 0 to 255, the value 256 marks the end of a block, and values 257 to 285 are length codes, some followed by extra bits.
The data comes in blocks, and each block has one of three types:
- Stored. The bytes are copied as they are, with no compression.
- Fixed Huffman. Both code trees are predefined, so nothing needs to be written out.
- Dynamic Huffman. The block carries its own pair of trees, in compact form, just before the data. The RFC notes that the trees for each block are independent of the others.
A compressor picks the type per block. Pointers may reach back into an earlier block, up to 32 KiB before.
How ZIP, gzip and PNG use it
ZIP's specification lists a numbered compression method for each file. Method 0 means stored, with no compression, and method 8 means Deflated. The same document says method 8 is the one most ZIP-compatible programs use by default. Each file in an archive can be stored or compressed independently of the others.
A gzip file wraps DEFLATE blocks in the header and trailer shown above. The zlib format wraps them in a shorter header and an Adler-32 checksum. The smallest possible framing is therefore 6 bytes for zlib (2 + 4) and 18 bytes for gzip (10 + 8), before any optional fields.
PNG applies DEFLATE after a separate step. The specification says its compression method 0 is deflate with a sliding window of at most 32,768 bytes. Image data is first filtered, a reversible step meant to improve compressibility, and the result is stored as a zlib stream split across data chunks.
What the format leaves to the compressor
RFC 1951 defines how compressed data is read, not how a compressor finds the matches. Its own section on algorithm details says it is not part of the specification, and a compressor need not follow it to be compliant. A compressor may also limit its window or block size and still comply.
The RFC does state one hard limit. A simple counting argument shows no lossless method compresses every input, and the worst-case expansion for DEFLATE is 5 bytes per 32K-byte block, about 0.015% for large data. This guide reads 32K as 32,768 bytes, the same figure as the distance limit. Example: 1,000,000 bytes that cannot be compressed fit in 31 blocks of up to 32,768 bytes (1,000,000 ÷ 32,768 = 30.5, rounded up), so at most 31 × 5 = 155 extra bytes, a 0.0155% increase. A gzip file adds 18 bytes of framing on top. Use the compression ratio calculator with 1,000,000 and 1,000,173 bytes to see a ratio just under 1:1.
Common mistakes
- Treating DEFLATE, ZIP and gzip as the same thing. DEFLATE is a method. ZIP is an archive format that can hold many files and several methods. gzip is a file format for one stream. For the archive side, see how ZIP files work.
- Assuming every file in a ZIP archive is deflated. The specification allows stored files and other methods too.
- Thinking PNG is only DEFLATE. PNG filters rows first, and the filtered bytes go into DEFLATE.
- Reading "32 KiB window" as a file size limit. The window is how far back a pointer may reach. Files can be far larger.
- Expecting DEFLATE to beat every specialized method. RFC 1951 says the format does not try to compress specialized data, such as raster graphics, as well as the best specialized algorithms.
FAQ
Is DEFLATE lossless?
Yes. The zlib and gzip specifications both describe their formats as lossless, and decompressing returns the original bytes exactly. See lossless vs. lossy compression for the wider picture.
Is gzip the same as ZIP?
No. gzip holds one compressed stream per member. ZIP is an archive of many files, each with its own method. Both can use DEFLATE.
What does "zlib" have to do with PNG?
PNG's image data chunks, joined together, form a zlib stream, which is a short header, DEFLATE data and a checksum. The zlib format is defined in RFC 1950.
What is the "LZ77" part?
It is the back-reference idea from a 1977 paper by Lempel and Ziv: replace a repeat with a pointer to the earlier copy. The RFC cites that paper as its source for the idea.
Sources
- IETF, RFC 1951: DEFLATE Compressed Data Format Specification version 1.3. Accessed November 6, 2026.
- IETF, RFC 1950: ZLIB Compressed Data Format Specification version 3.3. Accessed November 6, 2026.
- IETF, RFC 1952: GZIP File Format Specification version 4.3. Accessed November 6, 2026.
- W3C, Portable Network Graphics (PNG) Specification (Third Edition). Accessed November 6, 2026.
- PKWARE, APPNOTE.TXT: .ZIP File Format Specification, version 6.3.10. Accessed November 6, 2026.



