Skip to content

lz4.zigUltra-Fast Native LZ4 Compression for Zig

Pure Zig implementation of the LZ4 ecosystem targeting Zig 0.17.0. Zero C dependencies, zero overhead, memory-safe.

Installation ​

Add lz4.zig to your build.zig.zon:

bash
# Stable Release
zig fetch --save https://github.com/muhammad-fiaz/lz4.zig/archive/refs/tags/0.0.1.tar.gz

# Latest / Dev / Nightly
zig fetch --save git+https://github.com/muhammad-fiaz/lz4.zig.git

Then expose the module in your build.zig:

zig
const lz4_dep = b.dependency("lz4", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("lz4", lz4_dep.module("lz4"));

Quick Example ​

Compress and decompress data in just a few lines of idiomatic Zig:

zig
const std = @import("std");
const lz4 = @import("lz4");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const raw_data = "LZ4 is an extremely fast lossless compression algorithm!";

    // One-shot compression to a standard LZ4 frame
    const compressed = try lz4.compress(allocator, raw_data);
    defer allocator.free(compressed);

    // One-shot decompression (handles frames and blocks automatically)
    const restored = try lz4.decompress(allocator, compressed);
    defer allocator.free(restored);

    std.debug.print("Original size:   {d} bytes\n", .{raw_data.len});
    std.debug.print("Compressed size: {d} bytes\n", .{compressed.len});
    std.debug.print("Restored:        {s}\n", .{restored});
}

Released under the MIT License.