From 4f4f76c9a207b6c45c1d7f057692544e6f7ede83 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 23 Jul 2023 10:28:11 +0200 Subject: [PATCH 01/14] 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 000000000..87e52bccb --- /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(()) +} + From fbf000cf5671b9b00f88db8aa59a4e0f9fcfc56e Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Wed, 1 May 2024 15:06:55 -0700 Subject: [PATCH 02/14] docs(examples): Update FileOptions import --- examples/write-large-file.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 87e52bccb..3326af86e 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -2,7 +2,7 @@ use std::io::Write; -use zip::write::FileOptions; +use zip::write::{SimpleFileOptions}; fn main() -> Result<(), Box> { let args: Vec<_> = std::env::args().collect(); if args.len() < 2 { @@ -18,7 +18,7 @@ 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() + let options = SimpleFileOptions::default() .compression_method(zip::CompressionMethod::Deflated) // files over u32::MAX require this flag set. .large_file(true) From 04390ca27632d710e42a64058a51f404079fcf0b Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Wed, 1 May 2024 15:07:15 -0700 Subject: [PATCH 03/14] style: cargo fmt --all --- examples/write-large-file.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 3326af86e..7302f7d2f 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -2,7 +2,7 @@ use std::io::Write; -use zip::write::{SimpleFileOptions}; +use zip::write::SimpleFileOptions; fn main() -> Result<(), Box> { let args: Vec<_> = std::env::args().collect(); if args.len() < 2 { @@ -33,4 +33,3 @@ fn doit(filename: &str) -> zip::result::ZipResult<()> { zip.finish()?; Ok(()) } - From e670e98073bc7e1b5f59a19535da6b50cf294daa Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Wed, 1 May 2024 15:19:52 -0700 Subject: [PATCH 04/14] fix: only compile new example if a Deflate encoder is enabled --- examples/write-large-file.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 7302f7d2f..2b8ee9433 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -3,6 +3,7 @@ use std::io::Write; use zip::write::SimpleFileOptions; +#[cfg(feature = "_deflate-any")] fn main() -> Result<(), Box> { let args: Vec<_> = std::env::args().collect(); if args.len() < 2 { From 2c61eb0cb734d1f261e26e1fc97e9c253b76e036 Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 13:14:49 -0700 Subject: [PATCH 05/14] doc(examples): fix a bug `doit()` function can't exist without `cfg(feature = "_deflate-any")`, and making it unreachable isn't enough Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> --- examples/write-large-file.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 2b8ee9433..f7ae183e5 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -15,6 +15,7 @@ fn main() -> Result<(), Box> { Ok(()) } +#[cfg(feature = "_deflate-any")] fn doit(filename: &str) -> zip::result::ZipResult<()> { let file = std::fs::File::create(filename)?; let mut zip = zip::ZipWriter::new(file); From 855d57610c09914e79fc45f8529dc7e6adfba44b Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 13:35:08 -0700 Subject: [PATCH 06/14] style: Fix unused warnings by making the entire file cfg-dependent --- examples/write-large-file.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index f7ae183e5..8e6b594e1 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -1,9 +1,9 @@ +#![cfg(feature = "_deflate-any")] //! Write a huge file with lots of zeros, that should compress perfectly. use std::io::Write; use zip::write::SimpleFileOptions; -#[cfg(feature = "_deflate-any")] fn main() -> Result<(), Box> { let args: Vec<_> = std::env::args().collect(); if args.len() < 2 { @@ -15,7 +15,6 @@ fn main() -> Result<(), Box> { Ok(()) } -#[cfg(feature = "_deflate-any")] fn doit(filename: &str) -> zip::result::ZipResult<()> { let file = std::fs::File::create(filename)?; let mut zip = zip::ZipWriter::new(file); From 7355c51f93845bda3837e0b522a97885eed01018 Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 13:47:36 -0700 Subject: [PATCH 07/14] doc(examples): Fix errors main() will still exist without a deflate cfg, it just won't do anything. Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> --- examples/write-large-file.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 8e6b594e1..fc38838fc 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -1,20 +1,25 @@ -#![cfg(feature = "_deflate-any")] //! Write a huge file with lots of zeros, that should compress perfectly. use std::io::Write; use zip::write::SimpleFileOptions; fn main() -> Result<(), Box> { + if !cfg!(feature = "_deflate-any") { + return Err("Please enable one of the deflate features"); + } let args: Vec<_> = std::env::args().collect(); if args.len() < 2 { return Err(format!("Usage: {} ", args[0]).into()); } let filename = &*args[1]; - doit(filename)?; + #![] { + doit(filename)?; + } Ok(()) } +#[cfg(feature = "_deflate-any")] fn doit(filename: &str) -> zip::result::ZipResult<()> { let file = std::fs::File::create(filename)?; let mut zip = zip::ZipWriter::new(file); From 90b646b2029dac1b3752b99c018fc86731933879 Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 14:55:40 -0700 Subject: [PATCH 08/14] doc(examples): Fix missing piece from previous commit Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> --- examples/write-large-file.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index fc38838fc..3c40977fb 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -13,7 +13,7 @@ fn main() -> Result<(), Box> { } let filename = &*args[1]; - #![] { + #![cfg(feature = "_deflate-any")] { doit(filename)?; } Ok(()) From 468f314b2931605cdd5a0022da0aa8569604efbc Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 15:38:55 -0700 Subject: [PATCH 09/14] doc(examples): fix typo Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> --- examples/write-large-file.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 3c40977fb..c30c52bb2 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -13,7 +13,7 @@ fn main() -> Result<(), Box> { } let filename = &*args[1]; - #![cfg(feature = "_deflate-any")] { + #[cfg(feature = "_deflate-any")] { doit(filename)?; } Ok(()) From b7cce8a296d74bc54ef3f3712acae76297a31d5b Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 17:09:26 -0700 Subject: [PATCH 10/14] doc(examples): fix type mismatch Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> --- examples/write-large-file.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index c30c52bb2..772e509d2 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -5,7 +5,7 @@ use std::io::Write; use zip::write::SimpleFileOptions; fn main() -> Result<(), Box> { if !cfg!(feature = "_deflate-any") { - return Err("Please enable one of the deflate features"); + return Err("Please enable one of the deflate features".into()); } let args: Vec<_> = std::env::args().collect(); if args.len() < 2 { From 8fa3ebb3431b0a8acfe6e4c7f64b409abd4898bf Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 17:41:49 -0700 Subject: [PATCH 11/14] style: Fix cargo fmt warning Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> --- examples/write-large-file.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 772e509d2..47810dad5 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -13,7 +13,8 @@ fn main() -> Result<(), Box> { } let filename = &*args[1]; - #[cfg(feature = "_deflate-any")] { + #[cfg(feature = "_deflate-any")] + { doit(filename)?; } Ok(()) From 63793a32ffc45a752756be4a9dbcbeb2f08e2348 Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 18:04:05 -0700 Subject: [PATCH 12/14] chore: Fix errors by moving `use` statements inside the cfg-guarded function that uses them --- examples/write-large-file.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 47810dad5..f9005cf03 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -1,8 +1,5 @@ //! Write a huge file with lots of zeros, that should compress perfectly. -use std::io::Write; - -use zip::write::SimpleFileOptions; fn main() -> Result<(), Box> { if !cfg!(feature = "_deflate-any") { return Err("Please enable one of the deflate features".into()); @@ -22,6 +19,10 @@ fn main() -> Result<(), Box> { #[cfg(feature = "_deflate-any")] fn doit(filename: &str) -> zip::result::ZipResult<()> { + use std::io::Write; + + use zip::write::SimpleFileOptions; + let file = std::fs::File::create(filename)?; let mut zip = zip::ZipWriter::new(file); From 28c2eb59e70c1a36b449c36c7b88d4f0b2c6f47a Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 18:32:20 -0700 Subject: [PATCH 13/14] chore: Fix error by cfg-guarding the declaration of `filename` and not just its use --- examples/write-large-file.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index f9005cf03..38914650d 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -8,10 +8,10 @@ fn main() -> Result<(), Box> { if args.len() < 2 { return Err(format!("Usage: {} ", args[0]).into()); } - - let filename = &*args[1]; + #[cfg(feature = "_deflate-any")] { + let filename = &*args[1]; doit(filename)?; } Ok(()) From 731c8058dc7f251783c9ab1d45fa15e30484b622 Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 2 May 2024 19:29:25 -0700 Subject: [PATCH 14/14] style: cargo fmt --all --- examples/write-large-file.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/write-large-file.rs b/examples/write-large-file.rs index 38914650d..d9742e280 100644 --- a/examples/write-large-file.rs +++ b/examples/write-large-file.rs @@ -8,7 +8,7 @@ fn main() -> Result<(), Box> { if args.len() < 2 { return Err(format!("Usage: {} ", args[0]).into()); } - + #[cfg(feature = "_deflate-any")] { let filename = &*args[1];