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. dynamic Lempel-Ziv-Welch-Algorithm
042     *
043     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
044     */
045    UNSHRINKING(1),
046
047    /**
048     * Reduced with compression factor 1.
049     *
050     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
051     */
052    EXPANDING_LEVEL_1(2),
053
054    /**
055     * Reduced with compression factor 2.
056     *
057     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
058     */
059    EXPANDING_LEVEL_2(3),
060
061    /**
062     * Reduced with compression factor 3.
063     *
064     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
065     */
066    EXPANDING_LEVEL_3(4),
067
068    /**
069     * Reduced with compression factor 4.
070     *
071     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
072     */
073    EXPANDING_LEVEL_4(5),
074
075    /**
076     * Imploding.
077     *
078     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
079     */
080    IMPLODING(6),
081
082    /**
083     * Tokenization.
084     *
085     * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
086     */
087    TOKENIZATION(7),
088
089    /**
090     * Compression method 8 for compressed (deflated) entries.
091     *
092     * @see ZipEntry#DEFLATED
093     */
094    DEFLATED(ZipEntry.DEFLATED),
095
096    /**
097     * Compression Method 9 for enhanced deflate.
098     *
099     * @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}