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

Unzip an AES Encrypted Text File directly into a String Variable

See more Zip Examples

A common need is to unzip from an AES encrypted Zip archive directly into a string variable, such that the unencrypted file never resides on disk, even temporarily. This example shows how to do it.

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();

    var success: bool = false;

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

    const zip = try chilkat.Zip.init();
    defer zip.deinit();

    // This example opens a WinZip-compatible AES encrypted
    // .zip that contains a single file: hamlet.xml.

    // It decrypts and unzips hamlet.xml directly into a string
    // variable.
ERROR: Method not in the Zig package: Zip.SetPassword
    zip.ERROR();
    success = if (zip.openZip("qa_data/hamlet.zip")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    }

    const entry = try chilkat.ZipEntry.init();
    defer entry.deinit();
    success = zip.entryOf("hamlet.xml", entry);
    if (!success) {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    }

    // lineEndingBehavior:
    // 0 = leave unchanged.
    // 1 = convert all to bare LF's
    // 2 = convert all to CRLF's
    const line_ending_behavior = 0;
    const src_charset = "utf-8";
    const xml_text = try entry.unzipToString(alloc, line_ending_behavior, src_charset);
    std.debug.print("{s}\n", .{xml_text});
}