Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust

// 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..
let zip = chilkat::Zip::new();

let _ = zip.new_zip("qa_output/abc.zip");

// Add some files..
let charset = "utf-8".to_string();
let _ = zip.add_string("a.txt", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", &charset);
let _ = zip.add_string("b.txt", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", &charset);
let _ = zip.add_string("c.txt", "cccccccccccccccccccccccccccccccccccc", &charset);

// Write to qa_output/abc.zip
// This .zip contains three files: a.txt, b.txt, and c.txt
let _ = zip.write_zip_and_close().is_ok();

// -------------------------------------------------------------------
// Open abc.zip, replace the content of the "b.txt" entry with something else, and re-write.
let zip2 = chilkat::Zip::new();
let _ = zip2.open_zip("qa_output/abc.zip");

let entry = chilkat::ZipEntry::new();
if zip2.entry_of("b.txt", &entry).is_ok() {
    let _ = entry.replace_string("This is the new content.  bbbbbbbbbbbbbbbbbbbbbb", "utf-8");
}

// Write the modified .zip back to "abc.zip"
let _ = zip2.write_zip_and_close().is_ok();

println!("success.");