Intuition: compressing the word “banana”
Consider the six‑letter word: banana A typical computer stores each ASCII character using 8 bits, so naively this string takes 6 × 8 = 48 bits. But the letters are not equally frequent:- A appears 3 times
- N appears 2 times
- B appears 1 time
- A → 0
- B → 10
- N → 11
10 0 11 0 11 0
That sequence is 9 bits total (not 48) — we represented the same information more compactly by using variable‑length codes optimized for symbol frequency.
How to choose the codes: Huffman coding
Huffman coding is a standard algorithm that builds an optimal set of prefix‑free, variable‑length codes from symbol frequencies. Step‑by‑step for “banana”:-
Count symbol frequencies:
B: 1N: 2A: 3
-
Build the Huffman tree by repeatedly joining the two least‑frequent items:
- Combine
B (1)andN (2)→ node weight3. - Now you have
A (3)and(B+N) (3). - Combine
Aand(B+N)→ root node with weight6.
- Combine
-
Label left branches
0and right branches1. The resulting codes:A→0B→10N→11
Decoding unambiguously
Because Huffman codes are prefix‑free (no code is the prefix of another), a decoder reading the bitstream from left to right can unambiguously traverse the Huffman tree:- Start at the root; read bits and follow left/right children until reaching a leaf node—output that symbol, then return to the root for the next bits.
- Example decode of
100110110:1 0→B0→A11→N0→A11→N- reconstructs
BANANA

How ZIP/DEFLATE actually compresses data
Real ZIP files (when using the common DEFLATE method) do two things together:- LZ77-style dictionary substitution: detect repeated substrings and replace later occurrences with references (length and distance) to earlier occurrences.
- Huffman (entropy) coding: encode the symbols that represent raw bytes and the LZ77 references using variable‑length codes optimized for frequency.
- LZ77 reduces redundancy by converting repeated substrings into pointer tokens.
- Huffman coding then compresses the stream of bytes and pointer tokens, assigning shorter codes to frequent items.
Quick comparison
Practical notes and caveats
- Overhead for small inputs: very small files or short strings may not compress well because the archive must include headers and code tables. Metadata can outweigh savings.
- Compressibility depends on redundancy: highly redundant data (repeated text, logs, large homogeneous files) compresses much better than already‑compressed or high‑entropy data (e.g., JPEG, MP3, encrypted files).
- Implementation details matter: block sizes, whether static or dynamic Huffman tables are used, and how aggressively the LZ77 matcher looks for matches all affect compression ratio and CPU usage.
- ZIP stores sufficient code and metadata so decompression reproduces the original bytes exactly (lossless compression).
ZIP compression (DEFLATE) is lossless. It first replaces repeated substrings using LZ77-style references, then applies Huffman coding to the symbols and references. The archive contains the necessary tables so the exact original bytes can be reconstructed.
Further reading and references
- DEFLATE (RFC 1951): https://datatracker.ietf.org/doc/html/rfc1951
- Huffman coding: https://en.wikipedia.org/wiki/Huffman_coding
- LZ77 algorithm: https://en.wikipedia.org/wiki/LZ77
- ZIP file format: https://en.wikipedia.org/wiki/ZIP_(file_format)