Skip to content

Added legacy shrink/reduce/implode compression. #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/zip-rs/zip2.git"
keywords = ["zip", "archive", "compression"]
# Any change to rust-version must be reflected also in `README.md` and `.github/workflows/ci.yaml`.
# The MSRV policy is documented in `README.md`.
rust-version = "1.75.0"
rust-version = "1.83.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is version 1.83.0 needed? I won't release this change until the MSRV has been stable for 6 months; for 1.83.0 that would mean 2025-05-28.

Also, an MSRV change needs to be reflected in README.md and .github/workflows/ci.yaml; the latter is why CI is failing for this PR.

description = """
Library to support the reading and writing of zip files.
"""
Expand Down Expand Up @@ -49,6 +49,7 @@ zstd = { version = "0.13", optional = true, default-features = false }
zopfli = { version = "0.8", optional = true }
deflate64 = { version = "0.1.9", optional = true }
lzma-rs = { version = "0.3", default-features = false, optional = true }
bitstream-io = { version = "2.6.0", optional = true }
xz2 = { version = "0.1.7", optional = true }

[target.'cfg(fuzzing)'.dependencies]
Expand Down Expand Up @@ -80,6 +81,7 @@ jiff-02 = ["dep:jiff"]
nt-time = ["dep:nt-time"]
lzma = ["lzma-rs/stream"]
unreserved = []
legacy-zip = ["bitstream-io"]
xz = ["dep:xz2"]
default = [
"aes-crypto",
Expand Down
78 changes: 71 additions & 7 deletions src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ pub enum CompressionMethod {
/// Compress the file using LZMA
#[cfg(feature = "lzma")]
Lzma,
/// Legacy format
#[cfg(feature = "legacy-zip")]
Shrink,
/// Reduce (Method 2-5)
#[cfg(feature = "legacy-zip")]
Reduce(u8),
/// Method 6 Implode/explode
#[cfg(feature = "legacy-zip")]
Implode,
/// Compress the file using XZ
#[cfg(feature = "xz")]
Xz,
Expand All @@ -52,12 +61,18 @@ pub enum CompressionMethod {
/// All compression methods defined for the ZIP format
impl CompressionMethod {
pub const STORE: Self = CompressionMethod::Stored;
pub const SHRINK: Self = CompressionMethod::Unsupported(1);
pub const REDUCE_1: Self = CompressionMethod::Unsupported(2);
pub const REDUCE_2: Self = CompressionMethod::Unsupported(3);
pub const REDUCE_3: Self = CompressionMethod::Unsupported(4);
pub const REDUCE_4: Self = CompressionMethod::Unsupported(5);
pub const IMPLODE: Self = CompressionMethod::Unsupported(6);
#[cfg(feature = "legacy-zip")]
pub const SHRINK: Self = CompressionMethod::Shrink;
#[cfg(feature = "legacy-zip")]
pub const REDUCE_1: Self = CompressionMethod::Reduce(1);
#[cfg(feature = "legacy-zip")]
pub const REDUCE_2: Self = CompressionMethod::Reduce(2);
#[cfg(feature = "legacy-zip")]
pub const REDUCE_3: Self = CompressionMethod::Reduce(3);
#[cfg(feature = "legacy-zip")]
pub const REDUCE_4: Self = CompressionMethod::Reduce(4);
#[cfg(feature = "legacy-zip")]
pub const IMPLODE: Self = CompressionMethod::Implode;
#[cfg(feature = "_deflate-any")]
pub const DEFLATE: Self = CompressionMethod::Deflated;
#[cfg(not(feature = "_deflate-any"))]
Expand Down Expand Up @@ -99,6 +114,18 @@ impl CompressionMethod {
pub(crate) const fn parse_from_u16(val: u16) -> Self {
match val {
0 => CompressionMethod::Stored,
#[cfg(feature = "legacy-zip")]
1 => CompressionMethod::Shrink,
#[cfg(feature = "legacy-zip")]
2 => CompressionMethod::Reduce(1),
#[cfg(feature = "legacy-zip")]
3 => CompressionMethod::Reduce(2),
#[cfg(feature = "legacy-zip")]
4 => CompressionMethod::Reduce(3),
#[cfg(feature = "legacy-zip")]
5 => CompressionMethod::Reduce(4),
#[cfg(feature = "legacy-zip")]
6 => CompressionMethod::Implode,
#[cfg(feature = "_deflate-any")]
8 => CompressionMethod::Deflated,
#[cfg(feature = "deflate64")]
Expand Down Expand Up @@ -130,6 +157,13 @@ impl CompressionMethod {
pub(crate) const fn serialize_to_u16(self) -> u16 {
match self {
CompressionMethod::Stored => 0,
#[cfg(feature = "legacy-zip")]
CompressionMethod::Shrink => 1,
#[cfg(feature = "legacy-zip")]
CompressionMethod::Reduce(n) => 1 + n as u16,
#[cfg(feature = "legacy-zip")]
CompressionMethod::Implode => 6,

#[cfg(feature = "_deflate-any")]
CompressionMethod::Deflated => 8,
#[cfg(feature = "deflate64")]
Expand Down Expand Up @@ -203,6 +237,12 @@ pub(crate) enum Decompressor<R: io::BufRead> {
Zstd(zstd::Decoder<'static, R>),
#[cfg(feature = "lzma")]
Lzma(Box<crate::read::lzma::LzmaDecoder<R>>),
#[cfg(feature = "legacy-zip")]
Shrink(crate::legacy::shrink::ShrinkDecoder<R>),
#[cfg(feature = "legacy-zip")]
Reduce(crate::legacy::reduce::ReduceDecoder<R>),
#[cfg(feature = "legacy-zip")]
Implode(crate::legacy::implode::ImplodeDecoder<R>),
#[cfg(feature = "xz")]
Xz(xz2::bufread::XzDecoder<R>),
}
Expand All @@ -211,6 +251,12 @@ 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 = "legacy-zip")]
Decompressor::Shrink(r) => r.read(buf),
#[cfg(feature = "legacy-zip")]
Decompressor::Reduce(r) => r.read(buf),
#[cfg(feature = "legacy-zip")]
Decompressor::Implode(r) => r.read(buf),
#[cfg(feature = "deflate-flate2")]
Decompressor::Deflated(r) => r.read(buf),
#[cfg(feature = "deflate64")]
Expand All @@ -228,9 +274,21 @@ impl<R: io::BufRead> io::Read for Decompressor<R> {
}

impl<R: io::BufRead> Decompressor<R> {
pub fn new(reader: R, compression_method: CompressionMethod) -> crate::result::ZipResult<Self> {
pub fn new(reader: R, compression_method: CompressionMethod, _uncompressed_size: u64, _flags: u16) -> crate::result::ZipResult<Self> {
Ok(match compression_method {
CompressionMethod::Stored => Decompressor::Stored(reader),
#[cfg(feature = "legacy-zip")]
CompressionMethod::Shrink => {
Decompressor::Shrink(crate::legacy::shrink::ShrinkDecoder::new(reader, _uncompressed_size))
}
#[cfg(feature = "legacy-zip")]
CompressionMethod::Reduce(n) => {
Decompressor::Reduce(crate::legacy::reduce::ReduceDecoder::new(reader, _uncompressed_size, n))
}
#[cfg(feature = "legacy-zip")]
CompressionMethod::Implode => {
Decompressor::Implode(crate::legacy::implode::ImplodeDecoder::new(reader, _uncompressed_size, _flags))
}
#[cfg(feature = "deflate-flate2")]
CompressionMethod::Deflated => {
Decompressor::Deflated(flate2::bufread::DeflateDecoder::new(reader))
Expand Down Expand Up @@ -271,6 +329,12 @@ impl<R: io::BufRead> Decompressor<R> {
Decompressor::Zstd(r) => r.finish(),
#[cfg(feature = "lzma")]
Decompressor::Lzma(r) => r.into_inner(),
#[cfg(feature = "legacy-zip")]
Decompressor::Shrink(r) => r.into_inner(),
#[cfg(feature = "legacy-zip")]
Decompressor::Reduce(r) => r.into_inner(),
#[cfg(feature = "legacy-zip")]
Decompressor::Implode(r) => r.into_inner(),
#[cfg(feature = "xz")]
Decompressor::Xz(r) => r.into_inner(),
}
Expand Down
Loading
Loading