LZ4

极快压缩算法。(Extremely Fast Compression algorithm)

Github stars Tracking Chart

LZ4 -- 极快的压缩

LZ4 是一种无损压缩算法,每核提供大于 500MB/s 的压缩速度,可通过多核 CPU 扩展。它的特点是解码器速度极快,每核速度为多 GB/s,通常可达到多核系统的 RAM 速度极限。

速度可以动态调整,选择一个 "加速" 因子,以压缩比换取更快的速度。在另一端,还提供了一个高压缩衍生物 LZ4_HC,以 CPU 时间换取压缩比的提高。所有版本都具有相同的解压速度。

LZ4 还兼容字典压缩,包括 API 和 CLI 级别。它可以摄取任何输入文件作为字典,尽管只使用最后的 64KB。这一功能可以与 Zstandard Dictionary Builder 结合使用,以大幅提高小文件的压缩性能。

LZ4 库作为开源软件提供,使用 BSD 2-Clause 许可。

Branch Status
master Build Status Build status coverity
dev Build StatusBuild status

分支政策

  • "master "分支在任何时候都被认为是稳定的。
  • 在 "dev" 分支,所有的贡献都必须在升级到 master 之前被合并。
    • 如果你打算提出一个补丁,请提交到 "dev" 分支,或者它自己的特性分支。不允许直接提交到 "master "分支。

基准测试

基准测试使用的是 @inikep 用 GCC v8.2.0 在 Linux 64-bits(Ubuntu 4.18.0-17)上编译的 lzbench。参考系统使用 Core i7-9700K CPU @ 4.9GHz (w/ turbo boost)。基准测试评估了单线程模式下参考 Silesia 语料库的压缩情况。

Compressor 压缩率 压缩 解压缩
memcpy 1.000 13700 MB/s 13700 MB/s
LZ4 default (v1.9.0) 2.101 780 MB/s 4970 MB/s
LZO 2.09 2.108 670 MB/s 860 MB/s
QuickLZ 1.5.0 2.238 575 MB/s 780 MB/s
Snappy 1.1.4 2.091 565 MB/s 1950 MB/s
Zstandard 1.4.0 -1 2.883 515 MB/s 1380 MB/s
LZF v3.6 2.073 415 MB/s 910 MB/s
zlib deflate 1.2.11 -1 2.730 100 MB/s 415 MB/s
LZ4 HC -9 (v1.9.0) 2.721 41 MB/s 4900 MB/s
zlib deflate 1.2.11 -6 3.099 36 MB/s 445 MB/s

LZ4 还兼容并优化了 x32 模式,为其提供了额外的速度性能。

安装方法

make
make install # 这个命令可能需要root权限。

LZ4 的 Makefile 支持标准的 Makefile 约定,包括分步安装重定向命令重新定义。它与并行构建(-j#)兼容。

构建 LZ4 -- 使用 vcpkg

你可以使用 vcpkg 依赖管理器下载并安装 LZ4。

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install lz4

vcpkg 中的 LZ4 port 是由微软团队成员和社区贡献者更新的。如果版本过期,请在 vcpkg 仓库中创建一个问题或拉取请求

文档

原始的 LZ4 块压缩格式在 lz4_Block_format 中详细介绍。

为了满足流媒体的要求,任意长的文件或数据流会使用多个块进行压缩,这些块会被组织成一帧,定义在 lz4_Frame_format 中。LZ4 的互操作版本也必须尊重框架格式。

其他源版本

除了 C 语言参考源之外,许多贡献者已经用多种语言(Java、C#、Python、Perl、Ruby 等)创建了 lz4 的版本。在 LZ4 主页上有一个已知的源码移植列表。


Main metrics

Overview
Name With Ownerlz4/lz4
Primary LanguageC
Program languageMakefile (Language Count: 10)
PlatformBSD, Linux, SmartOS, Unix-like, Windows
License:Other
所有者活动
Created At2014-03-25 15:52:21
Pushed At2025-05-06 05:03:25
Last Commit At
Release Count33
Last Release Namev1.10.0 (Posted on )
First Release Namer116 (Posted on )
用户参与
Stargazers Count10.9k
Watchers Count250
Fork Count1.4k
Commits Count3.7k
Has Issues Enabled
Issues Count580
Issue Open Count31
Pull Requests Count809
Pull Requests Open Count14
Pull Requests Close Count184
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

LZ4 - Extremely fast compression

LZ4 is lossless compression algorithm,
providing compression speed > 500 MB/s per core,
scalable with multi-cores CPU.
It features an extremely fast decoder,
with speed in multiple GB/s per core,
typically reaching RAM speed limits on multi-core systems.

Speed can be tuned dynamically, selecting an "acceleration" factor
which trades compression ratio for faster speed.
On the other end, a high compression derivative, LZ4_HC, is also provided,
trading CPU time for improved compression ratio.
All versions feature the same decompression speed.

LZ4 is also compatible with dictionary compression,
both at API and CLI levels.
It can ingest any input file as dictionary, though only the final 64KB are used.
This capability can be combined with the Zstandard Dictionary Builder,
in order to drastically improve compression performance on small files.

LZ4 library is provided as open-source software using BSD 2-Clause license.