Skip to main content

zip/format/
compression.rs

1//! Compression
2
3/// Compression
4#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
5#[repr(u16)]
6#[non_exhaustive]
7pub enum Compression {
8    /// Store the file as is
9    #[default]
10    Stored = 0,
11    /// Method 1 Shrink
12    Shrink = 1,
13    /// Reduce with compression factor 1
14    ReduceFactor1 = 2,
15    /// Reduce with compression factor 2
16    ReduceFactor2 = 3,
17    /// Reduce with compression factor 3
18    ReduceFactor3 = 4,
19    /// Reduce with compression factor 4
20    ReduceFactor4 = 5,
21    /// Implode/explode
22    Implode = 6,
23    /// Reserved for Tokenizing compression algorithm
24    TokenizingCompression = 7,
25    /// Compress the file using Deflate
26    Deflated = 8,
27    /// Compress the file using Deflate64.
28    Deflate64 = 9,
29    /// PKWARE Data Compression Library Imploding (old IBM TERSE)
30    PkwareDataCompressionImploding = 10,
31    /// Reserved by PKWARE
32    Reserved11 = 11,
33    /// Compress the file using BZIP2
34    Bzip2 = 12,
35    /// Reserved by PKWARE
36    Reserved13 = 13,
37    /// Compress the file using LZMA
38    Lzma = 14,
39    /// Reserved by PKWARE
40    Reserved15 = 15,
41    /// IBM z/OS CMPSC Compression
42    CmpscCompression = 16,
43    /// Reserved by PKWARE
44    Reserved17 = 17,
45    /// File is compressed using IBM TERSE (new)
46    IbmTerseNew = 18,
47    /// IBM LZ77 z Architecture
48    IbmLz77 = 19,
49    /// deprecated ZSTD
50    DeprecatedZstd = 20,
51    /// Compress the file using `ZStandard`
52    Zstd = 93,
53    /// MP3 Compression
54    Mp3 = 94,
55    /// Compress the file using XZ
56    Xz = 95,
57    /// JPEG variant
58    Jpeg = 96,
59    /// WavPack compressed data
60    WavPack = 97,
61    /// Compress the file using `PPMd`
62    Ppmd = 98,
63    /// Encrypted using AES.
64    ///
65    /// The actual compression method has to be taken from the AES extra data field
66    /// or from `ZipFileData`.
67    Aes = 99,
68    /// Unsupported compression method
69    Unknown(u16),
70}
71
72impl From<u16> for Compression {
73    fn from(value: u16) -> Self {
74        match value {
75            0 => Self::Stored,
76            1 => Self::Shrink,
77            2 => Self::ReduceFactor1,
78            3 => Self::ReduceFactor2,
79            4 => Self::ReduceFactor3,
80            5 => Self::ReduceFactor4,
81            6 => Self::Implode,
82            7 => Self::TokenizingCompression,
83            8 => Self::Deflated,
84            9 => Self::Deflate64,
85            10 => Self::PkwareDataCompressionImploding,
86            11 => Self::Reserved11,
87            12 => Self::Bzip2,
88            13 => Self::Reserved13,
89            14 => Self::Lzma,
90            15 => Self::Reserved15,
91            16 => Self::CmpscCompression,
92            17 => Self::Reserved17,
93            18 => Self::IbmTerseNew,
94            19 => Self::IbmLz77,
95            20 => Self::DeprecatedZstd,
96            93 => Self::Zstd,
97            94 => Self::Mp3,
98            95 => Self::Xz,
99            96 => Self::Jpeg,
100            97 => Self::WavPack,
101            98 => Self::Ppmd,
102            99 => Self::Aes,
103            n => Self::Unknown(n),
104        }
105    }
106}
107
108impl From<Compression> for u16 {
109    fn from(value: Compression) -> Self {
110        match value {
111            Compression::Stored => 0,
112            Compression::Shrink => 1,
113            Compression::ReduceFactor1 => 2,
114            Compression::ReduceFactor2 => 3,
115            Compression::ReduceFactor3 => 4,
116            Compression::ReduceFactor4 => 5,
117            Compression::Implode => 6,
118            Compression::TokenizingCompression => 7,
119            Compression::Deflated => 8,
120            Compression::Deflate64 => 9,
121            Compression::PkwareDataCompressionImploding => 10,
122            Compression::Reserved11 => 11,
123            Compression::Bzip2 => 12,
124            Compression::Reserved13 => 13,
125            Compression::Lzma => 14,
126            Compression::Reserved15 => 15,
127            Compression::CmpscCompression => 16,
128            Compression::Reserved17 => 17,
129            Compression::IbmTerseNew => 18,
130            Compression::IbmLz77 => 19,
131            Compression::DeprecatedZstd => 20,
132            Compression::Zstd => 93,
133            Compression::Mp3 => 94,
134            Compression::Xz => 95,
135            Compression::Jpeg => 96,
136            Compression::WavPack => 97,
137            Compression::Ppmd => 98,
138            Compression::Aes => 99,
139            Compression::Unknown(value) => value,
140        }
141    }
142}
143
144impl Compression {
145    /// as u16
146    #[must_use]
147    pub fn as_u16(self) -> u16 {
148        u16::from(self)
149    }
150}