Skip to content

Commit

Permalink
Merge pull request #210 from a1phyr/multiple_refactors
Browse files Browse the repository at this point in the history
Multiple refactors
  • Loading branch information
Pr0methean authored Jul 20, 2024
2 parents 9caa3b6 + 7471cf5 commit a60bd79
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 266 deletions.
88 changes: 87 additions & 1 deletion src/compression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Possible ZIP compression methods.
use std::fmt;
use std::{fmt, io};

#[allow(deprecated)]
/// Identifies the storage format used to compress a file within a ZIP archive.
Expand Down Expand Up @@ -189,6 +189,92 @@ pub const SUPPORTED_COMPRESSION_METHODS: &[CompressionMethod] = &[
CompressionMethod::Zstd,
];

pub(crate) enum Decompressor<R: io::BufRead> {
Stored(R),
#[cfg(feature = "_deflate-any")]
Deflated(flate2::bufread::DeflateDecoder<R>),
#[cfg(feature = "deflate64")]
Deflate64(deflate64::Deflate64Decoder<R>),
#[cfg(feature = "bzip2")]
Bzip2(bzip2::bufread::BzDecoder<R>),
#[cfg(feature = "zstd")]
Zstd(zstd::Decoder<'static, R>),
#[cfg(feature = "lzma")]
Lzma(Box<crate::read::lzma::LzmaDecoder<R>>),
#[cfg(feature = "xz")]
Xz(crate::read::xz::XzDecoder<R>),
}

impl<R: io::BufRead> io::Read for Decompressor<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
Decompressor::Stored(r) => r.read(buf),
#[cfg(feature = "_deflate-any")]
Decompressor::Deflated(r) => r.read(buf),
#[cfg(feature = "deflate64")]
Decompressor::Deflate64(r) => r.read(buf),
#[cfg(feature = "bzip2")]
Decompressor::Bzip2(r) => r.read(buf),
#[cfg(feature = "zstd")]
Decompressor::Zstd(r) => r.read(buf),
#[cfg(feature = "lzma")]
Decompressor::Lzma(r) => r.read(buf),
#[cfg(feature = "xz")]
Decompressor::Xz(r) => r.read(buf),
}
}
}

impl<R: io::BufRead> Decompressor<R> {
pub fn new(reader: R, compression_method: CompressionMethod) -> crate::result::ZipResult<Self> {
Ok(match compression_method {
CompressionMethod::Stored => Decompressor::Stored(reader),
#[cfg(feature = "_deflate-any")]
CompressionMethod::Deflated => {
Decompressor::Deflated(flate2::bufread::DeflateDecoder::new(reader))
}
#[cfg(feature = "deflate64")]
CompressionMethod::Deflate64 => {
Decompressor::Deflate64(deflate64::Deflate64Decoder::with_buffer(reader))
}
#[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => Decompressor::Bzip2(bzip2::bufread::BzDecoder::new(reader)),
#[cfg(feature = "zstd")]
CompressionMethod::Zstd => Decompressor::Zstd(zstd::Decoder::with_buffer(reader)?),
#[cfg(feature = "lzma")]
CompressionMethod::Lzma => {
Decompressor::Lzma(Box::new(crate::read::lzma::LzmaDecoder::new(reader)))
}
#[cfg(feature = "xz")]
CompressionMethod::Xz => Decompressor::Xz(crate::read::xz::XzDecoder::new(reader)),
_ => {
return Err(crate::result::ZipError::UnsupportedArchive(
"Compression method not supported",
))
}
})
}

/// Consumes this decoder, returning the underlying reader.
pub fn into_inner(self) -> R {
match self {
Decompressor::Stored(r) => r,
#[cfg(feature = "_deflate-any")]
Decompressor::Deflated(r) => r.into_inner(),
#[cfg(feature = "deflate64")]
Decompressor::Deflate64(r) => r.into_inner(),
#[cfg(feature = "bzip2")]
Decompressor::Bzip2(r) => r.into_inner(),
#[cfg(feature = "zstd")]
Decompressor::Zstd(r) => r.finish(),
#[cfg(feature = "lzma")]
Decompressor::Lzma(r) => r.into_inner(),
#[cfg(feature = "xz")]
Decompressor::Xz(r) => r.into_inner(),
}
}
}

#[cfg(test)]
mod test {
use super::{CompressionMethod, SUPPORTED_COMPRESSION_METHODS};
Expand Down
Loading

0 comments on commit a60bd79

Please sign in to comment.