Understanding LZ77 Compression Algorithm

LZ77 (Lempel-Ziv 1977) is a lossless data compression algorithm developed by Abraham Lempel and Jacob Ziv in 1977. It operates on the principle of dictionary-based compression (or “sliding window compression”), replacing repeated sequences of data with references to earlier occurrences of the same sequence. LZ77 is the foundation of many popular compression formats (e.g., DEFLATE, ZIP, PNG, GIF) and is valued for its simplicity, speed, and effectiveness on data with repeated patterns.

Core Working Principle

LZ77 compresses data by scanning the input stream with a sliding window that divides the data into two regions:

  1. History Buffer (Sliding Window): A fixed-size window containing the most recently processed data (the “dictionary” of previously seen sequences).
  2. Lookahead Buffer: A small buffer containing the data to be compressed next (the sequence currently being analyzed).

The algorithm works in iterative steps:

Step 1: Scan for Matches

For the current position in the lookahead buffer, search the history buffer for the longest matching sequence (a consecutive series of bytes that appears in both the history and lookahead buffers).

Step 2: Encode the Match (or Literal)

  • If a match is found: Encode it as a triplet (or pair, in simplified variants) containing:
    • Offset: The distance (in bytes) from the current position to the start of the matching sequence in the history buffer.
    • Length: The number of bytes in the matching sequence.
    • Next Byte: The first byte in the lookahead buffer that follows the matched sequence (to start the next search).
  • If no match is found (or the match is shorter than a single byte): Encode the current byte as a literal (raw byte value) and move one byte forward in the lookahead buffer.

Step 3: Slide the Window

After encoding, move the sliding window forward by the length of the matched sequence (or 1 byte for a literal), updating the history buffer with the newly processed data and refilling the lookahead buffer with new input data.

Example of LZ77 Compression

Suppose the input data is: ABABABAABABAA

  • History buffer: Empty (initial state)
  • Lookahead bufferABABABAABABAA
  1. First two bytes (AB) are literals (no history to match).
  2. Next byte (A) matches the A at offset 2 (distance 2), length 1 → encode (2,1,B) (offset=2, length=1, next byte=B).
  3. Next sequence (ABA) matches the ABA at offset 4 (distance 4), length 3 → encode (4,3,A) (offset=4, length=3, next byte=A).
  4. Remaining bytes are encoded similarly, replacing repeated AB sequences with offset/length references.

The compressed output replaces redundant sequences with compact references, reducing overall size.

Key Parameters

The performance of LZ77 depends on two critical parameters:

ParameterDefinitionImpact on Compression
History Buffer SizeThe maximum number of bytes stored in the history (e.g., 32 KB, 64 KB).Larger buffers improve compression (more chance to find long matches) but increase memory usage and search time.
Lookahead Buffer SizeThe number of bytes scanned for matches (e.g., 256 bytes).Larger lookahead buffers allow longer matches but slow down the search process.

Most practical implementations (e.g., DEFLATE) use fixed buffer sizes:

  • History buffer: 32 KB (max offset = 32768 bytes)
  • Lookahead buffer: 258 bytes (max match length = 258 bytes)

LZ77 Variants & Derivatives

LZ77 is the basis for many widely used compression algorithms and formats:

1. LZ78 (Lempel-Ziv 1978)

A sibling algorithm that builds an explicit dictionary of all seen sequences (instead of a sliding window). Used in GIF, LZW (Lempel-Ziv-Welch), and Unix compress.

2. DEFLATE

A hybrid algorithm combining LZ77 with Huffman coding (entropy encoding). DEFLATE is used in ZIP, PNG, gzip, and HTTP compression (GZIP). It optimizes LZ77 output by further compressing offset/length pairs with Huffman codes.

3. LZSS (Lempel-Ziv-Storer-Szymanski)

A simplified LZ77 variant that omits the “next byte” in match triplets (only uses offset/length for matches, literals for single bytes). Used in disk compression (e.g., DOS DoubleSpace) and game ROM compression.

4. LZMA (Lempel-Ziv-Markov chain Algorithm)

A high-compression variant of LZ77 with a larger history buffer (up to 4 GB) and Markov chain entropy encoding. Used in 7-Zip, xz, and Windows ESD files.

Advantages of LZ77

  1. Lossless Compression: Preserves all original data (no information loss), making it suitable for text, executables, and critical files.
  2. Speed: Fast encoding and decoding (especially with small buffer sizes) compared to other lossless algorithms (e.g., LZMA).
  3. Effectiveness on Repeated Data: Excels at compressing data with frequent repeats (e.g., text documents, log files, firmware, images with patterns).
  4. Simple Implementation: Easy to code in hardware or software, with minimal overhead for embedded systems.
  5. Streaming Support: Processes data in a single pass (no need to read the entire input first), ideal for real-time compression (e.g., network streams).

Limitations of LZ77

  1. Poor Compression on Random Data: No benefit for data with no repeated patterns (e.g., encrypted files, random binary data)—may even increase size slightly due to overhead.
  2. Buffer Size Tradeoff: Larger buffers improve compression but require more memory and slower search times (a problem for low-resource devices).
  3. No Global Dictionary: The sliding window only references recent data, so long-range repeats (beyond the history buffer) are not compressed.
  4. Overhead for Short Matches: Encoding offset/length pairs for very short matches (e.g., 2 bytes) may add overhead compared to storing the literal bytes.

Typical Application Scenarios

  • File Compression: ZIP, gzip, and 7-Zip (via DEFLATE/LZMA) use LZ77 for compressing documents, archives, and executables.
  • Image Formats: PNG (DEFLATE) and GIF (LZW, a derivative) use LZ77-based compression for lossless image storage.
  • Networking: HTTP/HTTPS (GZIP compression), SSH, and VPNs use LZ77 to reduce bandwidth usage for data transmission.
  • Embedded Systems: Firmware, IoT devices, and game cartridges use lightweight LZ77 variants (e.g., LZSS) to save storage space.
  • Database & Logs: Compressing repetitive database records or server logs to reduce disk usage.

LZ77 vs. Other Lossless Compression Algorithms

AlgorithmKey DifferenceCompression RatioSpeedUse Case
LZ77Sliding window, local dictionaryModerateFastGeneral-purpose compression (ZIP, PNG)
LZMALarger window + entropy codingHighSlowHigh-compression archives (7-Zip)
Huffman CodingEntropy encoding (no dictionary)LowVery fastAuxiliary compression (DEFLATE, JPEG)
BrotliLZ77 variant with context modelingHighFastModern web compression (HTTP/2)



了解 Ruigu Electronic 的更多信息

订阅后即可通过电子邮件收到最新文章。

Posted in

Leave a comment