# Lempel–Ziv 压缩

Lempel–Ziv 压缩是一类无损数据压缩算法，它用对同一文本先前出现位置的引用替代重复子串。压缩器把输入序列本身当作字典，因此不需要关于数据的先验知识。<sup>[1](https://didawiki.di.unipi.it/lib/exe/fetch.php/magistraleinformanianetworking/ae/ae2013/chap_11.pdf)</sup> LZ77 是该家族的开创性方案，被称为最常用的无损压缩算法。<sup>[2](https://par.nsf.gov/servlets/purl/10314275)</sup> 同样的解析在信息论中也用作个体序列的复杂度度量。<sup>[3](https://courses.cs.duke.edu/spring03/cps296.5/papers/ziv_lempel_1978_variable-rate.pdf)</sup>

| 关键事实 | 细节 |
|---|---|
| 输出 | 指向短语字典的指针；该字典在解析过程中由输入逐步构建<sup>[4](https://pages.di.unipi.it/rossano/assets/pdf/papers/SODA09.pdf)</sup> |
| LZ77 短语 | 三元组 ⟨d, ℓ, c⟩：距离 d、匹配长度 ℓ、下一字符 c<sup>[5](https://ar5iv.labs.arxiv.org/html/1707.09789)</sup> |
| LZ78 规则 | 输出字典中已有的最长前缀再加上一个新字符；字典始终保持前缀完备<sup>[1](https://didawiki.di.unipi.it/lib/exe/fetch.php/magistraleinformanianetworking/ae/ae2013/chap_11.pdf)</sup> |
| Deflate 限制 | 距离最大 32 KB，匹配长度最大 258 字节<sup>[6](https://www.rfc-editor.org/rfc/rfc1951.txt)</sup> |
| LZ4 速度 | 压缩 780 MB/s、解压 4970 MB/s 时达到 2.101 倍（v1.9.0，单核，作者基准测试）<sup>[7](https://github.com/lz4/lz4)</sup> |
| 理论 | LZ78 对每个个体序列都渐近达到有限状态可压缩性 \( p(x) \)<sup>[3](https://courses.cs.duke.edu/spring03/cps296.5/papers/ziv_lempel_1978_variable-rate.pdf)</sup> |
| 部署 | ZIP、TIFF、PNG、GIF、PDF、Unix compress、HTTP 内容编码<sup>[8](https://bibliotekanauki.pl/articles/64172284.pdf)</sup><sup> • </sup><sup>[9](https://www.hanshq.net/zip2.html)</sup><sup> • </sup><sup>[10](https://datatracker.ietf.org/doc/html/rfc9842)</sup> |

## 工作原理

字符串的解析是把它划分为若干连续短语。<sup>[11](https://mapl.cs.nycu.edu.tw/course/VC_2013/files/Lempel-Ziv_Coding.pdf)</sup> LZ77 和 LZ78 都是动态字典方法：它们用更短的码字替代子串，这些码字指向解析过程中逐步构建的短语字典。<sup>[4](https://pages.di.unipi.it/rossano/assets/pdf/papers/SODA09.pdf)</sup>

**LZ77** 的做法是从保存最近过去输出的缓冲区中进行最大长度复制；传输的码字由缓冲区地址和复制段的长度组成。<sup>[12](https://psycnet.apa.org/doi/10.1109/TIT.1977.1055714)</sup> 具体来说，编码器在过去符号的窗口内任意位置寻找与即将到来的文本的最长匹配，并发出指向匹配位置的指针以及匹配长度。<sup>[11](https://mapl.cs.nycu.edu.tw/course/VC_2013/files/Lempel-Ziv_Coding.pdf)</sup> 每个短语可以写成三元组 ⟨d, ℓ, c⟩，其中 ℓ 是匹配长度，d 是到匹配字符串的距离，c 是最后那个字面字符；因此除输入结束约定外，该短语长度为 ℓ + 1 个字符。<sup>[5](https://ar5iv.labs.arxiv.org/html/1707.09789)</sup>

**LZ78** 则维护一个显式的、不断增长的字典。解析器取剩余输入中最长的、已经是字典短语的前缀，输出二元组 ⟨id, c⟩，其中 id 标识该短语，c 是紧随其后的字符，并把新短语插入字典，因此字典始终保持前缀完备。<sup>[1](https://didawiki.di.unipi.it/lib/exe/fetch.php/magistraleinformanianetworking/ae/ae2013/chap_11.pdf)</sup> 概括地说：LZ77 在最近过去中搜索最长匹配，并传输位置、长度以及下一个字母；LZ78 则搜索整个累积字典，并传输一个序号以及下一个字母。<sup>[13](https://algo.inria.fr/seminars/sem96-97/szpankowski.html)</sup>

## 具体做法

使用 LZ77 类方法进行压缩的实践者按以下步骤操作：

1. **贪心解析。** 从左到右扫描，使每个短语尽可能长。这种贪心解析在该字符串的所有 LZ77 编码（包括滑动窗口版本）中产生最少的短语数。<sup>[5](https://ar5iv.labs.arxiv.org/html/1707.09789)</sup>
2. **寻找匹配。** deflate 压缩器（gzip 和 ZIP 中使用的 LZ77 加霍夫曼编码格式）使用以 3 字节序列为索引的链式哈希表来定位重复字符串，并采用惰性匹配，把匹配选择推迟一步以获得更好的压缩。<sup>[6](https://www.rfc-editor.org/rfc/rfc1951.txt)</sup> 距离限制为 32 KB，长度限制为 258 字节。<sup>[6](https://www.rfc-editor.org/rfc/rfc1951.txt)</sup>
3. **编码字面量和反向引用。** Deflate 随后对得到的符号应用霍夫曼编码。<sup>[6](https://www.rfc-editor.org/rfc/rfc1951.txt)</sup> Zstandard 把每个压缩块组织为字面量段和序列段，其中一个序列是先字面量复制、后匹配复制（偏移量和长度）；字面量用霍夫曼编码，字面量长度、匹配长度和偏移量符号使用 FSE——一种基于 ANS 的熵编码器。<sup>[14](https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md)</sup>
4. **或者跳过熵编码。** LZ4 是一种 LZ77 型压缩器，采用固定的面向字节格式，没有熵编码器：每个序列以一个单字节标记开头，该标记分成两个 4 位字段（字面量长度和匹配长度）；偏移量在 64 KB 窗口内为 2 字节小端序；最小匹配长度为 4；输入的最后 5 个字节始终作为字面量。<sup>[7](https://github.com/lz4/lz4)</sup>

有一点需要注意：贪心解析使短语数最少，但在 gzip 这类变长编码器中，它在输出比特数上不再是最优的。<sup>[4](https://pages.di.unipi.it/rossano/assets/pdf/papers/SODA09.pdf)</sup>

## 起源

LZ77 由[雅各布·齐夫](https://www.edgepedia.org/zh/jacob-ziv)（Jacob Ziv）和[亚伯拉罕·伦佩尔](https://www.edgepedia.org/zh/abraham-lempel)（Abraham Lempel）在《一种用于序列数据压缩的通用算法》（“A Universal Algorithm for Sequential Data Compression”）中提出，发表于 1977 年的《IEEE 信息论汇刊》（IEEE Transactions on Information Theory）。<sup>[12](https://psycnet.apa.org/doi/10.1109/TIT.1977.1055714)</sup> 其算法改编自两位作者更早的前身论文《论有限序列的复杂度》（“On the Complexity of Finite Sequences”，A. 伦佩尔与 J. 齐夫著）中的一个简单复制过程，该文于 1976 年发表在同一期刊上。<sup>[15](https://doi.org/10.1109/tit.1976.1055501)</sup> LZ78 来自 1978 年的后续论文《通过变速率编码压缩个体序列》（“Compression of Individual Sequences via Variable-Rate Coding”，齐夫与伦佩尔著），同样发表于《IEEE 信息论汇刊》。<sup>[16](https://doi.org/10.1109/tit.1978.1055934)</sup> 齐夫还在同一期刊上单独发表了 1978 年的配套论文《个体序列的编码定理》（“Coding theorems for individual sequences”）。<sup>[17](https://doi.org/10.1109/tit.1978.1055911)</sup> LZW 出现在韦尔奇（Welch）1984 年发表于《计算机》（Computer）期刊的论文《一种高性能数据压缩技术》（“A Technique for High-Performance Data Compression”）中。<sup>[18](https://doi.org/10.1109/mc.1984.1659158)</sup> 后来的变体包括：LZ-End，来自塞巴斯蒂安·克雷夫特（Sebastian Kreft）和贡萨洛·纳瓦罗（Gonzalo Navarro）2012 年发表于《理论计算机科学》（Theoretical Computer Science）的工作<sup>[19](https://doi.org/10.1016/j.tcs.2012.02.006)</sup>；高度有界的 Lempel-Ziv 编码，由希德奥·班奈（Hideo Bannai）、米茨鲁·船越（Mitsuru Funakoshi）、迪普塔拉马·亨德里安（Diptarama Hendrian）、缪吉·松田（Myuji Matsuda）和西蒙·J·普利西（Simon J. Puglisi）于 2024 年发表在 ESA 2024 的 LIPIcs 会议录中<sup>[20](https://doi.org/10.4230/lipics.esa.2024.18)</sup>；以及 Brotli，由 J. 阿拉库伊亚拉（J. Alakuijala）和 Z. 萨巴德卡（Z. Szabadka）于 2016 年作为 RFC 7932 发布<sup>[21](https://doi.org/10.17487/rfc7932)</sup>。2004 年，LZ77 被列为 IEEE 里程碑；2021 年，雅各布·齐夫因其在 LZ77 和 LZ78 上的工作获得 IEEE 荣誉奖章。<sup>[22](https://ar5iv.labs.arxiv.org/html/2409.12146)</sup>

## 变体

- **LZSS** 用二元组替代 LZ77 的三元组：每个输出项要么是单个字符，要么是一个（偏移量，长度）指针，从而在没有找到匹配时避免浪费一个整数；它常被与 LZ77 混淆。<sup>[1](https://didawiki.di.unipi.it/lib/exe/fetch.php/magistraleinformanianetworking/ae/ae2013/chap_11.pdf)</sup>
- **LZW** 通过用所有输入字母表符号预初始化字典，从 LZ78 的输出中去掉了显式字面量；最初的方案使用 12 位指针，允许在字典变为静态之前有 4096 个字典条目。<sup>[23](https://tuxtina.de/files/seminar/LempelZivReport.pdf)</sup> 它的后代包括 PKZip 的 Shrink 方法和 Unix compress 程序，并用于 GIF。<sup>[9](https://www.hanshq.net/zip2.html)</sup>
- **Deflate** 把 LZ77 与[霍夫曼编码](https://www.edgepedia.org/zh/huffman-coding)结合起来。<sup>[6](https://www.rfc-editor.org/rfc/rfc1951.txt)</sup>
- **Zstandard** 把一种 LZ77 变体与霍夫曼编码和 FSE 熵编码搭配使用。<sup>[14](https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md)</sup>
- **LZ4** 完全省略熵编码器，窗口为 64 KB。<sup>[7](https://github.com/lz4/lz4)</sup>
- **Brotli** 是较晚出现的 LZ 家族格式。<sup>[21](https://doi.org/10.17487/rfc7932)</sup>
- 研究变体包括 **LZ-End**<sup>[19](https://doi.org/10.1016/j.tcs.2012.02.006)</sup> 和高度有界的 Lempel-Ziv 编码。<sup>[20](https://doi.org/10.4230/lipics.esa.2024.18)</sup> 2024 年还出现了首个亚线性时间的 LZ77 分解，对于二进制字母表运行时间为 \( O(n/\sqrt{\log n}) \)。<sup>[22](https://ar5iv.labs.arxiv.org/html/2409.12146)</sup>
- 字典大小已远远超出 deflate 的 32 KB：[7-Zip](https://www.edgepedia.org/zh/7-zip) 25.00 中的 LZMA2 可达 3840 MB，[WinRAR](https://www.edgepedia.org/zh/winrar) 7.0 中可达 64 GB，不过大字典只对大文件有帮助。<sup>[8](https://bibliotekanauki.pl/articles/64172284.pdf)</sup>

## 应用

Deflate 对英文文本的压缩比为 2.5 到 3，对可执行文件略低，对光栅图像往往高得多；一本教科书的估计认为，Lempel–Ziv 方法对典型文本文件可压缩约 33%。<sup>[6](https://www.rfc-editor.org/rfc/rfc1951.txt)</sup><sup> • </sup><sup>[1](https://didawiki.di.unipi.it/lib/exe/fetch.php/magistraleinformanianetworking/ae/ae2013/chap_11.pdf)</sup> 按粗略的速度区间划分，LZMA 衍生物、bzip2 和 brotli 的压缩速度低于 10 MB/s；deflate/gzip 和 zstd 位于 10–500 MB/s 区间；lzo、lz4 和 snappy 的压缩与解压速度都在约 1 GB/s 及以上。<sup>[24](https://morotti.github.io/lzbench-web/)</sup>

部署随格式而定：deflate 用于 ZIP、TIFF 和 PNG；<sup>[8](https://bibliotekanauki.pl/articles/64172284.pdf)</sup> LZW 留存于 TIFF、GIF 和 PDF 中，<sup>[8](https://bibliotekanauki.pl/articles/64172284.pdf)</sup> 而 Unix compress 成为 Unix 上的事实标准压缩器；<sup>[9](https://www.hanshq.net/zip2.html)</sup> HTTP 现在把 zstd 和 brotli 作为内容编码传输，RFC 9842（2025）新增共享字典传输，规定把先前的 HTTP 响应用作 Brotli 和 Zstandard 的外部字典，并注册 'dcb' 和 'dcz' 内容编码。<sup>[10](https://datatracker.ietf.org/doc/html/rfc9842)</sup>

1978 年的论文定义了个体序列的有限状态可压缩性 \( p(x) \)，并将其归一化为 \( 0 < p(x) < 1 \)，还通过 1977 年算法的一个变体证明了一种渐近最优的通用编码，其对每个个体序列的压缩率都趋于 \( p(x) \)。<sup>[3](https://courses.cs.duke.edu/spring03/cps296.5/papers/ziv_lempel_1978_variable-rate.pdf)</sup> 1977 年的论文同样表明，其通用码在事先不知道信源的情况下，一致地逼近为完全指定的信源所设计的码所能达到的下界。<sup>[12](https://psycnet.apa.org/doi/10.1109/TIT.1977.1055714)</sup> 作者们把可压缩性描述为扮演着类似于经典信息论中熵的角色，而经典信息论处理的是概率集合而非个体序列。<sup>[3](https://courses.cs.duke.edu/spring03/cps296.5/papers/ziv_lempel_1978_variable-rate.pdf)</sup> 在编码之外，LZ77 度量在对数因子范围内等价于语法压缩、LZ-End、游程 BWT、字符串吸引子和子串复杂度；这些度量中有许多难以优化（NP 难），而 LZ77 可以在线性时间内构造。<sup>[22](https://ar5iv.labs.arxiv.org/html/2409.12146)</sup> LZ 惩罚施加一个与生成文本的边际 LZ 压缩量成正比的 logit 更新，消除了自回归语言模型中的退化重复；在那些模型中，频率惩罚和重复惩罚仍会留下高达约 4% 的重复率。<sup>[25](https://arxiv.org/html/2504.20131v4)</sup>

## 局限与替代方案

- **不可压缩数据。** 已经压缩过的文件，如 ZIP、MP3 或 JPG，无法再被有意义地压缩，因此对它们进行文件系统压缩没有意义。<sup>[8](https://bibliotekanauki.pl/articles/64172284.pdf)</sup> 最坏情况下的膨胀是有界的，但非零：deflate 每 32 KB 块膨胀 5 字节，约 0.015%，<sup>[6](https://www.rfc-editor.org/rfc/rfc1951.txt)</sup> 而不可压缩的 LZ4 块膨胀 0.4%，其格式还把可达到的压缩比上限限制在约 250。<sup>[7](https://github.com/lz4/lz4)</sup>
- **字典增长。** LZ78 的字典无界增长；补救办法包括在满时冻结、丢弃并重新开始，或采用 LRU 之类的替换方案。<sup>[23](https://tuxtina.de/files/seminar/LempelZivReport.pdf)</sup>
- **错误传播。** 1977 年的作者指出，该算法的主要缺点是容易在信道错误下发生错误传播。<sup>[12](https://psycnet.apa.org/doi/10.1109/TIT.1977.1055714)</sup>
- **小文件。** 冷字典会限制短输入上的收益；LZ4 通过字典压缩解决这一点，使用任何提供的输入文件的最后 64 KB，并可与 Zstandard 字典构建器结合使用。<sup>[7](https://github.com/lz4/lz4)</sup>
- **替代方案。** LZSS 通常比 LZW 压缩得更好，除非文件主要由空字节和短重复模式构成，此时 LZW 更便宜的指针会占优。<sup>[26](https://u.cs.biu.ac.il/~wisemay/lzm.pdf)</sup> 基于 Burrows–Wheeler 变换的 bzip2 通常比 deflate 压缩得更好，但明显更慢。<sup>[8](https://bibliotekanauki.pl/articles/64172284.pdf)</sup> PPM 统计压缩器 PPMC 在几乎所有测试用例的压缩率上都胜过最好的 Lempel–Ziv 变体，但需要多得多的资源。<sup>[23](https://tuxtina.de/files/seminar/LempelZivReport.pdf)</sup> 无界 LZ77 窗口在 HTML 等数据上比 gzip（其有界 LZ77 窗口无法利用长距离重复）和 bzip2（使用不同的压缩方法）带来显著收益，这些数据在网页和文本集合中表现出许多长距离重复。<sup>[4](https://pages.di.unipi.it/rossano/assets/pdf/papers/SODA09.pdf)</sup>

## References

1. [Dictionary-based compressors (course chapter, Università di Pisa)](https://didawiki.di.unipi.it/lib/exe/fetch.php/magistraleinformanianetworking/ae/ae2013/chap_11.pdf)
2. [NSF PAR manuscript stating LZ77's prevalence](https://par.nsf.gov/servlets/purl/10314275)
3. [Compression of Individual Sequences via Variable-Rate Coding (Ziv & Lempel, 1978)](https://courses.cs.duke.edu/spring03/cps296.5/papers/ziv_lempel_1978_variable-rate.pdf)
4. [On the bit-complexity of Lempel-Ziv compression (Ferragina, Nitto, Venturini, SODA 2009)](https://pages.di.unipi.it/rossano/assets/pdf/papers/SODA09.pdf)
5. [Relations Between Greedy and Bit-Optimal LZ77 Encodings](https://ar5iv.labs.arxiv.org/html/1707.09789)
6. [RFC 1951: DEFLATE Compressed Data Format Specification version 1.3](https://www.rfc-editor.org/rfc/rfc1951.txt)
7. [LZ4, Extremely fast compression algorithm (official repository and block format doc)](https://github.com/lz4/lz4)
8. [A Review of Classical Lossless Data Compression Algorithms and Their Contemporary Relevance (Biuletyn WAT, 2025)](https://bibliotekanauki.pl/articles/64172284.pdf)
9. [Shrink, Reduce, and Implode: The Legacy Zip Compression Methods](https://www.hanshq.net/zip2.html)
10. [RFC 9842 - Compression Dictionary Transport](https://datatracker.ietf.org/doc/html/rfc9842)
11. [Elements of Information Theory, Section 13.4.1 Sliding Window Lempel–Ziv Algorithm (Cover & Thomas)](https://mapl.cs.nycu.edu.tw/course/VC_2013/files/Lempel-Ziv_Coding.pdf)
12. [A Universal Algorithm for Sequential Data Compression (Ziv & Lempel, IEEE Trans. Inf. Theory, 1977), DOI record](https://psycnet.apa.org/doi/10.1109/TIT.1977.1055714)
13. [Towards Analytical Information Theory: Recent Results on Lempel-Ziv Data Compression Schemes (Szpankowski, INRIA seminar)](https://algo.inria.fr/seminars/sem96-97/szpankowski.html)
14. [Zstandard Compression Format specification](https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md)
15. [A. Lempel, J. Ziv (1976). On the Complexity of Finite Sequences. IEEE Transactions on Information Theory.](https://doi.org/10.1109/tit.1976.1055501)
16. [J. Ziv, A. Lempel (1978). Compression of individual sequences via variable-rate coding. IEEE Transactions on Information Theory.](https://doi.org/10.1109/tit.1978.1055934)
17. [J. Ziv (1978). Coding theorems for individual sequences. IEEE Transactions on Information Theory.](https://doi.org/10.1109/tit.1978.1055911)
18. [Welch (1984). A Technique for High-Performance Data Compression. Computer.](https://doi.org/10.1109/mc.1984.1659158)
19. [Sebastian Kreft, Gonzalo Navarro (2012). On compressing and indexing repetitive sequences. Theoretical Computer Science.](https://doi.org/10.1016/j.tcs.2012.02.006)
20. [Bannai, Hideo and colleagues (2024). Height-Bounded Lempel-Ziv Encodings. arXiv (Cornell University).](https://doi.org/10.4230/lipics.esa.2024.18)
21. [J. Alakuijala, Z. Szabadka (2016). Brotli Compressed Data Format. .](https://doi.org/10.17487/rfc7932)
22. [Lempel–Ziv (LZ77) Factorization in Sublinear Time](https://ar5iv.labs.arxiv.org/html/2409.12146)
23. [The Lempel Ziv Algorithm (seminar report)](https://tuxtina.de/files/seminar/LempelZivReport.pdf)
24. [lzbench Compression Benchmark (web version)](https://morotti.github.io/lzbench-web/)
25. [Lempel-Ziv Penalty: An information-theoretic repetition penalty for autoregressive language models](https://arxiv.org/html/2504.20131v4)
26. [The Relative Efficiency of LZW and LZSS (Yair Wiseman, Bar-Ilan University)](https://u.cs.biu.ac.il/~wisemay/lzm.pdf)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Numerical, string, and geometric algorithms › String algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/lempel-ziv-compression

English original: https://www.edgepedia.org/lempel-ziv-compression
