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

Unzip Selected Files from a Zip Archive

See more Zip Examples

Demonstrates how to iterate over the files contained within a .zip and unzip specific files.

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.openZip("my_files.zip") catch {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    };

    const unzip_dir = "/temp/unzipDir";

    // Get the number of files and directories in the .zip
    const n = zip.getNumEntries();

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

    var i: i32 = 0;
    while (i < n) {
        zip.entryAt(i, entry) catch {};
        if (!entry.getIsDirectory()) {
            // (the filename may include a path)
            std.debug.print("{s}\n", .{try entry.getFileName(alloc)});

            // Your application may choose to unzip this entry
            // based on the filename.
            // If the entry should be unzipped, then call Extract(unzipDir)
            entry.extract(unzip_dir) catch {
                std.debug.print("{s}\n", .{try entry.getLastErrorText(alloc)});
                return;
            };
        }

        i = i + 1;
    }
}