From b3988b2aecb8a2efb72393c3be2daee9efedc197 Mon Sep 17 00:00:00 2001 From: Chris Hennick Date: Sat, 27 May 2023 14:42:23 -0700 Subject: [PATCH] Refactor: short-circuit empty writes --- src/write.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/write.rs b/src/write.rs index 8b2c5981..eaff39d5 100644 --- a/src/write.rs +++ b/src/write.rs @@ -335,6 +335,9 @@ impl Write for ZipWriter { "No file has been started", )); } + if buf.len() == 0 { + return Ok(0); + } match self.inner.ref_mut() { Some(ref mut w) => { let write_result = w.write(buf); @@ -1559,6 +1562,7 @@ mod test { use std::io; use std::io::{Read, Write}; use std::sync::Arc; + use crate::CompressionMethod::Deflated; #[test] fn write_empty_zip() { @@ -1974,6 +1978,18 @@ mod test { writer.start_file("", FileOptions::default()).unwrap(); Ok(()) } + + #[cfg(feature = "deflate-zopfli")] + #[test] + fn zopfli_empty_write() -> ZipResult<()> { + let mut options = FileOptions::default(); + options = options.compression_method(Deflated).compression_level(Some(264)); + let mut writer = ZipWriter::new(io::Cursor::new(Vec::new()), false); + writer.start_file("", options).unwrap(); + writer.write_all(&[]).unwrap(); + writer.write_all(&[]).unwrap(); + Ok(()) + } } #[cfg(not(feature = "unreserved"))]