Skip to content

Commit

Permalink
feat: Add zstd support (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime authored Jan 31, 2024
1 parent c448134 commit 258be09
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"rust-analyzer.cargo.features": ["default", "lzma", "deflate64", "bzip2"]
"rust-analyzer.cargo.features": ["default", "lzma", "deflate64", "bzip2", "zstd"]
}
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cfg-if = "1.0.0"
lzma-rs = { version = "0.3.0", features = ["stream"], optional = true }
deflate64 = { version = "0.1.7", optional = true }
bzip2 = { version = "0.4.4", optional = true }
zstd = { version = "0.13.0", optional = true }

[features]
default = ["sync", "file", "deflate"]
Expand All @@ -47,6 +48,7 @@ deflate = ["dep:flate2"]
deflate64 = ["dep:deflate64"]
lzma = ["dep:lzma-rs"]
bzip2 = ["dep:bzip2"]
zstd = ["dep:zstd"]

[dev-dependencies]
clap = { version = "4.4.18", features = ["derive"] }
Expand Down
12 changes: 12 additions & 0 deletions src/reader/sync/entry_reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ mod deflate64_dec;
#[cfg(feature = "bzip2")]
mod bzip2_dec;

#[cfg(feature = "zstd")]
mod zstd_dec;

use cfg_if::cfg_if;
use oval::Buffer;
use std::io;
Expand Down Expand Up @@ -309,6 +312,15 @@ where
}
}
}
Method::Zstd => {
cfg_if! {
if #[cfg(feature = "zstd")] {
Box::new(zstd_dec::mk_decoder(limited_reader)?)
} else {
return Err(Error::method_not_enabled(self.method));
}
}
}
method => {
return Err(Error::method_not_supported(method));
}
Expand Down
23 changes: 23 additions & 0 deletions src/reader/sync/entry_reader/zstd_dec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::io::{BufReader, Read};

use zstd::stream::Decoder as ZstdDecoder;

use crate::reader::sync::{Decoder, LimitedReader};

impl<R> Decoder<R> for ZstdDecoder<'static, BufReader<R>>
where
R: Read,
{
fn into_inner(self: Box<Self>) -> R {
Self::finish(*self).into_inner()
}

fn get_mut(&mut self) -> &mut R {
Self::get_mut(self).get_mut()
}
}

pub(crate) fn mk_decoder(r: LimitedReader) -> std::io::Result<impl Decoder<LimitedReader>> {
// TODO: have LimitedReader (and Buffer) implement BufRead, cf. https://github.com/fasterthanlime/rc-zip/issues/55
ZstdDecoder::with_buffer(BufReader::new(r))
}
Binary file added tests/data/found-me-zstd.zip
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ fn test_cases() -> Vec<ZipTest> {
}],
..Default::default()
},
// same with zstd
#[cfg(feature = "zstd")]
ZipTest {
source: ZipSource::File("found-me-zstd.zip"),
expected_encoding: Some(Encoding::Utf8),
files: vec![ZipTestFile {
name: "found-me.txt",
content: FileContent::Bytes("Oh no, you found me\n".repeat(5000).into()),
modified: Some(date((2024, 1, 31), (6, 10, 25), 800491400, time_zone(0)).unwrap()),
..Default::default()
}],
..Default::default()
},
]
}

Expand Down

0 comments on commit 258be09

Please sign in to comment.