ZipMethod.java

  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. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.zip.ZipEntry;

  22. /**
  23.  * List of known compression methods
  24.  *
  25.  * Many of these methods are currently not supported by commons compress
  26.  *
  27.  * @since 1.5
  28.  */
  29. public enum ZipMethod {

  30.     /**
  31.      * Compression method 0 for uncompressed entries.
  32.      *
  33.      * @see ZipEntry#STORED
  34.      */
  35.     STORED(ZipEntry.STORED),

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

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

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

  54.     /**
  55.      * Reduced with compression factor 3.
  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_3(4),

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

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

  72.     /**
  73.      * Tokenization.
  74.      *
  75.      * @see <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">Explanation of fields: compression method: (2 bytes)</a>
  76.      */
  77.     TOKENIZATION(7),

  78.     /**
  79.      * Compression method 8 for compressed (deflated) entries.
  80.      *
  81.      * @see ZipEntry#DEFLATED
  82.      */
  83.     DEFLATED(ZipEntry.DEFLATED),

  84.     /**
  85.      * Compression Method 9 for enhanced deflate.
  86.      *
  87.      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
  88.      */
  89.     ENHANCED_DEFLATED(9),

  90.     /**
  91.      * PKWARE Data Compression Library Imploding.
  92.      *
  93.      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
  94.      */
  95.     PKWARE_IMPLODING(10),

  96.     /**
  97.      * Compression Method 12 for bzip2.
  98.      *
  99.      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
  100.      */
  101.     BZIP2(12),

  102.     /**
  103.      * Compression Method 14 for LZMA.
  104.      *
  105.      * @see <a href="https://www.7-zip.org/sdk.html">https://www.7-zip.org/sdk.html</a>
  106.      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
  107.      */
  108.     LZMA(14),

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

  115.     /**
  116.      * Compression Method 96 for Jpeg compression.
  117.      *
  118.      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
  119.      */
  120.     JPEG(96),

  121.     /**
  122.      * Compression Method 97 for WavPack.
  123.      *
  124.      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
  125.      */
  126.     WAVPACK(97),

  127.     /**
  128.      * Compression Method 98 for PPMd.
  129.      *
  130.      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
  131.      */
  132.     PPMD(98),

  133.     /**
  134.      * Compression Method 99 for AES encryption.
  135.      *
  136.      * @see <a href="https://www.winzip.com/wz54.htm">https://www.winzip.com/wz54.htm</a>
  137.      */
  138.     AES_ENCRYPTED(99),

  139.     /**
  140.      * Unknown compression method.
  141.      */
  142.     UNKNOWN();

  143.     static final int UNKNOWN_CODE = -1;

  144.     private static final Map<Integer, ZipMethod> codeToEnum;

  145.     static {
  146.         final Map<Integer, ZipMethod> cte = new HashMap<>();
  147.         for (final ZipMethod method : values()) {
  148.             cte.put(method.getCode(), method);
  149.         }
  150.         codeToEnum = Collections.unmodifiableMap(cte);
  151.     }

  152.     /**
  153.      * returns the {@link ZipMethod} for the given code or null if the method is not known.
  154.      *
  155.      * @param code the code
  156.      * @return the {@link ZipMethod} for the given code or null if the method is not known.
  157.      */
  158.     public static ZipMethod getMethodByCode(final int code) {
  159.         return codeToEnum.get(code);
  160.     }

  161.     private final int code;

  162.     ZipMethod() {
  163.         this(UNKNOWN_CODE);
  164.     }

  165.     /**
  166.      * private constructor for enum style class.
  167.      */
  168.     ZipMethod(final int code) {
  169.         this.code = code;
  170.     }

  171.     /**
  172.      * the code of the compression method.
  173.      *
  174.      * @see ZipArchiveEntry#getMethod()
  175.      *
  176.      * @return an integer code for the method
  177.      */
  178.     public int getCode() {
  179.         return code;
  180.     }
  181. }