Zig
Zig
Add Base64-Encoded Data to a ZIP Using AddEncoded
See more Zip Examples
This example demonstrates how to use the AddEncoded method to add encoded binary data as a file entry within a ZIP archive.
The example adds Base64-encoded data as a file named hello.txt within the ZIP archive. The Base64 text is automatically decoded to its original binary bytes before being stored in the ZIP.
This method is useful when binary data already exists in encoded textual form, such as Base64 or hex.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const zip = try chilkat.Zip.init();
defer zip.deinit();
zip.newZip("encodedData.zip") catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
// Base64 for the text: "Hello World!"
const base64_data = "SGVsbG8gV29ybGQh";
// Add the decoded bytes as "hello.txt" within the ZIP archive.
zip.addEncoded("hello.txt", "base64", base64_data) catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
// Write the ZIP archive to disk and close it.
zip.writeZipAndClose() catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
std.debug.print("ZIP archive created successfully.\n", .{});
}