Refactor: short-circuit empty writes
This commit is contained in:
parent
72a2f73496
commit
b3988b2aec
1 changed files with 16 additions and 0 deletions
16
src/write.rs
16
src/write.rs
|
@ -335,6 +335,9 @@ impl<W: Write + Seek> Write for ZipWriter<W> {
|
||||||
"No file has been started",
|
"No file has been started",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
if buf.len() == 0 {
|
||||||
|
return Ok(0);
|
||||||
|
}
|
||||||
match self.inner.ref_mut() {
|
match self.inner.ref_mut() {
|
||||||
Some(ref mut w) => {
|
Some(ref mut w) => {
|
||||||
let write_result = w.write(buf);
|
let write_result = w.write(buf);
|
||||||
|
@ -1559,6 +1562,7 @@ mod test {
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use crate::CompressionMethod::Deflated;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn write_empty_zip() {
|
fn write_empty_zip() {
|
||||||
|
@ -1974,6 +1978,18 @@ mod test {
|
||||||
writer.start_file("", FileOptions::default()).unwrap();
|
writer.start_file("", FileOptions::default()).unwrap();
|
||||||
Ok(())
|
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"))]
|
#[cfg(not(feature = "unreserved"))]
|
||||||
|
|
Loading…
Add table
Reference in a new issue