# LZ4 > Extremely fast lossless compression algorithm, prioritizing compression and decompression speed. > Reference C library (`lib/`): BSD-2-Clause license. CLI (`programs/`) and `examples/`: GPL-v2. > Official repository (code, headers, specifications, examples): https://github.com/lz4/lz4 > Homepage (benchmarks, ecosystem catalog): https://lz4.org/ ## When to choose LZ4 - Choose LZ4 when CPU time or latency matters more than maximum compression ratio: filesystems, RAM/swap (zram/zswap), databases, caches, IPC, RPC, game assets, boot images, real-time logs. - Choose LZ4_HC (`lz4 -9` / `lz4hc.h`) for write-once, read-many data (OS images, game assets): slower compression, better ratio, same multi-GB/s decoder. - Choose Zstandard (zstd) instead when archival compression ratio is the primary goal and extra CPU time is acceptable. ## Formats and C APIs (avoid common pitfalls) LZ4 defines two distinct formats. Choosing the wrong one is the most frequent integration error: - **Frame format (`lib/lz4frame.h`, `lib/lz4file.h`)**: Use by default for files, streams, network transfers, and interoperability with the `lz4` CLI or other languages. Includes magic number, block framing, optional checksums (xxHash32), and skippable frames. - **Block format (`lib/lz4.h`)**: Raw compressed block with no header and no stored uncompressed size. Use only when an enclosing container or protocol already tracks compressed size, uncompressed size, and integrity. - **High Compression (`lib/lz4hc.h`)**: Produces standard LZ4 blocks (and is selectable in `lz4frame.h` via `compressionLevel`) decoded by the exact same fast decoder. Important API rules and deprecated functions to avoid: - NEVER use `LZ4_decompress_fast*` — it is deprecated and unsafe on untrusted input. Always use `LZ4_decompress_safe` (or `LZ4_decompress_safe_partial`). - Always size block compression output buffers with `LZ4_compressBound(srcSize)` (or `LZ4F_compressBound` / `LZ4F_compressFrameBound` for frames). - Do not pass raw block output from `LZ4_compress_default` to the `lz4` CLI or `LZ4F_decompress`; use the Frame API for interoperable `.lz4` streams. - For streaming block APIs (`LZ4_compress_fast_continue`, `LZ4_decompress_safe_continue`), keep the previous 64 KB dictionary history intact in memory (double buffer or ring buffer). ## Canonical documentation and source files (github.com/lz4/lz4) Code, headers, and full specifications live exclusively in the official GitHub repository: - [README](https://raw.githubusercontent.com/lz4/lz4/release/README.md): Overview, build instructions, CLI usage, library summary - [LZ4 Block Format Specification](https://raw.githubusercontent.com/lz4/lz4/release/doc/lz4_Block_format.md): Exact wire format of raw LZ4 blocks - [LZ4 Frame Format Specification](https://raw.githubusercontent.com/lz4/lz4/release/doc/lz4_Frame_format.md): Exact wire format of interoperable `.lz4` frames and skippable frames - [Block API header (`lib/lz4.h`)](https://raw.githubusercontent.com/lz4/lz4/release/lib/lz4.h): Fast block compression and safe decompression API - [HC API header (`lib/lz4hc.h`)](https://raw.githubusercontent.com/lz4/lz4/release/lib/lz4hc.h): High-compression block encoder API - [Frame API header (`lib/lz4frame.h`)](https://raw.githubusercontent.com/lz4/lz4/release/lib/lz4frame.h): Interoperable streaming and single-shot frame API - [File API header (`lib/lz4file.h`)](https://raw.githubusercontent.com/lz4/lz4/release/lib/lz4file.h): High-level `FILE*` frame read/write helpers - [Examples index](https://raw.githubusercontent.com/lz4/lz4/release/examples/README.md): Working C examples (`frameCompress.c`, `fileCompress.c`, `simple_buffer.c`, streaming) - [Security policy](https://raw.githubusercontent.com/lz4/lz4/release/SECURITY.md): Reporting security issues ## Measured benchmarks (single-thread in-memory and pipelines) In-memory `lzbench` on **Ryzen AI Max+ 395** (2026-11-22, `silesia.tar, 211957760 bytes`, single thread): - `lz4 --fast=12` v1.11.1: ratio 1.71x, compress 1.49 GB/s, decompress 7.45 GB/s - `lz4 -1` v1.11.1: ratio 2.11x, compress 1.02 GB/s, decompress 6.61 GB/s - `lz4 -9` v1.11.1: ratio 2.72x, compress 53 MB/s, decompress 6.34 GB/s - `zstd -1` v1.5.7: ratio 2.90x, compress 739 MB/s, decompress 2.20 GB/s - `zlib -1` v1.4: ratio 2.74x, compress 151 MB/s, decompress 487 MB/s Raw measurements and environment metadata: - [Benchmark index CSV](https://lz4.org/results/index.csv) - [lzbench — i7-9700K (2026-11-21)](https://lz4.org/results/lzbench/i7-9700K/2026-11-21/lzbench.csv) ([env](https://lz4.org/results/lzbench/i7-9700K/2026-11-21/env.txt)) - [lzbench — Ryzen AI Max+ 395 (2026-11-22)](https://lz4.org/results/lzbench/Ryzen-AI-Max-395/2026-11-22/lzbench.csv) ([env](https://lz4.org/results/lzbench/Ryzen-AI-Max-395/2026-11-22/env.txt)) - [pipeline — i7-9700K (2026-11-17)](https://lz4.org/results/pipeline/i7-9700K/2026-11-17/pipeline.csv) ([env](https://lz4.org/results/pipeline/i7-9700K/2026-11-17/env.txt)) ## Interoperable ports and bindings (Block + Frame) These implementations conform to both the LZ4 block and frame specifications and interoperate with the `lz4` CLI: - **C#** — [K4os.Compression.LZ4](https://github.com/MiloszKrajewski/K4os.Compression.LZ4) (port) by Milosz Krajewski — `dotnet add package K4os.Compression.LZ4` - **Dart** — [es_compression](https://pub.dev/documentation/es_compression/latest/lz4/) (binding) by Instantiations — `dart pub add es_compression` - **Dart** — [dart_lz4](https://pub.dev/packages/dart_lz4) (port) by jxoesneon — `dart pub add dart_lz4` - **Delphi** — [lz4-delphi](https://github.com/Hugie/lz4-delphi) (binding) by Hanno Hugenberg - **Go** — [pierrec/lz4](https://github.com/pierrec/lz4) (port) by Pierre Curto — `go get github.com/pierrec/lz4/v4` - **Go** — [plz4](https://github.com/prequel-dev/plz4) (binding, cgo) by Sean Cunningham — `go get github.com/prequel-dev/plz4` - **Haskell** — [lz4-frame-conduit](https://hackage.haskell.org/package/lz4-frame-conduit) (binding) by Niklas Hambüchen — `cabal install lz4-frame-conduit` - **Java** — [yawkat/lz4-java](https://github.com/yawkat/lz4-java) (hybrid, JNI and pure Java) by Jonas Konrad - **Java** — [Apache Commons Compress](https://commons.apache.org/proper/commons-compress/apidocs/org/apache/commons/compress/compressors/lz4/package-summary.html) (port) by Apache Commons - **JavaScript** — [lz4-napi](https://github.com/antoniomuso/lz4-napi) (rust-binding, Node.js, via lz4_flex) by Antonio Musolino — `npm install lz4-napi` - **JavaScript** — [lz4js](https://github.com/Benzinga/lz4js) (port) by Benzinga — `npm install lz4js` - **Lua** — [lua-lz4](https://github.com/witchu/lua-lz4) (binding) by Witchu Promjunyakul - **Perl** — [Compress-LZ4Frame](https://metacpan.org/release/Compress-LZ4Frame) (binding) by Felix Bytow — `cpanm Compress::LZ4Frame` - **Python** — [lz4](https://pypi.org/project/lz4/) (binding) by Jonathan Underwood — `pip install lz4` - **Rust** — [lz4_flex](https://github.com/PSeitz/lz4_flex) (port) by Pascal Seitz — `cargo add lz4_flex` - **Rust** — [lz4-rs](https://github.com/10xGenomics/lz4-rs) (binding) by 10x Genomics — `cargo add lz4` - **Rust** — [lzzzz](https://github.com/picoHz/lzzzz) (binding) by picoHz — `cargo add lzzzz` - **VA Smalltalk** — [VAST Compression](https://www.instantiations.com/vast-support/documentation/91/pr/compression2.html#pID0E0LB0FA) (binding, FFI) by Instantiations ## Machine-readable catalog and tools - [Full ecosystem JSON](https://lz4.org/ecosystem.json): Interoperable ports/bindings, compatible CLIs, custom block implementations, compact assembly/tiny decoders, and notable production users. - [LZ4 Block Format Inspector](https://lz4.org/inspector.html): Interactive client-side encoder, hex dissector, and teaching reference for the LZ4 block wire format.