Unofficial Zig bindings for LZ4.
- LZ4 & LZ4Frame Compression
- LZ4 & LZ4Frame Decompression
- Encoder (based on LZ4Frame)
- Decoder (based on LZ4Frame)
- Add dependency to
build.zig.zon
zig fetch --save https://github.com/SnorlaxAssist/zig-lz4/archive/refs/heads/master.tar.gz
- Add module in
build.zig
const ziglz4 = b.dependency("zig-lz4", .{
.target = target,
.optimize = optimize,
});
exe.addModule("lz4", ziglz4.module("zig-lz4"));
File Encoder
const std = @import("std");
const allocator = ...;
var encoder = try Encoder.init(allocator);
_ = encoder.setLevel(4)
.setContentChecksum(Frame.ContentChecksum.Enabled)
.setBlockMode(Frame.BlockMode.Independent);
defer encoder.deinit();
const input = "Sample Text";
// Compress input
const compressed = try encoder.compress(input);
defer allocator.free(compressed);
// Allocate memory for the output file with space for the size hint
const fileOutput = try allocator.alloc(u8, compressed.len + 4);
defer allocator.free(fileOutput);
// Add size hint to the header of the file
const sizeHintHeader : [4]u8 = @bitCast(@as(u32, @intCast(input.len)));
// Write the size hint and the compressed data to the file output
@memcpy(fileOutput[0..4], sizeHintHeader[0..4]);
// Write the compressed data to the file output
@memcpy(fileOutput[4..][0..compressed.len], compressed[0..]);
// Write the compressed file to workspace
try std.fs.cwd().writeFile("compressedFile", fileOutput);
File Decoder
const std = @import("std");
const allocator = ...;
var decoder = try Decoder.init(allocator);
defer decoder.deinit();
// Read the compressed file
const fileInput = try std.fs.cwd().readFileAlloc(allocator, "compressedFile", std.math.maxInt(usize));
defer allocator.free(fileInput);
// Read the size hint from the header of the file
const sizeHint = std.mem.bytesAsSlice(u32, fileInput[0..4])[0];
// Decompress the compressed data
const decompressed = try decoder.decompress(fileInput[4..], sizeHint);
defer allocator.free(decompressed);
std.debug.print("Decompressed: {s}\n", .{decompressed});
Simple Compression & Decompression
const std = @import("std");
const allocator = ...;
const input = "Sample Text";
// Compression
const compressed = try Standard.compress(allocator, input);
defer allocator.free(compressed);
std.debug.print("Compressed: {s}\n", .{compressed});
// Decompression
const decompressed = try Standard.decompress(allocator, compressed, input.len);
defer allocator.free(decompressed);
std.debug.print("Decompressed: {s}\n", .{decompressed});