Rust
Rust
Compress and Encrypt a Large File (Low and Constant Memory Footprint)
See more Compression Examples
Demonstrates how to compress and encrypt a large file such that the memory footprint remains low and constant.Note: This example requires Chilkat v9.5.0.99 or greater.
Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let compress = chilkat::Compression::new();
compress.set_algorithm("deflate");
// Set encryption params.
// The possible values are the same as for the corresponding properties in the Chilkat Crypt2 class/object.
// The encoded IV and Key must be specified as hex.
let json = chilkat::JsonObject::new();
let _ = json.update_string("cryptAlgorithm", "aes");
let _ = json.update_string("cipherMode", "cbc");
let _ = json.update_int("keyLength", 128);
let _ = json.update_int("paddingScheme", 0);
let _ = json.update_string("encodedIV", "000102030405060708090A0B0C0D0E0F");
let _ = json.update_string("encodedKey", "000102030405060708090A0B0C0D0E0F");
// Do file-to-file compression+encryption in a single call.
let in_path = "qa_data/largeFile.dat".to_string();
let out_path = "c:/temp/qa_output/compressed_encrypted.dat".to_string();
if compress.compress_encrypt_file(&json, &in_path, &out_path).is_err() {
println!("{}", compress.last_error_text());
return;
}
// We can do file-to-file decrypt/decompress like this:
let in_path2 = out_path.clone();
let out_path2 = "c:/temp/qa_output/restored.dat".to_string();
if compress.decrypt_decompress_file(&json, &in_path2, &out_path2).is_err() {
println!("{}", compress.last_error_text());
return;
}
// Note: The above decrypt + decompress is the equivalent of doing the same in these two steps:
let crypt = chilkat::Crypt2::new();
crypt.set_crypt_algorithm("aes");
crypt.set_cipher_mode("cbc");
crypt.set_key_length(128);
crypt.set_padding_scheme(0);
crypt.set_encoded_iv("000102030405060708090A0B0C0D0E0F", "hex");
crypt.set_encoded_key("000102030405060708090A0B0C0D0E0F", "hex");
let decrypted_path = "c:/temp/qa_output/decrypted.dat".to_string();
if crypt.ck_decrypt_file(&in_path2, &decrypted_path).is_err() {
println!("{}", crypt.last_error_text());
return;
}
let out_path3 = "c:/temp/qa_output/restored_in_two_steps.dat".to_string();
if compress.decompress_file(&decrypted_path, &out_path3).is_err() {
println!("{}", compress.last_error_text());
return;
}
println!("Success.");