Sample code for 30+ languages & platforms
Rust

Encrypt Already Existing Zip

See more Zip Examples

To encrypt an already existing non-encrypted .zip, the application must open the .zip, set the encryption related properties, and then re-write.

Chilkat Rust Downloads

Rust

// This requires the Chilkat Zip API to have been previously unlocked.
// See Unlock Chilkat Zip for sample code.

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

// Open an unencrypted .zip
if zip.open_zip("qa_data/zips/test.zip").is_err() {
    println!("{}", zip.last_error_text());
    return;
}

// Unzip to a temp directory.
let num_files_unzipped = zip.unzip("qa_output/tmp");
if num_files_unzipped < 0 {
    println!("{}", zip.last_error_text());
    return;
}

// Clear the zip object.
let _ = zip.new_zip("qa_output/aesTest.zip");

// Indicate that 128-bit AES encryption is to be used when writing the .zip
zip.set_encryption(4);
zip.set_encrypt_key_length(128);

// Set the password.
zip.set_encrypt_password("secret");

// Append the files.
zip.set_append_from_dir("qa_output/tmp");
if zip.append_files("*.*", true).is_err() {
    println!("{}", zip.last_error_text());
    return;
}

// Write the .zip and close it.
if zip.write_zip_and_close().is_err() {
    println!("{}", zip.last_error_text());
    return;
}

println!("Success.");