Skip to content

snappy.zigUltra-Fast Native Snappy Compression for Zig

Pure Zig implementation of Google's Snappy compression format targeting Zig 0.17.0. Zero C dependencies, zero overhead, memory-safe.

Installation ​

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

bash
# 1. Stable Release (Tagged)
zig fetch --save https://github.com/muhammad-fiaz/snappy.zig/archive/refs/tags/0.0.1.tar.gz

# 2. Nightly / Latest Development URL
zig fetch --save git+https://github.com/muhammad-fiaz/snappy.zig.git

Then expose the module in your build.zig:

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

Quick Example ​

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

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

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

    const raw_data = "Snappy is a compression/decompression library aimed at very high speeds!";

    // One-shot framed compression (standard .sz stream format)
    const compressed = try snappy.compress(allocator, raw_data);
    defer allocator.free(compressed);

    // One-shot decompression (handles framed streams and raw blocks automatically)
    const restored = try snappy.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.