use s_zip::{Result, StreamingZipReader, StreamingZipWriter};
fn main() -> Result<()> {
println!("๐ Encryption Roundtrip Test\n");
let zip_path = "roundtrip_test.zip";
let password = "test_password_12345";
println!("Step 1: Creating encrypted ZIP...");
{
let mut writer = StreamingZipWriter::new(zip_path)?;
writer.set_password(password);
writer.start_entry("secret1.txt")?;
writer.write_data(b"This is the first secret message!")?;
writer.start_entry("secret2.txt")?;
writer.write_data(b"This is the second secret message with more data!")?;
writer.start_entry("folder/secret3.txt")?;
writer.write_data(b"Nested secret file in a folder!")?;
writer.finish()?;
}
println!("โ
Created encrypted ZIP with 3 files\n");
println!("Step 2: Reading encrypted ZIP...");
let mut reader = StreamingZipReader::open(zip_path)?;
reader.set_password(password);
println!("๐ Archive contents:");
for entry in reader.entries() {
println!(" - {}: {} bytes", entry.name, entry.uncompressed_size);
#[cfg(feature = "encryption")]
if entry.is_encrypted {
println!(" ๐ Encrypted");
}
}
println!("\nStep 3: Decrypting and verifying contents...");
let data1 = reader.read_entry_by_name("secret1.txt")?;
let text1 = String::from_utf8_lossy(&data1);
println!("โ
secret1.txt: \"{}\"", text1);
assert_eq!(text1, "This is the first secret message!");
let data2 = reader.read_entry_by_name("secret2.txt")?;
let text2 = String::from_utf8_lossy(&data2);
println!("โ
secret2.txt: \"{}\"", text2);
assert_eq!(text2, "This is the second secret message with more data!");
let data3 = reader.read_entry_by_name("folder/secret3.txt")?;
let text3 = String::from_utf8_lossy(&data3);
println!("โ
folder/secret3.txt: \"{}\"", text3);
assert_eq!(data3, b"Nested secret file in a folder!");
println!("\n๐ Roundtrip test PASSED!");
println!(" - Encryption: โ
");
println!(" - Decryption: โ
");
println!(" - Password validation: โ
");
println!(" - HMAC authentication: โ
");
println!(" - Data integrity: โ
");
println!("\nStep 4: Testing wrong password...");
let mut reader_wrong = StreamingZipReader::open(zip_path)?;
reader_wrong.set_password("wrong_password");
match reader_wrong.read_entry_by_name("secret1.txt") {
Ok(_) => {
println!("โ ERROR: Should have failed with wrong password!");
std::process::exit(1);
}
Err(e) => {
println!("โ
Correctly rejected wrong password: {}", e);
}
}
println!("\nStep 5: Testing without password...");
let mut reader_no_pw = StreamingZipReader::open(zip_path)?;
match reader_no_pw.read_entry_by_name("secret1.txt") {
Ok(_) => {
println!("โ ERROR: Should have failed without password!");
std::process::exit(1);
}
Err(e) => {
println!("โ
Correctly rejected missing password: {}", e);
}
}
println!("\nโจ All security checks PASSED!");
std::fs::remove_file(zip_path)?;
println!("\n๐งน Cleaned up test file");
Ok(())
}