    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 *
    017 */
    018package org.apache.commons.compress.archivers.zip;
    019
    020import java.util.Collections;
    021import java.util.HashMap;
    022import java.util.Map;
    023import java.util.zip.ZipEntry;
    024
    025/**
    026 * List of known compression methods
    027 *
    028 * Many of these methods are currently not supported by commons compress
    029 *
    030 * @since 1.5
    031 */
    032public enum ZipMethod {
    033
    034    /**
    035     * Compression method 0 for uncompressed entries.
    036     *
    037     * @see ZipEntry#STORED
    038     */
    039    STORED(ZipEntry.STORED),
    040
    041    /**
    042     * UnShrinking.
    043     * dynamic Lempel-Ziv-Welch-Algorithm
    044     *
    045     * @see <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
    046     *      method: (2 bytes)</a>
    047     */
    048    UNSHRINKING(1),
    049
    050    /**
    051     * Reduced with compression factor 1.
    052     *
    053     * @see <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
    054     *      method: (2 bytes)</a>
    055     */
    056    EXPANDING_LEVEL_1(2),
    057
    058    /**
    059     * Reduced with compression factor 2.
    060     *
    061     * @see <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
    062     *      method: (2 bytes)</a>
    063     */
    064    EXPANDING_LEVEL_2(3),
    065
    066    /**
    067     * Reduced with compression factor 3.
    068     *
    069     * @see <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
    070     *      method: (2 bytes)</a>
    071     */
    072    EXPANDING_LEVEL_3(4),
    073
    074    /**
    075     * Reduced with compression factor 4.
    076     *
    077     * @see <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
    078     *      method: (2 bytes)</a>
    079     */
    080    EXPANDING_LEVEL_4(5),
    081
    082    /**
    083     * Imploding.
    084     *
    085     * @see <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
    086     *      method: (2 bytes)</a>
    087     */
    088    IMPLODING(6),
    089
    090    /**
    091     * Tokenization.
    092     *
    093     * @see <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression
    094     *      method: (2 bytes)</a>
    095     */
    096    TOKENIZATION(7),
    097
    098    /**
    099     * Compression method 8 for compressed (deflated) entries.
    100     *
    101     * @see ZipEntry#DEFLATED
    102     */
    103    DEFLATED(ZipEntry.DEFLATED),
    104
    105    /**
    106     * Compression Method 9 for enhanced deflate.
    107     *
    108     * @see <a href="http://www.winzip.com/wz54.htm">http://www.winzip.com/wz54.htm</a>
    109     */
    110    ENHANCED_DEFLATED(9),
    111
    112    /**
    113     * PKWARE Data Compression Library Imploding.
    114     *
    115     * @see <a href="http://www.winzip.com/wz54.htm">http://www.winzip.com/wz54.htm</a>
    116     */
    117    PKWARE_IMPLODING(10),
    118
    119    /**
    120     * Compression Method 12 for bzip2.
    121     *
    122     * @see <a href="http://www.winzip.com/wz54.htm">http://www.winzip.com/wz54.htm</a>
    123     */
    124    BZIP2(12),
    125
    126    /**
    127     * Compression Method 14 for LZMA.
    128     *
    129     * @see <a href="http://www.7-zip.org/sdk.html">http://www.7-zip.org/sdk.html</a>
    130     * @see <a href="http://www.winzip.com/wz54.htm">http://www.winzip.com/wz54.htm</a>
    131     */
    132    LZMA(14),
    133
    134
    135    /**
    136     * Compression Method 96 for Jpeg compression.
    137     *
    138     * @see <a href="http://www.winzip.com/wz54.htm">http://www.winzip.com/wz54.htm</a>
    139     */
    140    JPEG(96),
    141
    142    /**
    143     * Compression Method 97 for WavPack.
    144     *
    145     * @see <a href="http://www.winzip.com/wz54.htm">http://www.winzip.com/wz54.htm</a>
    146     */
    147    WAVPACK(97),
    148
    149    /**
    150     * Compression Method 98 for PPMd.
    151     *
    152     * @see <a href="http://www.winzip.com/wz54.htm">http://www.winzip.com/wz54.htm</a>
    153     */
    154    PPMD(98),
    155
    156
    157    /**
    158     * Compression Method 99 for AES encryption.
    159     *
    160     * @see <a href="http://www.winzip.com/wz54.htm">http://www.winzip.com/wz54.htm</a>
    161     */
    162    AES_ENCRYPTED(99),
    163
    164    /**
    165     * Unknown compression method.
    166     */
    167    UNKNOWN();
    168
    169    static final int UNKNOWN_CODE = -1;
    170
    171    private final int code;
    172
    173    private static final Map<Integer, ZipMethod> codeToEnum;
    174
    175    static {
    176        final Map<Integer, ZipMethod> cte = new HashMap<>();
    177        for (final ZipMethod method : values()) {
    178            cte.put(method.getCode(), method);
    179        }
    180        codeToEnum = Collections.unmodifiableMap(cte);
    181    }
    182
    183    private ZipMethod() {
    184        this(UNKNOWN_CODE);
    185    }
    186
    187    /**
    188     * private constructor for enum style class.
    189     */
    190    ZipMethod(final int code) {
    191        this.code = code;
    192    }
    193
    194    /**
    195     * the code of the compression method.
    196     *
    197     * @see ZipArchiveEntry#getMethod()
    198     *
    199     * @return an integer code for the method
    200     */
    201    public int getCode() {
    202        return code;
    203    }
    204
    205
    206    /**
    207     * returns the {@link ZipMethod} for the given code or null if the
    208     * method is not known.
    209     * @param code the code
    210     * @return the {@link ZipMethod} for the given code or null if the
    211     * method is not known.
    212     */
    213    public static ZipMethod getMethodByCode(final int code) {
    214        return codeToEnum.get(code);
    215    }
    216}