From 4f4f76c9a207b6c45c1d7f057692544e6f7ede83 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 23 Jul 2023 10:28:11 +0200 Subject: [PATCH] feat: add `write-large-file` example to show how to write huge files. This requires DEFLATE64 support, which is seemingly present in the default rust backend. --- examples/write-large-file.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/write-large-file.rs diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs new file mode 100644 index 00000000..87e52bcc --- /dev/null +++ b/examples/write-large-file.rs @@ -0,0 +1,36 @@ +//! Write a huge file with lots of zeros, that should compress perfectly. + +use std::io::Write; + +use zip::write::FileOptions; +fn main() -> Result<(), Box> { + let args: Vec<_> = std::env::args().collect(); + if args.len() < 2 { + return Err(format!("Usage: {} ", args[0]).into()); + } + + let filename = &*args[1]; + doit(filename)?; + Ok(()) +} + +fn doit(filename: &str) -> zip::result::ZipResult<()> { + let file = std::fs::File::create(filename)?; + let mut zip = zip::ZipWriter::new(file); + + let options = FileOptions::default() + .compression_method(zip::CompressionMethod::Deflated) + // files over u32::MAX require this flag set. + .large_file(true) + .unix_permissions(0o755); + zip.start_file("huge-file-of-zeroes", options)?; + let content: Vec<_> = std::iter::repeat(0_u8).take(65 * 1024).collect(); + let mut bytes_written = 0_u64; + while bytes_written < u32::MAX as u64 { + zip.write_all(&content)?; + bytes_written += content.len() as u64; + } + zip.finish()?; + Ok(()) +} +