Skip to content

Commit 81df1ee

Browse files
committed
feat: Replace liblzma with lzma-rust2
Replace the LZMA implementation from bindings to the `liblzma` to a pure Rust implementation.
1 parent aeb27cc commit 81df1ee

File tree

10 files changed

+58
-56
lines changed

10 files changed

+58
-56
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ Categories Used:
2727
- Provide Nushell completions (packages still need to install them) [\#827](https://github.com/ouch-org/ouch/pull/827) ([FrancescElies](https://github.com/FrancescElies))
2828
- Support `.lz` decompression [\#838](https://github.com/ouch-org/ouch/pull/838) ([zzzsyyy](https://github.com/zzzsyyy))
2929
- Support `.lzma` decompression (and fix `.lzma` being a wrong alias for `.xz`) [\#838](https://github.com/ouch-org/ouch/pull/838) ([zzzsyyy](https://github.com/zzzsyyy))
30+
- Support `.lz` compression [\#863](https://github.com/ouch-org/ouch/pull/863) ([sorairolake](https://github.com/sorairolake))
3031

3132
### Improvements
3233

3334
- Give better error messages when archive extensions are invalid [\#817](https://github.com/ouch-org/ouch/pull/817) ([marcospb19](https://github.com/marcospb19))
3435
- Add aliases for `--password` flag (`--pass` and `--pw`) [\#847](https://github.com/ouch-org/ouch/pull/847) ([marcospb19](https://github.com/marcospb19))
36+
- Use `lzma-rust2` crate instead of `liblzma` crate [\#865](https://github.com/ouch-org/ouch/pull/865) ([sorairolake](https://github.com/sorairolake))
3537

3638
### Bug Fixes
3739

Cargo.lock

Lines changed: 29 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ authors = [
66
"Vinícius Rodrigues Miguel <[email protected]>",
77
]
88
edition = "2021"
9+
rust-version = "1.82.0"
910
readme = "README.md"
1011
repository = "https://github.com/ouch-org/ouch"
1112
license = "MIT"
@@ -31,6 +32,7 @@ ignore = "0.4.23"
3132
libc = "0.2.155"
3233
linked-hash-map = "0.5.6"
3334
lz4_flex = "0.11.3"
35+
lzma-rust2 = "0.13.0"
3436
num_cpus = "1.16.0"
3537
once_cell = "1.20.2"
3638
rayon = "1.10.0"
@@ -41,7 +43,6 @@ tar = "0.4.42"
4143
tempfile = "3.10.1"
4244
time = { version = "0.3.36", default-features = false }
4345
unrar = { version = "0.5.7", optional = true }
44-
liblzma = "0.4"
4546
zip = { version = "0.6.6", default-features = false, features = [
4647
"time",
4748
"aes-crypto",

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Output:
113113

114114
| Format | `.tar` | `.zip` | `7z` | `.gz` | `.xz` | `.lzma` | `.lz` | `.bz`, `.bz2` | `.bz3` | `.lz4` | `.sz` (Snappy) | `.zst` | `.rar` | `.br` |
115115
|:---------:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
116-
| Supported || ✓¹ | ✓¹ | ✓² || ✓⁴ | |||| ✓² | ✓² | ✓³ ||
116+
| Supported || ✓¹ | ✓¹ | ✓² || ✓⁴ ||||| ✓² | ✓² | ✓³ ||
117117

118118
✓: Supports compression and decompression.
119119

@@ -189,7 +189,6 @@ If you're downloading binaries from the [releases page](https://github.com/ouch-
189189

190190
Otherwise, you'll need these libraries installed on your system:
191191

192-
* [liblzma](https://www.7-zip.org/sdk.html)
193192
* [libbz2](https://www.sourceware.org/bzip2)
194193
* [libbz3](https://github.com/kspalaiologos/bzip3)
195194
* [libz](https://www.zlib.net)

src/commands/compress.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,19 @@ pub fn compress_files(
7373
reason: "LZMA1 compression is not supported in ouch, use .xz instead.".to_string(),
7474
})
7575
}
76-
Xz => Box::new(liblzma::write::XzEncoder::new(
77-
encoder,
78-
level.map_or(6, |l| (l as u32).clamp(0, 9)),
79-
)),
76+
Xz => {
77+
let options = level.map_or_else(Default::default, |l| {
78+
lzma_rust2::XzOptions::with_preset((l as u32).clamp(0, 9))
79+
});
80+
let writer = lzma_rust2::XzWriter::new(encoder, options)?;
81+
Box::new(writer.auto_finish())
82+
}
8083
Lzip => {
81-
return Err(crate::Error::UnsupportedFormat {
82-
reason: "Lzip compression is not supported in ouch.".to_string(),
83-
})
84+
let options = level.map_or_else(Default::default, |l| {
85+
lzma_rust2::LzipOptions::with_preset((l as u32).clamp(0, 9))
86+
});
87+
let writer = lzma_rust2::LzipWriter::new(encoder, options);
88+
Box::new(writer.auto_finish())
8489
}
8590
Snappy => Box::new(
8691
gzp::par::compress::ParCompress::<gzp::snap::Snap>::builder()

src/commands/decompress.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,9 @@ pub fn decompress_file(options: DecompressOptions) -> crate::Result<()> {
128128
Box::new(bzip3::read::Bz3Decoder::new(decoder)?)
129129
}
130130
Lz4 => Box::new(lz4_flex::frame::FrameDecoder::new(decoder)),
131-
Lzma => Box::new(liblzma::read::XzDecoder::new_stream(
132-
decoder,
133-
liblzma::stream::Stream::new_lzma_decoder(u64::MAX).unwrap(),
134-
)),
135-
Xz => Box::new(liblzma::read::XzDecoder::new(decoder)),
136-
Lzip => Box::new(liblzma::read::XzDecoder::new_stream(
137-
decoder,
138-
liblzma::stream::Stream::new_lzip_decoder(u64::MAX, 0).unwrap(),
139-
)),
131+
Lzma => Box::new(lzma_rust2::LzmaReader::new_mem_limit(decoder, u32::MAX, None)?),
132+
Xz => Box::new(lzma_rust2::XzReader::new(decoder, true)),
133+
Lzip => Box::new(lzma_rust2::LzipReader::new(decoder)?),
140134
Snappy => Box::new(snap::read::FrameDecoder::new(decoder)),
141135
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
142136
Brotli => Box::new(brotli::Decompressor::new(decoder, BUFFER_CAPACITY)),

src/commands/list.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,9 @@ pub fn list_archive_contents(
5757
Box::new(bzip3::read::Bz3Decoder::new(decoder).unwrap())
5858
}
5959
Lz4 => Box::new(lz4_flex::frame::FrameDecoder::new(decoder)),
60-
Lzma => Box::new(liblzma::read::XzDecoder::new_stream(
61-
decoder,
62-
liblzma::stream::Stream::new_lzma_decoder(u64::MAX).unwrap(),
63-
)),
64-
Xz => Box::new(liblzma::read::XzDecoder::new(decoder)),
65-
Lzip => Box::new(liblzma::read::XzDecoder::new_stream(
66-
decoder,
67-
liblzma::stream::Stream::new_lzip_decoder(u64::MAX, 0).unwrap(),
68-
)),
60+
Lzma => Box::new(lzma_rust2::LzmaReader::new_mem_limit(decoder, u32::MAX, None)?),
61+
Xz => Box::new(lzma_rust2::XzReader::new(decoder, true)),
62+
Lzip => Box::new(lzma_rust2::LzipReader::new(decoder)?),
6963
Snappy => Box::new(snap::read::FrameDecoder::new(decoder)),
7064
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
7165
Brotli => Box::new(brotli::Decompressor::new(decoder, BUFFER_CAPACITY)),

src/extension.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub enum CompressionFormat {
9494
Lzip,
9595
/// .sz
9696
Snappy,
97-
/// tar, tgz, tbz, tbz2, tbz3, txz, tlz4, tlzma, tsz, tzst
97+
/// tar, tgz, tbz, tbz2, tbz3, txz, tlz, tlz4, tlzma, tsz, tzst
9898
Tar,
9999
/// .zst
100100
Zstd,

src/utils/formatting.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ impl Display for EscapedPathDisplay<'_> {
4545
/// This is different from [`Path::display`].
4646
///
4747
/// See <https://gist.github.com/marcospb19/ebce5572be26397cf08bbd0fd3b65ac1> for a comparison.
48-
pub fn path_to_str(path: &Path) -> Cow<str> {
48+
pub fn path_to_str(path: &Path) -> Cow<'_, str> {
4949
os_str_to_str(path.as_ref())
5050
}
5151

52-
pub fn os_str_to_str(os_str: &OsStr) -> Cow<str> {
52+
pub fn os_str_to_str(os_str: &OsStr) -> Cow<'_, str> {
5353
let format = || {
5454
let text = format!("{os_str:?}");
5555
Cow::Owned(text.trim_matches('"').to_string())
@@ -83,7 +83,7 @@ pub fn pretty_format_list_of_paths(paths: &[impl AsRef<Path>]) -> String {
8383
}
8484

8585
/// Display the directory name, but use "current directory" when necessary.
86-
pub fn nice_directory_display(path: &Path) -> Cow<str> {
86+
pub fn nice_directory_display(path: &Path) -> Cow<'_, str> {
8787
if path == Path::new(".") {
8888
Cow::Borrowed("current directory")
8989
} else {

tests/integration.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum DirectoryExtension {
3232
#[cfg(feature = "bzip3")]
3333
Tbz3,
3434
Tgz,
35+
Tlz,
3536
Tlz4,
3637
Tsz,
3738
Txz,
@@ -48,6 +49,7 @@ enum FileExtension {
4849
#[cfg(feature = "bzip3")]
4950
Bz3,
5051
Gz,
52+
Lz,
5153
Lz4,
5254
Sz,
5355
Xz,

0 commit comments

Comments
 (0)