Skip to main content
ExplainerData CompressionExplainer· 4 min read· in Guides

The (Offset, Length, Next Character) Triple: How the LZ77 Sliding Window Achieves Lossless Data Compression

By treating recently processed data as a dynamic dictionary, the LZ77 algorithm shrinks files by replacing repeated sequences with a simple mathematical back-reference. This sliding window mechanism remains the foundation of modern lossless compression formats like ZIP and PNG.

By Paige Carter

Algorithm Designers 40%Hardware Engineers 30%Web Standards Bodies 30%
Algorithm Designers
Prioritize maximizing compression ratios by utilizing larger sliding windows and exhaustive match-finding heuristics.
Hardware Engineers
Focus on memory constraints and decompression speed, favoring bounded window sizes that fit efficiently into CPU caches.
Web Standards Bodies
Prioritize universal compatibility and real-time streaming, ensuring decompression requires minimal processing power.

Perspectives this story doesn't cover

  • Information Theorists
  • Storage Hardware Manufacturers

Common questions

What does LZ77 stand for?

LZ77 is named after its creators, Abraham Lempel and Jacob Ziv, and the year they published the algorithm, 1977.

Why is it called a sliding window?

The algorithm maintains a fixed-size buffer of recently processed data. As it reads new data, this buffer 'slides' forward, dropping the oldest characters to make room for the newest ones.

Is LZ77 compression lossless?

Yes. LZ77 is strictly lossless, meaning the decompressed data is a mathematically perfect, bit-for-bit identical copy of the original input.

How is LZ77 different from a ZIP file?

LZ77 is the core pattern-matching engine. A ZIP file uses the DEFLATE algorithm, which first runs LZ77 to find repeating patterns, and then applies Huffman coding to compress the resulting triples.

The short answer

  1. The LZ77 algorithm compresses data by replacing repeated sequences with a mathematical reference to a previous occurrence.
  2. It uses a sliding window divided into a search buffer of past data and a look-ahead buffer of upcoming data.
  3. Matches are encoded as a triple specifying the offset distance, the match length, and the next literal character.
  4. Decompression is exceptionally fast because the decoder simply copies bytes from its own output buffer without searching.
  5. The DEFLATE algorithm combines LZ77 with Huffman coding to power ubiquitous formats like ZIP, gzip, and PNG.

The exact moment a file shrinks during lossless compression happens at the boundary between what the algorithm has already seen and what it is about to read. In the LZ77 algorithm, this step is the longest prefix match. If you want to understand how ZIP files or PNG images actually save space, this is the mechanism to learn. As the encoder scans a file, it constantly looks at the upcoming data in its look-ahead buffer and searches backward through its search buffer—a sliding window of recently processed text. When it finds an identical sequence of characters in that past window, the outcome is determined: instead of writing the raw data again, the algorithm emits a short mathematical reference pointing backward. That single matching decision collapses megabytes of redundant data into a fraction of their original size.[1]

Before Abraham Lempel and Jacob Ziv published their seminal paper in 1977, data compression often relied on assigning shorter codes to frequent individual symbols, a method known as Huffman coding. LZ77 attacked redundancy from a completely different angle. It recognized that human language, computer code, and structured data are full of repeated phrases. By treating the recently processed data as a dynamic dictionary, LZ77 eliminated the need to store a separate codebook. The algorithm simply slides a window forward, continuously asking whether the upcoming bytes have appeared recently.[1]

When a match is found, LZ77 encodes it using a specific three-part data structure known as the (Offset, Length, Next Character) triple. As the Wikipedia reference notes, this pair is equivalent to the instruction that "each of the next length characters is equal to the characters exactly distance characters behind it" in the uncompressed stream. The "offset" (or distance) dictates exactly how many bytes backward the decoder must look. The "length" specifies how many consecutive bytes to copy. Finally, the "next character" provides the first literal byte that breaks the pattern, ensuring the algorithm can continue processing new data.[1]

The sliding window searches backward to find the longest matching sequence of upcoming characters.

Consider a simple string like "abcabcabcabc". A naive storage method requires 12 bytes to hold the text. However, an LZ77 encoder processes the first "abc" as literal characters, outputting triples with an offset and length of zero. When it reaches the second "abc", the longest prefix match step recognizes that this exact sequence exists three bytes back. It emits a triple instructing the decoder to go back 3 bytes and copy 3 bytes forward. This mechanism allows the algorithm to reconstruct the entire 12-byte sequence using only a handful of back-references, drastically reducing the storage footprint.[3]

However, an LZ77 encoder processes the first "abc" as literal characters, outputting triples with an offset and length of zero.

The elegance of the LZ77 triple lies in its asymmetry: encoding is computationally heavy, but decoding is nearly instantaneous. The encoder must perform exhaustive searches or maintain complex hash chains to find the longest possible match within the sliding window. The decoder, however, performs no searching at all. As outlined in the DEFLATE specification, the decompression algorithm simply reads the offset and length, walks backward in its own output buffer, and copies the bytes forward. This is why software installations and web page loads can decompress data faster than a hard drive or network connection can deliver it.[2]

The size of the sliding window strictly dictates the algorithm's memory footprint and its compression ratio. A larger window allows the encoder to look further back in time to find matches, increasing the likelihood of replacing long strings with short triples. However, searching a massive window requires more RAM and CPU cycles. The DEFLATE algorithm, which pairs LZ77 with Huffman coding to power formats like gzip and PNG, standardized a 32-kilobyte sliding window and a maximum match length of 258 bytes. This specific constraint was chosen to balance compression efficiency with the limited memory available on 1990s hardware.[2][4]

The DEFLATE specification standardized strict window and length limits to ensure decompression could run on memory-constrained hardware.

While the original 1977 specification output a strict stream of triples, modern implementations optimize the format to save even more space. Algorithms like LZSS introduced a 1-bit flag to distinguish between literal characters and length-distance pairs, dropping the mandatory "next character" from the triple when it isn't needed. When combined with a secondary pass of Huffman coding—which assigns shorter bit sequences to the most common offsets and lengths—the resulting DEFLATE stream achieves the dense compression ratios that make the modern internet viable.[1][2]

Nearly fifty years after its invention, the sliding window mechanism remains the undisputed foundation of general-purpose lossless compression. While newer algorithms like Google's Brotli and Facebook's Zstandard have introduced larger windows, advanced context modeling, and asymmetric dictionaries, they still rely on the core LZ77 principle. The next major leap in compression ratio will not come from abandoning the sliding window, but from hardware accelerators built directly into CPUs that can search gigabyte-sized buffers without stalling the processor pipeline.[1]

Jargon, explained

Sliding Window
A fixed-size buffer of recently processed data that the algorithm uses as a dynamic dictionary to find repeating patterns.
Look-ahead Buffer
The portion of the uncompressed input data that the algorithm is currently analyzing to find a match in the sliding window.
Offset
The distance backward from the current position to the start of a matching sequence in the sliding window.
DEFLATE
A widely used compression algorithm that combines the LZ77 sliding window technique with Huffman coding.
Lossless Compression
A data reduction method that allows the exact original data to be perfectly reconstructed from the compressed file.

Sources

Source coverage

4 outlets

3 viewpoints surfaced

Algorithm Designers 40%Hardware Engineers 30%Web Standards Bodies 30%
  1. [1]WikipediaAlgorithm Designers

    LZ77 and LZ78

    Read on Wikipedia
  2. [2]RFC EditorHardware Engineers

    DEFLATE Compressed Data Format Specification version 1.3

    Read on RFC Editor
  3. [3]Factlen Editorial Team

    Synthesis by Factlen editorial team

    Read on Factlen Editorial Team
  4. [4]W3CWeb Standards Bodies

    Portable Network Graphics (PNG) Specification

    Read on W3C

Comments

Stay informed

Every angle. Every day.

Get Guides stories with full source coverage and perspective breakdowns delivered to your inbox.