1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */

17 package org.apache.commons.compress.archivers.zip;
18
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.zip.ZipEntry;
23
24 /**
25  * List of known compression methods
26  *
27  * Many of these methods are currently not supported by commons compress
28  *
29  * @since 1.5
30  */

31 public enum ZipMethod {
32
33     /**
34      * Compression method 0 for uncompressed entries.
35      *
36      * @see ZipEntry#STORED
37      */

38     STORED(ZipEntry.STORED),
39
40     /**
41      * UnShrinking. dynamic Lempel-Ziv-Welch-Algorithm
42      *
43      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
44      */

45     UNSHRINKING(1),
46
47     /**
48      * Reduced with compression factor 1.
49      *
50      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
51      */

52     EXPANDING_LEVEL_1(2),
53
54     /**
55      * Reduced with compression factor 2.
56      *
57      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
58      */

59     EXPANDING_LEVEL_2(3),
60
61     /**
62      * Reduced with compression factor 3.
63      *
64      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
65      */

66     EXPANDING_LEVEL_3(4),
67
68     /**
69      * Reduced with compression factor 4.
70      *
71      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
72      */

73     EXPANDING_LEVEL_4(5),
74
75     /**
76      * Imploding.
77      *
78      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
79      */

80     IMPLODING(6),
81
82     /**
83      * Tokenization.
84      *
85      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
86      */

87     TOKENIZATION(7),
88
89     /**
90      * Compression method 8 for compressed (deflated) entries.
91      *
92      * @see ZipEntry#DEFLATED
93      */

94     DEFLATED(ZipEntry.DEFLATED),
95
96     /**
97      * Compression Method 9 for enhanced deflate.
98      *
99      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
100      */

101     ENHANCED_DEFLATED(9),
102
103     /**
104      * PKWARE Data Compression Library Imploding.
105      *
106      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
107      */

108     PKWARE_IMPLODING(10),
109
110     /**
111      * Compression Method 12 for bzip2.
112      *
113      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
114      */

115     BZIP2(12),
116
117     /**
118      * Compression Method 14 for LZMA.
119      *
120      * @see <a href="https://www.7-zip.org/sdk.html">https://www.7-zip.org/sdk.html</a>
121      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
122      */

123     LZMA(14),
124
125     /**
126      * Compression Method 95 for XZ.
127      *
128      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
129      */

130     XZ(95),
131
132     /**
133      * Compression Method 96 for Jpeg compression.
134      *
135      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
136      */

137     JPEG(96),
138
139     /**
140      * Compression Method 97 for WavPack.
141      *
142      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
143      */

144     WAVPACK(97),
145
146     /**
147      * Compression Method 98 for PPMd.
148      *
149      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
150      */

151     PPMD(98),
152
153     /**
154      * Compression Method 99 for AES encryption.
155      *
156      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
157      */

158     AES_ENCRYPTED(99),
159
160     /**
161      * Unknown compression method.
162      */

163     UNKNOWN();
164
165     static final int UNKNOWN_CODE = -1;
166
167     private static final Map<Integer, ZipMethod> codeToEnum;
168
169     static {
170         final Map<Integer, ZipMethod> cte = new HashMap<>();
171         for (final ZipMethod method : values()) {
172             cte.put(method.getCode(), method);
173         }
174         codeToEnum = Collections.unmodifiableMap(cte);
175     }
176
177     /**
178      * returns the {@link ZipMethod} for the given code or null if the method is not known.
179      *
180      * @param code the code
181      * @return the {@link ZipMethod} for the given code or null if the method is not known.
182      */

183     public static ZipMethod getMethodByCode(final int code) {
184         return codeToEnum.get(code);
185     }
186
187     private final int code;
188
189     ZipMethod() {
190         this(UNKNOWN_CODE);
191     }
192
193     /**
194      * private constructor for enum style class.
195      */

196     ZipMethod(final int code) {
197         this.code = code;
198     }
199
200     /**
201      * the code of the compression method.
202      *
203      * @see ZipArchiveEntry#getMethod()
204      *
205      * @return an integer code for the method
206      */

207     public int getCode() {
208         return code;
209     }
210 }
211