Skip to content

Commit

Permalink
Merge pull request #64 from ouch-org/zstd
Browse files Browse the repository at this point in the history
Add support for Zstd
  • Loading branch information
marcospb19 authored Oct 6, 2021
2 parents 3f718b8 + 5597ddc commit c4bf226
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tar = "0.4.37"
xz2 = "0.1.6"
zip = { version = "0.5.13", default-features = false, features = ["deflate-miniz"] }
flate2 = { version = "1.0.22", default-features = false, features = ["zlib"] }
zstd = "0.9.0+zstd.1.5.0"

[dev-dependencies]
tempfile = "3.2.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ For compiling, check [the wiki guide](https://github.com/ouch-org/ouch/wiki/Comp

| | .tar | .zip | .bz, .bz2 | .gz | .xz, .lz, .lzma | .zst |
|:-------------:|:----:|:----:|:---------:| --- |:---------------:| --- |
| Decompression |||||| |
| Compression |||||| |
| Decompression |||||| |
| Compression |||||| |

Note that formats can be chained:
- `.tar.gz`
Expand Down
20 changes: 14 additions & 6 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ fn compress_files(
Gzip => Box::new(flate2::write::GzEncoder::new(encoder, Default::default())),
Bzip => Box::new(bzip2::write::BzEncoder::new(encoder, Default::default())),
Lzma => Box::new(xz2::write::XzEncoder::new(encoder, 6)),
Zstd => {
let zstd_encoder = zstd::stream::write::Encoder::new(encoder, Default::default());
// Safety:
// Encoder::new() can only fail if `level` is invalid, but Default::default()
// is guaranteed to be valid
Box::new(zstd_encoder.unwrap().auto_finish())
}
_ => unreachable!(),
};
encoder
Expand All @@ -180,7 +187,7 @@ fn compress_files(
}

match formats[0] {
Gzip | Bzip | Lzma => {
Gzip | Bzip | Lzma | Zstd => {
writer = chain_writer_encoder(&formats[0], writer);
let mut reader = fs::File::open(&files[0]).unwrap();
io::copy(&mut reader, &mut writer)?;
Expand Down Expand Up @@ -251,23 +258,24 @@ fn decompress_file(
let mut reader: Box<dyn Read> = Box::new(reader);

// Grab previous decoder and wrap it inside of a new one
let chain_reader_decoder = |format: &CompressionFormat, decoder: Box<dyn Read>| {
let chain_reader_decoder = |format: &CompressionFormat, decoder: Box<dyn Read>| -> crate::Result<Box<dyn Read>> {
let decoder: Box<dyn Read> = match format {
Gzip => Box::new(flate2::read::GzDecoder::new(decoder)),
Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)),
Lzma => Box::new(xz2::read::XzDecoder::new(decoder)),
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
_ => unreachable!(),
};
decoder
Ok(decoder)
};

for format in formats.iter().skip(1).rev() {
reader = chain_reader_decoder(format, reader);
reader = chain_reader_decoder(format, reader)?;
}

match formats[0] {
Gzip | Bzip | Lzma => {
reader = chain_reader_decoder(&formats[0], reader);
Gzip | Bzip | Lzma | Zstd => {
reader = chain_reader_decoder(&formats[0], reader)?;

// TODO: improve error treatment
let mut writer = fs::File::create(&output_path)?;
Expand Down
3 changes: 3 additions & 0 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum CompressionFormat {
Bzip, // .bz
Lzma, // .lzma
Tar, // .tar (technically not a compression extension, but will do for now)
Zstd, // .zst
Zip, // .zip
}

Expand All @@ -22,6 +23,7 @@ impl fmt::Display for CompressionFormat {
match self {
Gzip => ".gz",
Bzip => ".bz",
Zstd => ".zst",
Lzma => ".lz",
Tar => ".tar",
Zip => ".zip",
Expand Down Expand Up @@ -49,6 +51,7 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Compr
_ if extension == "bz" => Bzip,
_ if extension == "gz" || extension == "bz2" => Gzip,
_ if extension == "xz" || extension == "lzma" || extension == "lz" => Lzma,
_ if extension == "zst" => Zstd,
_ => break,
};

Expand Down
3 changes: 2 additions & 1 deletion tests/compress_and_decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn test_each_format() {
test_compressing_and_decompressing_archive("tar.xz");
test_compressing_and_decompressing_archive("tar.lz");
test_compressing_and_decompressing_archive("tar.lzma");
test_compressing_and_decompressing_archive("tar.zst");
test_compressing_and_decompressing_archive("zip");
test_compressing_and_decompressing_archive("zip.gz");
test_compressing_and_decompressing_archive("zip.bz");
Expand All @@ -31,7 +32,7 @@ fn test_each_format() {

// Why not
test_compressing_and_decompressing_archive(
"tar.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.lz.lz.lz.lz.lz.lz.lz.lz.lz.lz.bz.bz.bz.bz.bz.bz.bz",
"tar.gz.gz.gz.gz.gz.gz.gz.gz.zst.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.lz.lz.lz.lz.lz.lz.lz.lz.lz.lz.bz.bz.bz.bz.bz.bz.bz",
);
}

Expand Down

0 comments on commit c4bf226

Please sign in to comment.