Sample code for 30+ languages & platforms
Zig

Iterate Through ZIP Entries Using EntryAt and ZipEntry.GetNext

See more Zip Examples

This example demonstrates how to iterate through all entries in a ZIP archive using:

  • Zip.EntryAt to obtain the first entry
  • ZipEntry.GetNext to advance through the remaining entries

The example prints each ZIP entry's stored filename and whether the entry is a file or directory.

This approach is useful when sequentially processing ZIP entries without repeatedly calling EntryAt by index.

Suppose the ZIP archive contains:

docs/
docs/readme.txt
images/logo.png
hello.txt

The example iterates through each entry in the order stored within the 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();

    var success: bool = false;

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

    // Open an existing ZIP archive.
    success = if (zip.openZip("c:/temp/example.zip")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    }

    // A ZIP archive may contain zero entries.
    if (zip.getNumEntries() == 0) {
        std.debug.print("The ZIP archive is empty.\n", .{});

        zip.closeZip();
        return;
    }

    // ------------------------------------------------------------
    // Get the first ZIP entry.
    //
    // EntryAt(0,entry) initializes the ZipEntry object so that
    // it represents the first entry in the ZIP archive.
    //
    const entry = try chilkat.ZipEntry.init();
    defer entry.deinit();

    success = if (zip.entryAt(0, entry)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    }

    // ------------------------------------------------------------
    // Iterate through all ZIP entries.
    //
    // GetNext updates the same ZipEntry object so that it
    // represents the next entry in the ZIP archive.
    //
    while (success) {
        if (entry.getIsDirectory()) {
            std.debug.print("[Directory] {s}\n", .{try entry.getFileName(alloc)});
        } else {
            std.debug.print("[File] {s}\n", .{try entry.getFileName(alloc)});
        }

        // Advance to the next entry.
        success = if (entry.getNext()) true else |_| false;
    }

    zip.closeZip();

    std.debug.print("Done.\n", .{});
}