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

Remove an Entry from an Existing ZIP Using DeleteEntry

See more Zip Examples

This example demonstrates how to use the DeleteEntry method to remove a file from an existing ZIP archive.

The example:

  • Creates a ZIP archive containing three text files
  • Opens the ZIP archive for modification
  • Finds and deletes one entry
  • Writes the modified ZIP archive to a new filename

Suppose the original ZIP archive contains:

a.txt
b.txt
c.txt

After deleting b.txt, the modified ZIP archive contains:

a.txt
c.txt

The entry is removed only from the in-memory ZIP object until a Write* method is called.

Chilkat Rust Downloads

Rust

// ------------------------------------------------------------
// First create a ZIP archive containing three text files.

let zip = chilkat::Zip::new();

if zip.new_zip("original.zip").is_err() {
    println!("{}", zip.last_error_text());
    return;
}

let charset = "utf-8".to_string();

if zip.add_string("a.txt", "Contents of file A", &charset).is_err() {
    println!("{}", zip.last_error_text());
    return;
}

if zip.add_string("b.txt", "Contents of file B", &charset).is_err() {
    println!("{}", zip.last_error_text());
    return;
}

if zip.add_string("c.txt", "Contents of file C", &charset).is_err() {
    println!("{}", zip.last_error_text());
    return;
}

// Write the ZIP archive to disk.
// 
// The ZIP now contains:
// 
//     a.txt
//     b.txt
//     c.txt
// 
if zip.write_zip_and_close().is_err() {
    println!("{}", zip.last_error_text());
    return;
}

// ------------------------------------------------------------
// Open the existing ZIP archive for modification.

let zip2 = chilkat::Zip::new();

if zip2.open_zip("original.zip").is_err() {
    println!("{}", zip2.last_error_text());
    return;
}

// Find the entry named "b.txt".
let entry = chilkat::ZipEntry::new();

if zip2.entry_of("b.txt", &entry).is_err() {
    println!("{}", zip2.last_error_text());
    return;
}

// Remove the entry from the in-memory ZIP object.
// 
// At this point, the original ZIP file on disk is unchanged.
// The deletion takes effect only after WriteZip or
// WriteZipAndClose is called.
if zip2.delete_entry(&entry).is_err() {
    println!("{}", zip2.last_error_text());
    return;
}

// Write the modified ZIP archive to a new file.
zip2.set_file_name("modified.zip");

if zip2.write_zip_and_close().is_err() {
    println!("{}", zip2.last_error_text());
    return;
}

// The modified ZIP now contains:
// 
//     a.txt
//     c.txt
// 

println!("ZIP archive updated successfully.");