How to use lz4
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- LZ4 achieves decompression speeds of over 400 MB/s per core.
- Compression speed is typically around 100 MB/s.
- LZ4 is designed for speed, prioritizing decompression speed.
- It is often used in scenarios where decompression speed is critical, such as real-time data processing.
- LZ4 is available as a standalone command-line tool and a C library.
What is LZ4?
LZ4 is a high-performance, lossless data compression algorithm developed by Yann Collet. It is renowned for its exceptional speed, particularly in decompression. Unlike some other compression algorithms that prioritize achieving the smallest possible file size (high compression ratio), LZ4 strikes a balance, offering a good compression ratio while being significantly faster than many alternatives. This makes it an excellent choice for applications where fast data handling is crucial, such as in real-time systems, databases, file systems, and network transfers.
How to Use LZ4 - Command Line Interface (CLI)
The most straightforward way to use LZ4 is through its command-line interface. This tool is typically available on Linux and macOS systems, and can be installed on Windows. The basic syntax for compression and decompression is as follows:
Compressing a File
To compress a file named input.txt, you would use the following command:
lz4 input.txt output.lz4
This command reads input.txt, compresses it using the LZ4 algorithm, and writes the compressed data to output.lz4. By default, LZ4 uses a block-based compression strategy.
Decompressing a File
To decompress the file output.lz4 back into its original form, you would use:
lz4 -d output.lz4 input.txt
The -d flag indicates decompression. The second argument, input.txt, specifies the name for the decompressed output file. If you omit the output filename, LZ4 will typically decompress to standard output, which you can then redirect to a file.
lz4 -d output.lz4 > input.txt
Viewing Compressed File Contents (without decompressing)
Sometimes, you might want to see if a file is compressed or get some basic information about it. LZ4 provides a way to do this:
lz4 -v output.lz4
The -v (verbose) flag provides information about the compression, including the original and compressed sizes, and the compression ratio.
Other Useful CLI Options
-for--force: Overwrite output files without asking.-zor--compress: Force compression (useful if the input might be already compressed or if you want to ensure compression).-kor--keep: Keep source files after operation (default behavior is to delete source file after successful compression/decompression).-cor--stdout: Write to standard output and read from standard input. This is very useful for piping data.-B: Set the LZ4 block size to 2^n bytes (e.g.,-B7uses 128 KB blocks). Larger blocks can sometimes improve compression ratio at the cost of speed and memory.--fast=: Set the acceleration factor for the LZ4_fast compression level (default is 1). Higher values can speed up compression but reduce the ratio.--ultra=: Set the LZ4_ultra compression level (default is 0). Higher values increase compression ratio significantly but drastically reduce speed.
Piping with LZ4
LZ4's ability to read from standard input and write to standard output makes it excellent for use in shell pipelines. For example, to compress the output of another command:
some_command | lz4 > output.lz4
And to decompress and pipe to another command:
lz4 -dc output.lz4 | another_command
Note the -dc flags for decompression (-d) and writing to standard output (-c).
Using LZ4 in Applications (Library)
For developers, LZ4 provides a highly efficient C library that can be integrated into applications. This library offers functions for both streaming and block-based compression and decompression.
Key Library Functions
LZ4_compress_default(): Compresses a block of data.LZ4_decompress_safe(): Decompresses a block of data safely, preventing buffer overflows.LZ4_createStreamHC()andLZ4_freeStreamHC(): For creating and freeing high-compression streaming contexts.LZ4_compress_HC_continue(): Compresses data in chunks for streaming.
Many programming languages have bindings for the LZ4 C library, allowing developers to use LZ4's speed directly within their code without needing to call external processes. Examples include Python (python-lz4), Java (lz4-java), and Node.js (lz4).
When to Use LZ4
LZ4 is an excellent choice in the following scenarios:
- Real-time data processing: When data needs to be compressed and decompressed very quickly, such as in game development, live streaming, or high-frequency trading.
- Databases and file systems: To reduce storage space without significantly impacting read/write performance.
- Network transfers: Compressing data before sending it over a network to reduce bandwidth usage and latency, especially when decompression speed is critical on the receiving end.
- In-memory compression: When you need to reduce the memory footprint of data structures without incurring a large performance penalty.
- Situations where decompression speed is paramount: If the bottleneck is reading compressed data, LZ4's rapid decompression is a major advantage.
Limitations of LZ4
While LZ4 is very fast, it generally does not achieve the same level of compression as algorithms like Gzip, Bzip2, or Zstandard (at its highest levels). If your primary goal is to minimize file size at all costs, and speed is a secondary concern, you might consider other algorithms. However, for many common use cases, the speed benefits of LZ4 outweigh its slightly lower compression ratio.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- LZ4 - Extremely Fast Compression algorithmCC0-1.0
- LZ4 - WikipediaCC-BY-SA-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.