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

Replace/Update a FIle in a .zip

See more Zip Examples

Demonstrates how to replace/update a file from a .zip. Note: This requires the entire .zip to be rewritten.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main() !void {
    // This requires the Chilkat Zip API to have been previously unlocked.
    // See Unlock Chilkat Zip for sample code.

    // First prepare a .zip and write it..
    const zip = try chilkat.Zip.init();
    defer zip.deinit();

    zip.newZip("qa_output/abc.zip") catch {};

    // Add some files..
    const charset = "utf-8";
    zip.addString("a.txt", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", charset) catch {};
    zip.addString("b.txt", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", charset) catch {};
    zip.addString("c.txt", "cccccccccccccccccccccccccccccccccccc", charset) catch {};

    // Write to qa_output/abc.zip
    // This .zip contains three files: a.txt, b.txt, and c.txt
    try zip.writeZipAndClose();

    // -------------------------------------------------------------------
    // Open abc.zip, replace the content of the "b.txt" entry with something else, and re-write.
    const zip2 = try chilkat.Zip.init();
    defer zip2.deinit();
    zip2.openZip("qa_output/abc.zip") catch {};

    const entry = try chilkat.ZipEntry.init();
    defer entry.deinit();
    if (zip2.entryOf("b.txt", entry)) {
        entry.replaceString("This is the new content.  bbbbbbbbbbbbbbbbbbbbbb", "utf-8") catch {};
    }

    // Write the modified .zip back to "abc.zip"
    try zip2.writeZipAndClose();

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