LZ4

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.

Installation

linux: sudo apt install liblz4-dev
mac: brew install lz4
windows: Download precompiled binaries or build from source: https://github.com/lz4/lz4

Usage

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.

Compress a file using CLI

lz4 input.txt output.lz4

Compresses `input.txt` into `output.lz4` using LZ4's command-line tool.

Decompress a file using CLI

lz4 -d output.lz4 restored.txt

Decompresses `output.lz4` back to `restored.txt`.

Using LZ4 library in C to compress memory

#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.

Decompressing memory with LZ4

#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.

Streaming compression

// LZ4 provides streaming API using LZ4_stream_t and LZ4_streamDecode_t for large data streams.
// See documentation for detailed usage.

Error Handling

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.