View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.archivers.zip;
20  
21  import java.util.Collections;
22  import java.util.Map;
23  import java.util.function.Function;
24  import java.util.stream.Collectors;
25  import java.util.stream.Stream;
26  import java.util.zip.ZipEntry;
27  
28  /**
29   * Enumerates known compression methods.
30   *
31   * Some of these methods are currently not supported by commons compress.
32   *
33   * @since 1.5
34   */
35  public enum ZipMethod {
36  
37      /**
38       * Compression method 0 for uncompressed entries.
39       *
40       * @see ZipEntry#STORED
41       */
42      STORED(ZipEntry.STORED),
43  
44      /**
45       * UnShrinking. dynamic Lempel-Ziv-Welch-Algorithm
46       *
47       * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
48       */
49      UNSHRINKING(1),
50  
51      /**
52       * Reduced with compression factor 1.
53       *
54       * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
55       */
56      EXPANDING_LEVEL_1(2),
57  
58      /**
59       * Reduced with compression factor 2.
60       *
61       * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
62       */
63      EXPANDING_LEVEL_2(3),
64  
65      /**
66       * Reduced with compression factor 3.
67       *
68       * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
69       */
70      EXPANDING_LEVEL_3(4),
71  
72      /**
73       * Reduced with compression factor 4.
74       *
75       * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
76       */
77      EXPANDING_LEVEL_4(5),
78  
79      /**
80       * Imploding.
81       *
82       * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
83       */
84      IMPLODING(6),
85  
86      /**
87       * Tokenization.
88       *
89       * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
90       */
91      TOKENIZATION(7),
92  
93      /**
94       * Compression method 8 for compressed (deflated) entries.
95       *
96       * @see ZipEntry#DEFLATED
97       */
98      DEFLATED(ZipEntry.DEFLATED),
99  
100     /**
101      * Compression Method 9 for enhanced deflate.
102      *
103      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
104      */
105     ENHANCED_DEFLATED(9),
106 
107     /**
108      * PKWARE Data Compression Library Imploding.
109      *
110      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
111      */
112     PKWARE_IMPLODING(10),
113 
114     /**
115      * Compression Method 12 for bzip2.
116      *
117      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
118      */
119     BZIP2(12),
120 
121     /**
122      * Compression Method 14 for LZMA.
123      *
124      * @see <a href="https://www.7-zip.org/sdk.html">https://www.7-zip.org/sdk.html</a>
125      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
126      */
127     LZMA(14),
128 
129     /**
130      * Compression Method 20 for Zstandard (deprecated).
131      *
132      * @see <a href="https://github.com/facebook/zstd">Facebook Zstandard source code</a>
133      * @see <a href="https://pkwaredownloads.blob.core.windows.net/pkware-general/Documentation/APPNOTE-6.3.7.TXT">.ZIP File Format Specification 6.3.7:
134      *      Deprecated zstd compression method id</a>
135      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">.ZIP File Format Specification: Explanation of fields: compression method: (2
136      *      bytes)</a>
137      * @since 1.28.0
138      */
139     ZSTD_DEPRECATED(20),
140 
141     /**
142      * Compression Method 93 for Zstandard.
143      *
144      * @see <a href="https://github.com/facebook/zstd">Facebook Zstandard source code</a>
145      * @see <a href="https://pkwaredownloads.blob.core.windows.net/pkware-general/Documentation/APPNOTE-6.3.8.TXT">.ZIP File Format Specification 6.3.8: Changed
146      *      zstd compression method id</a>
147      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">.ZIP File Format Specification: Explanation of fields: compression method: (2
148      *      bytes)</a>
149      * @since 1.28.0
150      */
151     ZSTD(93),
152 
153     /**
154      * Compression Method 95 for XZ.
155      *
156      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
157      */
158     XZ(95),
159 
160     /**
161      * Compression Method 96 for Jpeg compression.
162      *
163      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
164      */
165     JPEG(96),
166 
167     /**
168      * Compression Method 97 for WavPack.
169      *
170      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
171      */
172     WAVPACK(97),
173 
174     /**
175      * Compression Method 98 for PPMd.
176      *
177      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
178      */
179     PPMD(98),
180 
181     /**
182      * Compression Method 99 for AES encryption.
183      *
184      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
185      */
186     AES_ENCRYPTED(99),
187 
188     /**
189      * Unknown compression method.
190      */
191     UNKNOWN();
192 
193     static final int UNKNOWN_CODE = -1;
194 
195     private static final Map<Integer, ZipMethod> codeToEnum = Collections
196             .unmodifiableMap(Stream.of(values()).collect(Collectors.toMap(ZipMethod::getCode, Function.identity())));
197 
198     /**
199      * Gets the {@link ZipMethod} for the given code or null if the method is not known.
200      *
201      * @param code the code
202      * @return the {@link ZipMethod} for the given code or null if the method is not known.
203      */
204     public static ZipMethod getMethodByCode(final int code) {
205         return codeToEnum.get(code);
206     }
207 
208     /**
209      * Tests whether the given ZIP method is a ZStandard method.
210      *
211      * @param method The method to test.
212      * @return Whether the given ZIP method is a ZStandard method.
213      */
214     static boolean isZstd(final int method) {
215         return method == ZSTD.getCode() || method == ZSTD_DEPRECATED.getCode();
216     }
217 
218     private final int code;
219 
220     ZipMethod() {
221         this(UNKNOWN_CODE);
222     }
223 
224     /**
225      * Constructs a new instance.
226      */
227     ZipMethod(final int code) {
228         this.code = code;
229     }
230 
231     /**
232      * Gets the code of the compression method.
233      *
234      * @see ZipArchiveEntry#getMethod()
235      * @return an integer code for the method
236      */
237     public int getCode() {
238         return code;
239     }
240 }