use s_zip::{Result, StreamingZipWriter};
use std::io::{Cursor, Seek, SeekFrom};
fn main() -> Result<()> {
println!("Example 1: Writing ZIP to in-memory buffer...");
let buffer = Vec::new();
let cursor = Cursor::new(buffer);
let mut zip = StreamingZipWriter::from_writer(cursor)?;
zip.start_entry("hello.txt")?;
zip.write_data(b"Hello from in-memory ZIP!")?;
zip.start_entry("data.txt")?;
zip.write_data(b"Some data in the second file.")?;
let cursor = zip.finish()?;
let zip_bytes = cursor.into_inner();
println!(
"✓ Successfully created in-memory ZIP ({} bytes)",
zip_bytes.len()
);
println!("\nExample 2: Writing with custom compression level...");
let buffer2 = Vec::new();
let cursor2 = Cursor::new(buffer2);
let mut zip2 = StreamingZipWriter::from_writer_with_compression(cursor2, 9)?;
zip2.start_entry("compressed.txt")?;
let large_data = "Hello World! ".repeat(1000);
zip2.write_data(large_data.as_bytes())?;
let cursor2 = zip2.finish()?;
let zip_bytes2 = cursor2.into_inner();
println!(
"✓ Successfully created highly compressed ZIP ({} bytes)",
zip_bytes2.len()
);
println!("\nExample 3: Using Cursor for random access...");
let mut buffer3 = Vec::new();
buffer3.extend_from_slice(b"PREFIX_");
let mut cursor3 = Cursor::new(buffer3);
cursor3.seek(SeekFrom::End(0))?;
let mut zip3 = StreamingZipWriter::from_writer(cursor3)?;
zip3.start_entry("after_prefix.txt")?;
zip3.write_data(b"This ZIP starts after the prefix")?;
let cursor3 = zip3.finish()?;
let final_buffer = cursor3.into_inner();
println!(
"✓ Successfully created ZIP with custom position ({} bytes total, {} prefix)",
final_buffer.len(),
"PREFIX_".len()
);
println!("\nExample 4: Creating in-memory ZIP and saving to file...");
let buffer4 = Vec::new();
let cursor4 = Cursor::new(buffer4);
let mut zip4 = StreamingZipWriter::from_writer(cursor4)?;
zip4.start_entry("output.txt")?;
zip4.write_data(b"This was created in memory, then saved to disk!")?;
let cursor4 = zip4.finish()?;
let zip_data = cursor4.into_inner();
std::fs::write("/tmp/example_output.zip", &zip_data)?;
println!(
"✓ Successfully saved in-memory ZIP to /tmp/example_output.zip ({} bytes)",
zip_data.len()
);
println!("\n✓ All examples completed successfully!");
println!("\n⚠️ CRITICAL - Memory Usage by Writer Type:");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!(" ✅ File (StreamingZipWriter::new): ~2-5 MB constant");
println!(" ✅ Network streams (TCP, pipes): ~2-5 MB constant");
println!(" ⚠️ Vec<u8>/Cursor: ENTIRE ZIP IN RAM!");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!("\n⚠️ WARNING for Vec<u8>/Cursor:");
println!(" While the compressor buffer is only ~2-5MB, the final ZIP");
println!(" accumulates in the Vec. A 1GB file will use ~1GB RAM!");
println!("\n✅ Recommended approach:");
println!(" - Large files (>100MB): Use StreamingZipWriter::new(path)");
println!(" - Network transfer: Use network streams");
println!(" - Small temp files: Vec<u8>/Cursor is fine (<100MB)");
Ok(())
}