Skip to content

Commit 365c81a

Browse files
authored
Use xz2 crate instead of a custom implementation (#306)
* Use `xz2` crate for xz decompression * Add support for xz encoding * Fix new clippy warning
1 parent ae94b34 commit 365c81a

File tree

5 files changed

+20
-277
lines changed

5 files changed

+20
-277
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ zstd = { version = "0.13", optional = true, default-features = false }
4949
zopfli = { version = "0.8", optional = true }
5050
deflate64 = { version = "0.1.9", optional = true }
5151
lzma-rs = { version = "0.3", default-features = false, optional = true }
52+
xz2 = { version = "0.1.7", optional = true }
5253

5354
[target.'cfg(any(all(target_arch = "arm", target_pointer_width = "32"), target_arch = "mips", target_arch = "powerpc"))'.dependencies]
5455
crossbeam-utils = "0.8.20"
@@ -80,7 +81,7 @@ deflate-zopfli = ["zopfli", "_deflate-any"]
8081
nt-time = ["dep:nt-time"]
8182
lzma = ["lzma-rs/stream"]
8283
unreserved = []
83-
xz = ["lzma-rs/raw_decoder"]
84+
xz = ["dep:xz2"]
8485
default = [
8586
"aes-crypto",
8687
"bzip2",

src/compression.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ pub const SUPPORTED_COMPRESSION_METHODS: &[CompressionMethod] = &[
187187
CompressionMethod::Bzip2,
188188
#[cfg(feature = "zstd")]
189189
CompressionMethod::Zstd,
190+
#[cfg(feature = "xz")]
191+
CompressionMethod::Xz,
190192
];
191193

192194
pub(crate) enum Decompressor<R: io::BufRead> {
@@ -202,7 +204,7 @@ pub(crate) enum Decompressor<R: io::BufRead> {
202204
#[cfg(feature = "lzma")]
203205
Lzma(Box<crate::read::lzma::LzmaDecoder<R>>),
204206
#[cfg(feature = "xz")]
205-
Xz(crate::read::xz::XzDecoder<R>),
207+
Xz(xz2::bufread::XzDecoder<R>),
206208
}
207209

208210
impl<R: io::BufRead> io::Read for Decompressor<R> {
@@ -246,7 +248,7 @@ impl<R: io::BufRead> Decompressor<R> {
246248
Decompressor::Lzma(Box::new(crate::read::lzma::LzmaDecoder::new(reader)))
247249
}
248250
#[cfg(feature = "xz")]
249-
CompressionMethod::Xz => Decompressor::Xz(crate::read::xz::XzDecoder::new(reader)),
251+
CompressionMethod::Xz => Decompressor::Xz(xz2::bufread::XzDecoder::new(reader)),
250252
_ => {
251253
return Err(crate::result::ZipError::UnsupportedArchive(
252254
"Compression method not supported",

src/read.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ pub(crate) mod stream;
3535
#[cfg(feature = "lzma")]
3636
pub(crate) mod lzma;
3737

38-
#[cfg(feature = "xz")]
39-
pub(crate) mod xz;
40-
4138
pub(crate) mod magic_finder;
4239

4340
// Put the struct declaration in a private module to convince rustdoc to display ZipArchive nicely

src/read/xz.rs

Lines changed: 0 additions & 270 deletions
This file was deleted.

src/write.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ enum GenericZipWriter<W: Write + Seek> {
9999
Bzip2(BzEncoder<MaybeEncrypted<W>>),
100100
#[cfg(feature = "zstd")]
101101
Zstd(ZstdEncoder<'static, MaybeEncrypted<W>>),
102+
#[cfg(feature = "xz")]
103+
Xz(xz2::write::XzEncoder<MaybeEncrypted<W>>),
102104
}
103105

104106
impl<W: Write + Seek> Debug for GenericZipWriter<W> {
@@ -118,6 +120,8 @@ impl<W: Write + Seek> Debug for GenericZipWriter<W> {
118120
GenericZipWriter::Bzip2(w) => f.write_fmt(format_args!("Bzip2({:?})", w.get_ref())),
119121
#[cfg(feature = "zstd")]
120122
GenericZipWriter::Zstd(w) => f.write_fmt(format_args!("Zstd({:?})", w.get_ref())),
123+
#[cfg(feature = "xz")]
124+
GenericZipWriter::Xz(w) => f.write_fmt(format_args!("Xz({:?})", w.get_ref())),
121125
}
122126
}
123127
}
@@ -1763,7 +1767,12 @@ impl<W: Write + Seek> GenericZipWriter<W> {
17631767
}
17641768
#[cfg(feature = "xz")]
17651769
CompressionMethod::Xz => {
1766-
Err(UnsupportedArchive("XZ isn't supported for compression"))
1770+
let level = clamp_opt(compression_level.unwrap_or(6), 0..=9)
1771+
.ok_or(UnsupportedArchive("Unsupported compression level"))?
1772+
as u32;
1773+
Ok(Box::new(move |bare| {
1774+
GenericZipWriter::Xz(xz2::write::XzEncoder::new(bare, level))
1775+
}))
17671776
}
17681777
CompressionMethod::Unsupported(..) => {
17691778
Err(UnsupportedArchive("Unsupported compression"))
@@ -1788,6 +1797,8 @@ impl<W: Write + Seek> GenericZipWriter<W> {
17881797
GenericZipWriter::Bzip2(w) => w.finish()?,
17891798
#[cfg(feature = "zstd")]
17901799
GenericZipWriter::Zstd(w) => w.finish()?,
1800+
#[cfg(feature = "xz")]
1801+
GenericZipWriter::Xz(w) => w.finish()?,
17911802
Closed => {
17921803
return Err(io::Error::new(
17931804
io::ErrorKind::BrokenPipe,
@@ -1813,6 +1824,8 @@ impl<W: Write + Seek> GenericZipWriter<W> {
18131824
GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut dyn Write),
18141825
#[cfg(feature = "zstd")]
18151826
GenericZipWriter::Zstd(ref mut w) => Some(w as &mut dyn Write),
1827+
#[cfg(feature = "xz")]
1828+
GenericZipWriter::Xz(ref mut w) => Some(w as &mut dyn Write),
18161829
Closed => None,
18171830
}
18181831
}

0 commit comments

Comments
 (0)