Skip to content

lzma2.zigNative LZMA2 Compression for Zig

Production-ready native Zig LZMA2 and LZMA compression library.

Installation ​

lzma2.zig is production-ready. The recommended method for applications and downstream libraries is to pin to the tagged stable release:

bash
# Recommended: Tagged stable release (0.0.1)
zig fetch --save https://github.com/muhammad-fiaz/lzma2.zig/archive/refs/tags/0.0.1.tar.gz

# Alternative: Latest development build (main branch)
zig fetch --save git+https://github.com/muhammad-fiaz/lzma2.zig.git

Then expose the module in your build.zig:

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

Quick Example ​

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

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

pub fn main() !void {
    const allocator = std.heap.page_allocator;

    const payload = "Native LZMA2 compression in pure Zig 0.17.0! High-ratio dictionary encoding.";

    // One-shot heap compression
    const compressed = try lzma2.compress(allocator, payload, .{});
    defer allocator.free(compressed);

    // One-shot heap decompression
    const restored = try lzma2.decompress(allocator, compressed, .{});
    defer allocator.free(restored);

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

Released under the MIT License.