Zig Requires Chilkat v11.0.0+
Zig
Iterate Over ZIP Entries Using EntryAt
This example demonstrates how to use the EntryAt method to iterate over all entries contained within a ZIP archive.
The EntryAt method retrieves a ZipEntry object for a given zero-based index.
This is useful for:
- Enumerating all entries in a ZIP archive
- Inspecting filenames, sizes, and timestamps
- Searching or filtering ZIP contents
Suppose the ZIP archive contains:
docs/readme.txt
images/logo.png
data/config.json The example loops through all ZIP entries and prints information about each entry.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Open an existing ZIP archive.
const zip = try chilkat.Zip.init();
defer zip.deinit();
zip.openZip("example.zip") catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
// Get the total number of entries in the ZIP archive.
const num_entries = zip.getNumEntries();
std.debug.print("Number of ZIP entries = {d}\n", .{num_entries});
// Create a ZipEntry object that will be reused
// for each entry retrieved by EntryAt.
const entry = try chilkat.ZipEntry.init();
defer entry.deinit();
// Iterate over all ZIP entries.
var i: i32 = 0;
while (i < num_entries) {
// Retrieve the entry at index i.
zip.entryAt(i, entry) catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
std.debug.print("Entry {d}\n", .{i});
std.debug.print(" FileName: {s}\n", .{try entry.getFileName(alloc)});
std.debug.print(" Uncompressed Length: {d}\n", .{entry.getUncompressedLength()});
std.debug.print(" Compressed Length: {d}\n", .{entry.getCompressedLength()});
std.debug.print("\n", .{});
i = i + 1;
}
// Sample output:
//
// Entry 0
// FileName: docs/readme.txt
// Uncompressed Length: 1204
// Compressed Length: 512
//
// Entry 1
// FileName: images/logo.png
// Uncompressed Length: 84211
// Compressed Length: 84102
//
zip.closeZip();
std.debug.print("Done.\n", .{});
}