Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StoredOnlyCompressor feature is here, and we can now access it. #490

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand Down
19 changes: 12 additions & 7 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ impl<W: Write> Writer<W> {
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()))?;

Expand Down Expand Up @@ -724,13 +725,7 @@ impl<W: Write> Writer<W> {
// 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
}
Expand Down Expand Up @@ -817,6 +812,15 @@ impl<W: Write> Writer<W> {
Ok(())
}

fn stored_only_compressor(data: &[u8], in_len: usize) -> Result<Vec<u8>> {
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
Expand Down Expand Up @@ -1706,6 +1710,7 @@ impl Compression {
Compression::Huffman => flate2::Compression::none(),
#[allow(deprecated)]
Compression::Rle => flate2::Compression::none(),
Compression::None => flate2::Compression::none(),
}
}
}
Expand Down
Loading