Language: C
Compression / Archiving
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.
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.
sudo apt install liblz4-devbrew install lz4Download precompiled binaries or build from source: https://github.com/lz4/lz4LZ4 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.
lz4 input.txt output.lz4Compresses `input.txt` into `output.lz4` using LZ4's command-line tool.
lz4 -d output.lz4 restored.txtDecompresses `output.lz4` back to `restored.txt`.
#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;
}Compresses a string in memory using LZ4 C API and prints the compressed size.
#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;
}Decompresses data in memory and prints the original string.
// LZ4 provides streaming API using LZ4_stream_t and LZ4_streamDecode_t for large data streams.
// See documentation for detailed usage.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.