001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.commons.compress.archivers.zip;
018
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022import java.util.zip.ZipEntry;
023
024/**
025 * List of known compression methods
026 *
027 * Many of these methods are currently not supported by commons compress
028 *
029 * @since 1.5
030 */
031public enum ZipMethod {
032
033    /**
034     * Compression method 0 for uncompressed entries.
035     *
036     * @see ZipEntry#STORED
037     */
038    STORED(ZipEntry.STORED),
039
040    /**
041     * UnShrinking.
042     * dynamic Lempel-Ziv-Welch-Algorithm
043     *
044     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
045     *      method: (2 bytes)</a>
046     */
047    UNSHRINKING(1),
048
049    /**
050     * Reduced with compression factor 1.
051     *
052     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
053     *      method: (2 bytes)</a>
054     */
055    EXPANDING_LEVEL_1(2),
056
057    /**
058     * Reduced with compression factor 2.
059     *
060     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
061     *      method: (2 bytes)</a>
062     */
063    EXPANDING_LEVEL_2(3),
064
065    /**
066     * Reduced with compression factor 3.
067     *
068     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
069     *      method: (2 bytes)</a>
070     */
071    EXPANDING_LEVEL_3(4),
072
073    /**
074     * Reduced with compression factor 4.
075     *
076     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
077     *      method: (2 bytes)</a>
078     */
079    EXPANDING_LEVEL_4(5),
080
081    /**
082     * Imploding.
083     *
084     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
085     *      method: (2 bytes)</a>
086     */
087    IMPLODING(6),
088
089    /**
090     * Tokenization.
091     *
092     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
093     *      method: (2 bytes)</a>
094     */
095    TOKENIZATION(7),
096
097    /**
098     * Compression method 8 for compressed (deflated) entries.
099     *
100     * @see ZipEntry#DEFLATED
101     */
102    DEFLATED(ZipEntry.DEFLATED),
103
104    /**
105     * Compression Method 9 for enhanced deflate.
106     *
107     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
108     */
109    ENHANCED_DEFLATED(9),
110
111    /**
112     * PKWARE Data Compression Library Imploding.
113     *
114     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
115     */
116    PKWARE_IMPLODING(10),
117
118    /**
119     * Compression Method 12 for bzip2.
120     *
121     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
122     */
123    BZIP2(12),
124
125    /**
126     * Compression Method 14 for LZMA.
127     *
128     * @see <a href="https://www.7-zip.org/sdk.html">https://www.7-zip.org/sdk.html</a>
129     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
130     */
131    LZMA(14),
132
133
134    /**
135     * Compression Method 95 for XZ.
136     *
137     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
138     */
139    XZ(95),
140
141    /**
142     * Compression Method 96 for Jpeg compression.
143     *
144     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
145     */
146    JPEG(96),
147
148    /**
149     * Compression Method 97 for WavPack.
150     *
151     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
152     */
153    WAVPACK(97),
154
155    /**
156     * Compression Method 98 for PPMd.
157     *
158     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
159     */
160    PPMD(98),
161
162
163    /**
164     * Compression Method 99 for AES encryption.
165     *
166     * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
167     */
168    AES_ENCRYPTED(99),
169
170    /**
171     * Unknown compression method.
172     */
173    UNKNOWN();
174
175    static final int UNKNOWN_CODE = -1;
176
177    private static final Map<Integer, ZipMethod> codeToEnum;
178
179    static {
180        final Map<Integer, ZipMethod> cte = new HashMap<>();
181        for (final ZipMethod method : values()) {
182            cte.put(method.getCode(), method);
183        }
184        codeToEnum = Collections.unmodifiableMap(cte);
185    }
186
187    /**
188     * returns the {@link ZipMethod} for the given code or null if the
189     * method is not known.
190     * @param code the code
191     * @return the {@link ZipMethod} for the given code or null if the
192     * method is not known.
193     */
194    public static ZipMethod getMethodByCode(final int code) {
195        return codeToEnum.get(code);
196    }
197
198    private final int code;
199
200    ZipMethod() {
201        this(UNKNOWN_CODE);
202    }
203
204    /**
205     * private constructor for enum style class.
206     */
207    ZipMethod(final int code) {
208        this.code = code;
209    }
210
211
212    /**
213     * the code of the compression method.
214     *
215     * @see ZipArchiveEntry#getMethod()
216     *
217     * @return an integer code for the method
218     */
219    public int getCode() {
220        return code;
221    }
222}