• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Possible ZIP compression methods.
2 
3 use std::fmt;
4 
5 #[allow(deprecated)]
6 /// Identifies the storage format used to compress a file within a ZIP archive.
7 ///
8 /// Each file's compression method is stored alongside it, allowing the
9 /// contents to be read without context.
10 ///
11 /// When creating ZIP files, you may choose the method to use with
12 /// [`zip::write::FileOptions::compression_method`]
13 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
14 pub enum CompressionMethod {
15     /// Store the file as is
16     Stored,
17     /// Compress the file using Deflate
18     #[cfg(any(
19         feature = "deflate",
20         feature = "deflate-miniz",
21         feature = "deflate-zlib"
22     ))]
23     Deflated,
24     /// Compress the file using BZIP2
25     #[cfg(feature = "bzip2")]
26     Bzip2,
27     /// Unsupported compression method
28     #[deprecated(since = "0.5.7", note = "use the constants instead")]
29     Unsupported(u16),
30 }
31 #[allow(deprecated, missing_docs)]
32 /// All compression methods defined for the ZIP format
33 impl CompressionMethod {
34     pub const STORE: Self = CompressionMethod::Stored;
35     pub const SHRINK: Self = CompressionMethod::Unsupported(1);
36     pub const REDUCE_1: Self = CompressionMethod::Unsupported(2);
37     pub const REDUCE_2: Self = CompressionMethod::Unsupported(3);
38     pub const REDUCE_3: Self = CompressionMethod::Unsupported(4);
39     pub const REDUCE_4: Self = CompressionMethod::Unsupported(5);
40     pub const IMPLODE: Self = CompressionMethod::Unsupported(6);
41     #[cfg(any(
42         feature = "deflate",
43         feature = "deflate-miniz",
44         feature = "deflate-zlib"
45     ))]
46     pub const DEFLATE: Self = CompressionMethod::Deflated;
47     #[cfg(not(any(
48         feature = "deflate",
49         feature = "deflate-miniz",
50         feature = "deflate-zlib"
51     )))]
52     pub const DEFLATE: Self = CompressionMethod::Unsupported(8);
53     pub const DEFLATE64: Self = CompressionMethod::Unsupported(9);
54     pub const PKWARE_IMPLODE: Self = CompressionMethod::Unsupported(10);
55     #[cfg(feature = "bzip2")]
56     pub const BZIP2: Self = CompressionMethod::Bzip2;
57     #[cfg(not(feature = "bzip2"))]
58     pub const BZIP2: Self = CompressionMethod::Unsupported(12);
59     pub const LZMA: Self = CompressionMethod::Unsupported(14);
60     pub const IBM_ZOS_CMPSC: Self = CompressionMethod::Unsupported(16);
61     pub const IBM_TERSE: Self = CompressionMethod::Unsupported(18);
62     pub const ZSTD_DEPRECATED: Self = CompressionMethod::Unsupported(20);
63     pub const ZSTD: Self = CompressionMethod::Unsupported(93);
64     pub const MP3: Self = CompressionMethod::Unsupported(94);
65     pub const XZ: Self = CompressionMethod::Unsupported(95);
66     pub const JPEG: Self = CompressionMethod::Unsupported(96);
67     pub const WAVPACK: Self = CompressionMethod::Unsupported(97);
68     pub const PPMD: Self = CompressionMethod::Unsupported(98);
69 }
70 impl CompressionMethod {
71     /// Converts an u16 to its corresponding CompressionMethod
72     #[deprecated(
73         since = "0.5.7",
74         note = "use a constant to construct a compression method"
75     )]
from_u16(val: u16) -> CompressionMethod76     pub fn from_u16(val: u16) -> CompressionMethod {
77         #[allow(deprecated)]
78         match val {
79             0 => CompressionMethod::Stored,
80             #[cfg(any(
81                 feature = "deflate",
82                 feature = "deflate-miniz",
83                 feature = "deflate-zlib"
84             ))]
85             8 => CompressionMethod::Deflated,
86             #[cfg(feature = "bzip2")]
87             12 => CompressionMethod::Bzip2,
88 
89             v => CompressionMethod::Unsupported(v),
90         }
91     }
92 
93     /// Converts a CompressionMethod to a u16
94     #[deprecated(
95         since = "0.5.7",
96         note = "to match on other compression methods, use a constant"
97     )]
to_u16(self) -> u1698     pub fn to_u16(self) -> u16 {
99         #[allow(deprecated)]
100         match self {
101             CompressionMethod::Stored => 0,
102             #[cfg(any(
103                 feature = "deflate",
104                 feature = "deflate-miniz",
105                 feature = "deflate-zlib"
106             ))]
107             CompressionMethod::Deflated => 8,
108             #[cfg(feature = "bzip2")]
109             CompressionMethod::Bzip2 => 12,
110             CompressionMethod::Unsupported(v) => v,
111         }
112     }
113 }
114 
115 impl fmt::Display for CompressionMethod {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result116     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117         // Just duplicate what the Debug format looks like, i.e, the enum key:
118         write!(f, "{:?}", self)
119     }
120 }
121 
122 #[cfg(test)]
123 mod test {
124     use super::CompressionMethod;
125 
126     #[test]
from_eq_to()127     fn from_eq_to() {
128         for v in 0..(::std::u16::MAX as u32 + 1) {
129             #[allow(deprecated)]
130             let from = CompressionMethod::from_u16(v as u16);
131             #[allow(deprecated)]
132             let to = from.to_u16() as u32;
133             assert_eq!(v, to);
134         }
135     }
136 
methods() -> Vec<CompressionMethod>137     fn methods() -> Vec<CompressionMethod> {
138         let mut methods = Vec::new();
139         methods.push(CompressionMethod::Stored);
140         #[cfg(any(
141             feature = "deflate",
142             feature = "deflate-miniz",
143             feature = "deflate-zlib"
144         ))]
145         methods.push(CompressionMethod::Deflated);
146         #[cfg(feature = "bzip2")]
147         methods.push(CompressionMethod::Bzip2);
148         methods
149     }
150 
151     #[test]
to_eq_from()152     fn to_eq_from() {
153         fn check_match(method: CompressionMethod) {
154             #[allow(deprecated)]
155             let to = method.to_u16();
156             #[allow(deprecated)]
157             let from = CompressionMethod::from_u16(to);
158             #[allow(deprecated)]
159             let back = from.to_u16();
160             assert_eq!(to, back);
161         }
162 
163         for method in methods() {
164             check_match(method);
165         }
166     }
167 
168     #[test]
to_display_fmt()169     fn to_display_fmt() {
170         fn check_match(method: CompressionMethod) {
171             let debug_str = format!("{:?}", method);
172             let display_str = format!("{}", method);
173             assert_eq!(debug_str, display_str);
174         }
175 
176         for method in methods() {
177             check_match(method);
178         }
179     }
180 }
181