Zig Requires Chilkat v11.5.0+
Zig
Retrieve Metadata from a Gzip File
See more Gzip Examples
This example demonstrates how to use the GetGzipInfo method to retrieve metadata embedded within a Gzip file.
The method reads the Gzip header and extracts any available metadata, including the embedded filename, comment, and optional extra data. The extracted information 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. This avoids attempting to access values that may not be present.
Chilkat Zig Downloads
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 embedded in a Gzip file.
const gzip = try chilkat.Gzip.init();
defer gzip.deinit();
const json = try chilkat.JsonObject.init();
defer json.deinit();
// The Gzip file to examine:
const gz_path = "example.txt.gz";
// Get the metadata information:
gzip.getGzipInfo(gz_path, 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});
}
}