Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.5.0+

Retrieve Metadata from Gzip Data in Memory (BinData)

This example demonstrates how to use the GetGzipInfoBd method to retrieve metadata from Gzip data stored in memory within a BinData object.

The Gzip data is first loaded into the BinData instance. The GetGzipInfoBd method then extracts any available metadata from the Gzip header, including the embedded filename, comment, and optional extra data.

The metadata is returned in a JsonObject. Because these fields are optional, the example checks for the existence of each JSON member using HasMember before retrieving its value.

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 demonstrates how to retrieve metadata from Gzip data
    // stored in a BinData object.

    const gzip = try chilkat.Gzip.init();
    defer gzip.deinit();
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    // Load a Gzip file into BinData:
    bd.loadFile("example.txt.gz") catch {
        std.debug.print("{s}\n", .{try bd.getLastErrorText(alloc)});
        return;
    };

    // Get the metadata information from the in-memory Gzip data:
    gzip.getGzipInfoBd(bd, json) catch {
        std.debug.print("{s}\n", .{try gzip.getLastErrorText(alloc)});
        return;
    };

    // Output the JSON containing metadata:
    std.debug.print("Gzip metadata JSON:\n", .{});
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Access individual fields only if they exist:

    if (json.hasMember("filename")) {
        const filename = try json.stringOf(alloc, "filename");
        std.debug.print("Filename: {s}\n", .{filename});
    }

    if (json.hasMember("comment")) {
        const comment = try json.stringOf(alloc, "comment");
        std.debug.print("Comment: {s}\n", .{comment});
    }

    if (json.hasMember("extraData")) {
        const extra_data = try json.stringOf(alloc, "extraData");
        std.debug.print("ExtraData (Base64): {s}\n", .{extra_data});
    }
}