What it is
LZ4 is an extremely fast lossless compression algorithm and library. It focuses on compression and decompression speed while maintaining reasonable compression ratios, making it ideal for real-time applications and large data streams.
LZ4 allows compressing and decompressing data either via command-line tools or C library functions. It supports stream compression, block compression, and integration with memory buffers for high-performance applications.
Installation
sudo apt install liblz4-devGetting started
The smallest useful thing you can do with it, and what each part means.
lz4 input.txt output.lz4lz4 -d output.lz4 restored.txtAdvanced usage
Where the library earns its place over a simpler alternative.
#include <lz4.h>
#include <stdio.h>
#include <string.h>
int main() {
const char* input = "This is some text to compress.";
int inputSize = strlen(input) + 1;
char compressed[256];
int compressedSize = LZ4_compress_default(input, compressed, inputSize, sizeof(compressed));
if (compressedSize <= 0) {
printf("Compression failed\n");
return 1;
}
printf("Compressed size: %d\n", compressedSize);
return 0;
}#include <lz4.h>
#include <stdio.h>
#include <string.h>
int main() {
const char* original = "Hello, LZ4!";
int originalSize = strlen(original) + 1;
char compressed[256];
int compressedSize = LZ4_compress_default(original, compressed, originalSize, sizeof(compressed));
char decompressed[256];
int decompressedSize = LZ4_decompress_safe(compressed, decompressed, compressedSize, sizeof(decompressed));
if (decompressedSize < 0) {
printf("Decompression failed\n");
return 1;
}
printf("Decompressed: %s\n", decompressed);
return 0;
}// LZ4 provides streaming API using LZ4_stream_t and LZ4_streamDecode_t for large data streams.
// See documentation for detailed usage.Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- LZ4_compress_default returned <= 0
- Compression failed. Ensure output buffer is large enough and input data is valid.
- LZ4_decompress_safe returned < 0
- Decompression failed. Verify the compressed data is not corrupted and buffer sizes are correct.
Best practices
- Use LZ4 when compression/decompression speed is more important than maximum compression ratio.
- Always check return values of library functions for errors.
- Use streaming API for large or continuous data streams.
- Pre-allocate buffers large enough to hold worst-case compressed data.
- Combine with other tools like zstd for layered compression if needed.
Background
Why it exists, and what it was reacting to.
LZ4 was developed by Yann Collet in 2011 to provide high-speed compression with minimal CPU overhead. It is widely used in databases, game engines, file systems, and network protocols where fast compression is critical.
