> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How ZIP Compression Works

> Explains how ZIP DEFLATE compression uses LZ77 dictionary substitution and Huffman entropy coding to losslessly reduce file size and reconstruct original data

How can a one‑gigabyte folder become a 400‑megabyte ZIP file and then expand back to the original data when you unzip it? Where did those 600 megabytes go?

In short: lossless compression finds shorter representations for repeated patterns without losing information. ZIP archives (specifically files using the DEFLATE method) combine dictionary substitution with entropy coding to represent common patterns and symbols using fewer bits. The rest of this article explains the idea with a simple example, then connects that example to how ZIP files store and recover the data.

## 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

Instead of storing every letter with the same 8‑bit code, we can assign shorter codes to frequent symbols and longer codes to rare ones. One valid assignment is:

* A → 0
* B → 10
* N → 11

Encoding "banana" with those codes yields:
`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":

1. Count symbol frequencies:
   * `B`: 1
   * `N`: 2
   * `A`: 3

2. Build the Huffman tree by repeatedly joining the two least‑frequent items:
   * Combine `B (1)` and `N (2)` → node weight `3`.
   * Now you have `A (3)` and `(B+N) (3)`.
   * Combine `A` and `(B+N)` → root node with weight `6`.

3. Label left branches `0` and right branches `1`. The resulting codes:
   * `A` → `0`
   * `B` → `10`
   * `N` → `11`

This yields a 1‑bit code for the most frequent symbol and 2‑bit codes for the others, drastically cutting the bit count for this tiny example.

## 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` → `B`
  * `0` → `A`
  * `11` → `N`
  * `0` → `A`
  * `11` → `N`
  * reconstructs `BANANA`

ZIP files store either the tree or an equivalent code table inside the archive, so the decompressor knows how to interpret the compressed bitstream.

![A dark-themed diagram of a file labeled "banana.zip" with a row of binary digit boxes and a dashed inset showing a Huffman coding tree. The tree maps letters (A, B, N) to bit codes, illustrating data compression.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Advanced-Concepts/How-ZIP-Compression-Works/banana-zip-huffman-tree-diagram.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=38f2b4fd1a44a2aea368b602b91f9eee)

## 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.

Putting the two together:

1. LZ77 reduces redundancy by converting repeated substrings into pointer tokens.
2. Huffman coding then compresses the stream of bytes and pointer tokens, assigning shorter codes to frequent items.

This combination is powerful because LZ77 captures repeated patterns at multiple scales, and Huffman minimizes the bit cost of the resulting symbol stream.

## Quick comparison

| Component | Purpose | Example / Output |
| - | - | - |
| LZ77 (dictionary) | Replace repeated substrings with references to prior data | `{length: 5, distance: 20}` tokens |
| Huffman (entropy) | Assign shorter bit codes to frequent symbols | `A → 0`, `B → 10`, `N → 11` |
| DEFLATE (ZIP) | Combine LZ77 + Huffman for efficient, lossless compression | `.zip` / `.gz` files using RFC 1951 |

## 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](https://datatracker.ietf.org/doc/html/rfc1951)
* Huffman coding: [https://en.wikipedia.org/wiki/Huffman\_coding](https://en.wikipedia.org/wiki/Huffman_coding)
* LZ77 algorithm: [https://en.wikipedia.org/wiki/LZ77](https://en.wikipedia.org/wiki/LZ77)
* ZIP file format: [https://en.wikipedia.org/wiki/ZIP\_(file\_format)](https://en.wikipedia.org/wiki/ZIP_\(file_format\))

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/5de176de-dc0d-4961-bac5-a735e7d70eda/lesson/1fc0990e-91c6-4217-a06e-f9333939d8a5)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.