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

Zip and Keep Open

See more Zip Examples

Zip a directory tree by calling WriteZip instead of WriteZipAndClose. When WriteZip is called, the created zip is automatically opened and the zip object contains references to the compressed zip entries (mapped entries) contained within the .zip.

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 zip = try chilkat.Zip.init();
    defer zip.deinit();

    const zip_path = "c:/temp/myFiles.zip";

    // Initialize the zip object, which also sets the FileName property to the path of the zip to be created.
    zip.newZip(zip_path) catch {};

    // Append references to files to be zipped.
    const recurse = true;
    zip.appendFiles("c:/temp/files_to_zip/*", recurse) catch {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    };

    // Write the .zip, but don't close the zip file.
    // The zip file we just created is automatically opened and the zip object
    // now contains entries that are contained within the zip.  (They are memory-mapped entries)
    zip.writeZip() catch {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Successfully created {s}\n", .{zip_path});

    // Let's look at what's in the .zip we just created..

    const num_entries = zip.getNumEntries();

    const entry = try chilkat.ZipEntry.init();
    defer entry.deinit();

    var i: i32 = 0;
    while (i < num_entries) {
        zip.entryAt(i, entry) catch {};
        if (entry.getIsDirectory()) {
            std.debug.print("{d}: {s} (directory)\n", .{ i, try entry.getFileName(alloc) });
        } else {
            std.debug.print("{d}: {s}\n", .{ i, try entry.getFileName(alloc) });
        }

        i = i + 1;
    }

    // Close the zip file when finished.
    zip.closeZip();
}