Sample code for 30+ languages & platforms
Zig

Gzip Compress In Memory and Base64 Encode

See more Gzip Examples

Demonstrates how to Gzip compress in-memory data and then encode the compressed data to base64.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const gzip = try chilkat.Gzip.init();
    defer gzip.deinit();

    // This example will load a file into the fileData object.
    // Your application might load fileData from other sources..
    const file_data = try chilkat.BinData.init();
    defer file_data.deinit();
    file_data.loadFile("qa_data/xml/hamlet.xml") catch {
        std.debug.print("Failed to load file.\n", .{});
        return;
    };

    // In-place compress the contents of fileData
    gzip.compressBd(file_data) catch {
        std.debug.print("{s}\n", .{try gzip.getLastErrorText(alloc)});
        return;
    };

    // Get the base64 encoded compressed data (in a single line).
    const str_base64 = try file_data.getEncoded(alloc, "base64");
    std.debug.print("{s}\n", .{str_base64});
    std.debug.print("--------\n", .{});

    // To get the base64 in multiple lines, as it might appear in MIME,
    // use "base64-mime".
    const str_base64_multi_line = try file_data.getEncoded(alloc, "base64-mime");
    std.debug.print("{s}\n", .{str_base64_multi_line});
}