zip 8.5.1

Library to support the reading and writing of zip files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! Possible ZIP compression methods.

use core::fmt;
use std::{fmt::Debug, io};

#[allow(deprecated)]
/// Identifies the storage format used to compress a file within a ZIP archive.
///
/// Each file's compression method is stored alongside it, allowing the
/// contents to be read without context.
///
/// When creating ZIP files, you may choose the method to use with
/// [`crate::write::FileOptions::compression_method`]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "_arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub enum CompressionMethod {
    /// Store the file as is
    Stored,
    /// Compress the file using Deflate
    #[cfg(feature = "_deflate-any")]
    Deflated,
    /// Compress the file using Deflate64.
    /// Decoding deflate64 is supported but encoding deflate64 is not supported.
    #[cfg(feature = "deflate64")]
    Deflate64,
    /// Compress the file using BZIP2
    #[cfg(feature = "_bzip2_any")]
    Bzip2,
    /// Encrypted using AES.
    ///
    /// The actual compression method has to be taken from the AES extra data field
    /// or from `ZipFileData`.
    #[cfg(feature = "aes-crypto")]
    Aes,
    /// Compress the file using `ZStandard`
    #[cfg(feature = "zstd")]
    Zstd,
    /// Compress the file using LZMA
    #[cfg(feature = "lzma")]
    Lzma,
    #[cfg(feature = "legacy-zip")]
    /// Method 1 Shrink
    Shrink,
    #[cfg(feature = "legacy-zip")]
    /// Reduce (Method 2-5)
    Reduce(u8),
    #[cfg(feature = "legacy-zip")]
    /// Method 6 Implode/explode
    Implode,
    /// Compress the file using XZ
    #[cfg(feature = "xz")]
    Xz,
    /// Compress the file using `PPMd`
    #[cfg(feature = "ppmd")]
    Ppmd,
    /// Unsupported compression method
    #[cfg_attr(
        not(any(fuzzing, feature = "_arbitrary")),
        deprecated(since = "0.5.7", note = "use the constants instead")
    )]
    Unsupported(u16),
}
#[allow(deprecated, missing_docs)]
/// All compression methods defined for the ZIP format
impl CompressionMethod {
    pub const STORE: Self = CompressionMethod::Stored;
    #[cfg(feature = "legacy-zip")]
    pub const SHRINK: Self = CompressionMethod::Shrink;
    #[cfg(not(feature = "legacy-zip"))]
    /// Legacy compression method (enable feature `legacy-zip` to get support)
    pub const SHRINK: Self = CompressionMethod::Unsupported(1);
    #[cfg(feature = "legacy-zip")]
    /// Legacy compression method
    pub const REDUCE_1: Self = CompressionMethod::Reduce(1);
    #[cfg(not(feature = "legacy-zip"))]
    /// Legacy compression method (enable feature `legacy-zip` to get support)
    pub const REDUCE_1: Self = CompressionMethod::Unsupported(2);
    #[cfg(feature = "legacy-zip")]
    /// Legacy compression method
    pub const REDUCE_2: Self = CompressionMethod::Reduce(2);
    #[cfg(not(feature = "legacy-zip"))]
    /// Legacy compression method (enable feature `legacy-zip` to get support)
    pub const REDUCE_2: Self = CompressionMethod::Unsupported(3);
    #[cfg(feature = "legacy-zip")]
    /// Legacy compression method
    pub const REDUCE_3: Self = CompressionMethod::Reduce(3);
    #[cfg(not(feature = "legacy-zip"))]
    /// Legacy compression method (enable feature `legacy-zip` to get support)
    pub const REDUCE_3: Self = CompressionMethod::Unsupported(4);
    #[cfg(feature = "legacy-zip")]
    /// Legacy compression method
    pub const REDUCE_4: Self = CompressionMethod::Reduce(4);
    #[cfg(not(feature = "legacy-zip"))]
    /// Legacy compression method (enable feature `legacy-zip` to get support)
    pub const REDUCE_4: Self = CompressionMethod::Unsupported(5);
    #[cfg(feature = "legacy-zip")]
    /// Legacy compression method
    pub const IMPLODE: Self = CompressionMethod::Implode;
    #[cfg(not(feature = "legacy-zip"))]
    /// Legacy compression method (enable feature `legacy-zip` to get support)
    pub const IMPLODE: Self = CompressionMethod::Unsupported(6);
    #[cfg(feature = "_deflate-any")]
    pub const DEFLATE: Self = CompressionMethod::Deflated;
    #[cfg(not(feature = "_deflate-any"))]
    pub const DEFLATE: Self = CompressionMethod::Unsupported(8);
    #[cfg(feature = "deflate64")]
    pub const DEFLATE64: Self = CompressionMethod::Deflate64;
    #[cfg(not(feature = "deflate64"))]
    pub const DEFLATE64: Self = CompressionMethod::Unsupported(9);
    pub const PKWARE_IMPLODE: Self = CompressionMethod::Unsupported(10);
    #[cfg(feature = "_bzip2_any")]
    pub const BZIP2: Self = CompressionMethod::Bzip2;
    #[cfg(not(feature = "_bzip2_any"))]
    pub const BZIP2: Self = CompressionMethod::Unsupported(12);
    #[cfg(not(feature = "lzma"))]
    pub const LZMA: Self = CompressionMethod::Unsupported(14);
    #[cfg(feature = "lzma")]
    pub const LZMA: Self = CompressionMethod::Lzma;
    pub const IBM_ZOS_CMPSC: Self = CompressionMethod::Unsupported(16);
    pub const IBM_TERSE: Self = CompressionMethod::Unsupported(18);
    pub const ZSTD_DEPRECATED: Self = CompressionMethod::Unsupported(20);
    #[cfg(feature = "zstd")]
    pub const ZSTD: Self = CompressionMethod::Zstd;
    #[cfg(not(feature = "zstd"))]
    pub const ZSTD: Self = CompressionMethod::Unsupported(93);
    pub const MP3: Self = CompressionMethod::Unsupported(94);
    #[cfg(feature = "xz")]
    pub const XZ: Self = CompressionMethod::Xz;
    #[cfg(not(feature = "xz"))]
    pub const XZ: Self = CompressionMethod::Unsupported(95);
    pub const JPEG: Self = CompressionMethod::Unsupported(96);
    pub const WAVPACK: Self = CompressionMethod::Unsupported(97);
    #[cfg(feature = "ppmd")]
    pub const PPMD: Self = CompressionMethod::Ppmd;
    #[cfg(not(feature = "ppmd"))]
    pub const PPMD: Self = CompressionMethod::Unsupported(98);
    #[cfg(feature = "aes-crypto")]
    pub const AES: Self = CompressionMethod::Aes;

    // E.4 The compression method field (section 4.4.5) is set to 99
    // to indicate a file has been encrypted using this method.
    #[cfg(not(feature = "aes-crypto"))]
    pub const AES: Self = CompressionMethod::Unsupported(99);

    #[cfg(feature = "_deflate-any")]
    pub const DEFAULT: Self = CompressionMethod::Deflated;

    #[cfg(all(not(feature = "_deflate-any"), feature = "_bzip2_any"))]
    pub const DEFAULT: Self = CompressionMethod::Bzip2;

    #[cfg(all(not(feature = "_deflate-any"), not(feature = "_bzip2_any")))]
    pub const DEFAULT: Self = CompressionMethod::Stored;
}
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")]
            9 => CompressionMethod::Deflate64,
            #[cfg(feature = "_bzip2_any")]
            12 => CompressionMethod::Bzip2,
            #[cfg(feature = "lzma")]
            14 => CompressionMethod::Lzma,
            #[cfg(feature = "xz")]
            95 => CompressionMethod::Xz,
            #[cfg(feature = "zstd")]
            93 => CompressionMethod::Zstd,
            #[cfg(feature = "ppmd")]
            98 => CompressionMethod::Ppmd,
            #[cfg(feature = "aes-crypto")]
            99 => CompressionMethod::Aes,
            #[allow(deprecated)]
            v => CompressionMethod::Unsupported(v),
        }
    }

    /// Converts a u16 to its corresponding `CompressionMethod`
    #[deprecated(
        since = "0.5.7",
        note = "use a constant to construct a compression method"
    )]
    #[must_use]
    pub const fn from_u16(val: u16) -> CompressionMethod {
        Self::parse_from_u16(val)
    }

    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")]
            CompressionMethod::Deflate64 => 9,
            #[cfg(feature = "_bzip2_any")]
            CompressionMethod::Bzip2 => 12,
            #[cfg(feature = "zstd")]
            CompressionMethod::Zstd => 93,
            #[cfg(feature = "lzma")]
            CompressionMethod::Lzma => 14,
            #[cfg(feature = "xz")]
            CompressionMethod::Xz => 95,
            #[cfg(feature = "ppmd")]
            CompressionMethod::Ppmd => 98,
            // E.4 The compression method field (section 4.4.5) is set to 99
            // to indicate a file has been encrypted using this method.
            #[cfg(feature = "aes-crypto")]
            CompressionMethod::Aes => 99,
            #[allow(deprecated)]
            CompressionMethod::Unsupported(v) => v,
        }
    }

    /// Converts a `CompressionMethod` to a u16
    #[deprecated(
        since = "0.5.7",
        note = "to match on other compression methods, use a constant"
    )]
    #[must_use]
    pub const fn to_u16(self) -> u16 {
        self.serialize_to_u16()
    }
}

impl Default for CompressionMethod {
    #[cfg(feature = "_deflate-any")]
    fn default() -> Self {
        CompressionMethod::Deflated
    }

    #[cfg(not(feature = "_deflate-any"))]
    fn default() -> Self {
        CompressionMethod::Stored
    }
}

impl fmt::Display for CompressionMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Just duplicate what the Debug format looks like, i.e, the enum key:
        write!(f, "{self:?}")
    }
}

/// The compression methods which have been implemented.
pub const SUPPORTED_COMPRESSION_METHODS: &[CompressionMethod] = &[
    CompressionMethod::Stored,
    #[cfg(feature = "_deflate-any")]
    CompressionMethod::Deflated,
    #[cfg(feature = "deflate64")]
    CompressionMethod::Deflate64,
    #[cfg(feature = "_bzip2_any")]
    CompressionMethod::Bzip2,
    #[cfg(feature = "zstd")]
    CompressionMethod::Zstd,
    #[cfg(feature = "xz")]
    CompressionMethod::Xz,
    #[cfg(feature = "ppmd")]
    CompressionMethod::Ppmd,
];

pub(crate) enum Decompressor<R: io::BufRead> {
    Stored(R),
    #[cfg(feature = "deflate-flate2")]
    Deflated(flate2::bufread::DeflateDecoder<R>),
    #[cfg(feature = "deflate64")]
    Deflate64(deflate64::Deflate64Decoder<R>),
    #[cfg(feature = "_bzip2_any")]
    Bzip2(bzip2::bufread::BzDecoder<R>),
    #[cfg(feature = "zstd")]
    Zstd(zstd::Decoder<'static, R>),
    #[cfg(feature = "lzma")]
    Lzma(Lzma<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(Box<lzma_rust2::XzReader<R>>),
    #[cfg(feature = "ppmd")]
    Ppmd(Ppmd<R>),
}

impl<R: io::BufRead> Debug for Decompressor<R> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Stored(_) => write!(f, "StoredDecompressor"),
            #[cfg(feature = "deflate-flate2")]
            Self::Deflated(_) => write!(f, "DeflatedDecompressor"),
            #[cfg(feature = "deflate64")]
            Self::Deflate64(_) => write!(f, "Deflate64Decompressor"),
            #[cfg(feature = "_bzip2_any")]
            Self::Bzip2(_) => write!(f, "Bzip2Decompressor"),
            #[cfg(feature = "zstd")]
            Self::Zstd(_) => write!(f, "ZstdDecompressor"),
            #[cfg(feature = "lzma")]
            Self::Lzma(_) => write!(f, "LzmaDecompressor"),
            #[cfg(feature = "legacy-zip")]
            Self::Shrink(_) => write!(f, "ShrinkDecompressor"),
            #[cfg(feature = "legacy-zip")]
            Self::Reduce(_) => write!(f, "ReduceDecompressor"),
            #[cfg(feature = "legacy-zip")]
            Self::Implode(_) => write!(f, "ImplodeDecompressor"),
            #[cfg(feature = "xz")]
            Self::Xz(_) => write!(f, "XzDecompressor"),
            #[cfg(feature = "ppmd")]
            Self::Ppmd(_) => write!(f, "PpmdDecompressor"),
        }
    }
}

#[cfg(feature = "lzma")]
pub(crate) enum Lzma<R: io::BufRead> {
    Uninitialized {
        reader: Option<R>,
        uncompressed_size: u64,
    },
    Initialized(Box<lzma_rust2::LzmaReader<R>>),
}

#[cfg(feature = "ppmd")]
pub(crate) enum Ppmd<R: io::BufRead> {
    Uninitialized(Option<R>),
    Initialized(Box<ppmd_rust::Ppmd8Decoder<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-flate2")]
            Decompressor::Deflated(r) => r.read(buf),
            #[cfg(feature = "deflate64")]
            Decompressor::Deflate64(r) => r.read(buf),
            #[cfg(feature = "_bzip2_any")]
            Decompressor::Bzip2(r) => r.read(buf),
            #[cfg(feature = "zstd")]
            Decompressor::Zstd(r) => r.read(buf),
            #[cfg(feature = "lzma")]
            Decompressor::Lzma(r) => match r {
                Lzma::Uninitialized {
                    reader,
                    uncompressed_size,
                } => {
                    let mut reader = reader.take().ok_or_else(|| {
                        io::Error::other("Reader was not set while reading LZMA data")
                    })?;

                    // 5.8.8.1 LZMA Version Information & 5.8.8.2 LZMA Properties Size
                    let mut header = [0; 4];
                    reader.read_exact(&mut header)?;
                    let _version_information =
                        u16::from_le_bytes(header[0..2].try_into().map_err(|e| {
                            std::io::Error::other(format!("Cannot transform header to u16: {e}"))
                        })?);
                    let properties_size =
                        u16::from_le_bytes(header[2..4].try_into().map_err(|e| {
                            std::io::Error::other(format!("Cannot transform header to u16: {e}"))
                        })?);
                    if properties_size != 5 {
                        return Err(io::Error::new(
                            io::ErrorKind::InvalidInput,
                            format!("unexpected LZMA properties size of {properties_size}"),
                        ));
                    }

                    let mut props_data = [0; 5];
                    reader.read_exact(&mut props_data)?;
                    let props = props_data[0];
                    let dict_size =
                        u32::from_le_bytes(props_data[1..5].try_into().map_err(|e| {
                            std::io::Error::other(format!("Cannot transform header to u32: {e}"))
                        })?);

                    // We don't need to handle the end-of-stream marker here, since the LZMA reader
                    // stops at the end-of-stream marker OR when it has decoded uncompressed_size bytes, whichever comes first.
                    let mut decompressor = lzma_rust2::LzmaReader::new_with_props(
                        reader,
                        *uncompressed_size,
                        props,
                        dict_size,
                        None,
                    )?;

                    let read = decompressor.read(buf)?;

                    *r = Lzma::Initialized(Box::new(decompressor));

                    Ok(read)
                }
                Lzma::Initialized(decompressor) => decompressor.read(buf),
            },
            #[cfg(feature = "xz")]
            Decompressor::Xz(r) => r.read(buf),
            #[cfg(feature = "ppmd")]
            Decompressor::Ppmd(r) => match r {
                Ppmd::Uninitialized(reader) => {
                    let mut reader = reader.take().ok_or_else(|| {
                        io::Error::other("Reader was not set while reading PPMd data")
                    })?;

                    let mut buffer = [0; 2];
                    reader.read_exact(&mut buffer)?;
                    let parameters = u16::from_le_bytes(buffer);

                    let order = u32::from((parameters & 0x0F) + 1);
                    let memory_size = 1024 * 1024 * u32::from(((parameters >> 4) & 0xFF) + 1);
                    let restoration_method = (parameters >> 12) & 0x0F;

                    let mut decompressor = ppmd_rust::Ppmd8Decoder::new(
                        reader,
                        order,
                        memory_size,
                        restoration_method.into(),
                    )
                    .map_err(|error| match error {
                        ppmd_rust::Error::RangeDecoderInitialization => io::Error::new(
                            io::ErrorKind::InvalidData,
                            "PPMd range coder initialization failed",
                        ),
                        ppmd_rust::Error::InvalidParameter => {
                            io::Error::new(io::ErrorKind::InvalidInput, "Invalid PPMd parameter")
                        }
                        ppmd_rust::Error::IoError(io_error) => io_error,
                        ppmd_rust::Error::MemoryAllocation => {
                            io::Error::new(io::ErrorKind::OutOfMemory, "Memory allocation failed")
                        }
                    })?;

                    let read = decompressor.read(buf)?;

                    *r = Ppmd::Initialized(Box::new(decompressor));

                    Ok(read)
                }
                Ppmd::Initialized(decompressor) => decompressor.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),
        }
    }
}

impl<R: io::BufRead> Decompressor<R> {
    pub fn new(
        reader: R,
        compression_method: CompressionMethod,
        #[cfg_attr(not(any(feature = "lzma", feature = "legacy-zip")), allow(unused))]
        uncompressed_size: u64,
        #[cfg_attr(not(feature = "legacy-zip"), allow(unused))] flags: u16,
    ) -> crate::result::ZipResult<Self> {
        Ok(match compression_method {
            CompressionMethod::Stored => Decompressor::Stored(reader),
            #[cfg(feature = "deflate-flate2")]
            CompressionMethod::Deflated => {
                Decompressor::Deflated(flate2::bufread::DeflateDecoder::new(reader))
            }
            #[cfg(feature = "deflate64")]
            CompressionMethod::Deflate64 => {
                Decompressor::Deflate64(deflate64::Deflate64Decoder::with_buffer(reader))
            }
            #[cfg(feature = "_bzip2_any")]
            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(Lzma::Uninitialized {
                reader: Some(reader),
                uncompressed_size,
            }),
            #[cfg(feature = "xz")]
            CompressionMethod::Xz => {
                Decompressor::Xz(Box::new(lzma_rust2::XzReader::new(reader, false)))
            }
            #[cfg(feature = "ppmd")]
            CompressionMethod::Ppmd => Decompressor::Ppmd(Ppmd::Uninitialized(Some(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),
            ),
            _ => {
                return Err(crate::result::ZipError::UnsupportedArchive(
                    "Compression method not supported",
                ));
            }
        })
    }

    /// Consumes this decoder, returning the underlying reader.
    #[allow(clippy::infallible_destructuring_match)]
    pub fn into_inner(self) -> io::Result<R> {
        let inner = match self {
            Decompressor::Stored(r) => r,
            #[cfg(feature = "deflate-flate2")]
            Decompressor::Deflated(r) => r.into_inner(),
            #[cfg(feature = "deflate64")]
            Decompressor::Deflate64(r) => r.into_inner(),
            #[cfg(feature = "_bzip2_any")]
            Decompressor::Bzip2(r) => r.into_inner(),
            #[cfg(feature = "zstd")]
            Decompressor::Zstd(r) => r.finish(),
            #[cfg(feature = "lzma")]
            Decompressor::Lzma(r) => match r {
                Lzma::Uninitialized { mut reader, .. } => reader
                    .take()
                    .ok_or_else(|| io::Error::other("Reader was not set"))?,
                Lzma::Initialized(decoder) => decoder.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(),
            #[cfg(feature = "ppmd")]
            Decompressor::Ppmd(r) => match r {
                Ppmd::Uninitialized(mut reader) => reader
                    .take()
                    .ok_or_else(|| io::Error::other("Reader was not set"))?,
                Ppmd::Initialized(decoder) => decoder.into_inner(),
            },
        };
        Ok(inner)
    }
}

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

    #[test]
    fn from_eq_to() {
        for v in 0..(u32::from(u16::MAX) + 1) {
            let from = CompressionMethod::parse_from_u16(v as u16);
            let to = u32::from(from.serialize_to_u16());
            assert_eq!(v, to);
        }
    }

    #[test]
    fn to_eq_from() {
        fn check_match(method: CompressionMethod) {
            let to = method.serialize_to_u16();
            let from = CompressionMethod::parse_from_u16(to);
            let back = from.serialize_to_u16();
            assert_eq!(to, back);
        }

        for &method in SUPPORTED_COMPRESSION_METHODS {
            check_match(method);
        }
    }

    #[test]
    fn to_display_fmt() {
        fn check_match(method: CompressionMethod) {
            let debug_str = format!("{method:?}");
            let display_str = format!("{method}");
            assert_eq!(debug_str, display_str);
        }

        for &method in SUPPORTED_COMPRESSION_METHODS {
            check_match(method);
        }
    }
}