From 903507298cdc69b67c6abc30c9550b00882c8ab1 Mon Sep 17 00:00:00 2001 From: adrian Date: Wed, 18 Sep 2024 09:45:56 -0400 Subject: [PATCH 1/2] StoredOnlyCompressor feature is here, and we can now access it. --- src/common.rs | 2 ++ src/encoder.rs | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/common.rs b/src/common.rs index 4475153e..dcc274d5 100644 --- a/src/common.rs +++ b/src/common.rs @@ -315,6 +315,8 @@ pub enum Compression { /// the encoder can do, but is meant to emulate the `Best` setting in the `Flate2` /// library. Best, + /// Do no compression + None, #[deprecated( since = "0.17.6", note = "use one of the other compression levels instead, such as 'fast'" diff --git a/src/encoder.rs b/src/encoder.rs index 15050ec0..8351a1f5 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -694,6 +694,7 @@ impl Writer { let adaptive_method = self.options.adaptive_filter; let zlib_encoded = match self.info.compression { + Compression::None => self.stored_only_compressor(data, in_len)?, Compression::Fast => { let mut compressor = fdeflate::Compressor::new(std::io::Cursor::new(Vec::new()))?; @@ -724,13 +725,7 @@ impl Writer { // requested by the user. Doing filtering again would only add performance // cost for both encoding and subsequent decoding, without improving the // compression ratio. - let mut compressor = - fdeflate::StoredOnlyCompressor::new(std::io::Cursor::new(Vec::new()))?; - for line in data.chunks(in_len) { - compressor.write_data(&[0])?; - compressor.write_data(line)?; - } - compressor.finish()?.into_inner() + self.stored_only_compressor(data, in_len)? } else { compressed } @@ -817,6 +812,15 @@ impl Writer { Ok(()) } + fn stored_only_compressor(&self, data: &[u8], in_len: usize) -> Result> { + let mut compressor = fdeflate::StoredOnlyCompressor::new(std::io::Cursor::new(Vec::new()))?; + for line in data.chunks(in_len) { + compressor.write_data(&[0])?; + compressor.write_data(line)?; + } + Ok(compressor.finish()?.into_inner()) + } + /// Set the used filter type for the following frames. /// /// The default filter is [`FilterType::Sub`] which provides a basic prediction algorithm for @@ -1706,6 +1710,7 @@ impl Compression { Compression::Huffman => flate2::Compression::none(), #[allow(deprecated)] Compression::Rle => flate2::Compression::none(), + Compression::None => flate2::Compression::none(), } } } From 077e8a56a6946af89be77e557cc34bc4145bbb13 Mon Sep 17 00:00:00 2001 From: adrian Date: Wed, 18 Sep 2024 09:41:30 -0400 Subject: [PATCH 2/2] code review comments. --- src/encoder.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/encoder.rs b/src/encoder.rs index 8351a1f5..bdb3ff83 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -694,7 +694,7 @@ impl Writer { let adaptive_method = self.options.adaptive_filter; let zlib_encoded = match self.info.compression { - Compression::None => self.stored_only_compressor(data, in_len)?, + Compression::None => Self::stored_only_compressor(data, in_len)?, Compression::Fast => { let mut compressor = fdeflate::Compressor::new(std::io::Cursor::new(Vec::new()))?; @@ -725,7 +725,7 @@ impl Writer { // requested by the user. Doing filtering again would only add performance // cost for both encoding and subsequent decoding, without improving the // compression ratio. - self.stored_only_compressor(data, in_len)? + Self::stored_only_compressor(data, in_len)? } else { compressed } @@ -812,7 +812,7 @@ impl Writer { Ok(()) } - fn stored_only_compressor(&self, data: &[u8], in_len: usize) -> Result> { + fn stored_only_compressor(data: &[u8], in_len: usize) -> Result> { let mut compressor = fdeflate::StoredOnlyCompressor::new(std::io::Cursor::new(Vec::new()))?; for line in data.chunks(in_len) { compressor.write_data(&[0])?;