Sample code for 30+ languages & platforms
Zig

Zip a Directory Tree

See more Zip Examples

Demonstrates how to zip an entire directory tree into a .zip archive.

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

    zip.newZip("test.zip") catch {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    };

    // Append a directory tree.  The call to AppendFiles does
    // not read the file contents or append them to the zip
    // object in memory.  It simply appends references
    // to the files so that when WriteZip or WriteZipAndClose
    // is called, the referenced files are streamed and compressed
    // into the .zip output file.

    const recurse = true;
    zip.appendFiles("c:/temp/a/*", recurse) catch {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    };

    zip.writeZipAndClose() catch {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Zip Created!\n", .{});
}