SevenZMethod.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.sevenz;

  18. import java.util.Arrays;

  19. /**
  20.  * The (partially) supported compression/encryption methods used in 7z archives.
  21.  * <p>
  22.  * All methods with a {@code _FILTER} suffix are used as preprocessors with the goal of creating a better compression ratio with the compressor that comes next
  23.  * in the chain of methods. 7z will in general only allow them to be used together with a "real" compression method but Commons Compress doesn't enforce this.
  24.  * </p>
  25.  * <p>
  26.  * The {@code BCJ_} filters work on executable files for the given platform and convert relative addresses to absolute addresses in CALL instructions. This
  27.  * means they are only useful when applied to executables of the chosen platform.
  28.  * </p>
  29.  */
  30. public enum SevenZMethod {

  31.     /** No compression at all. */
  32.     COPY(new byte[] { (byte) 0x00 }),

  33.     /** LZMA - only supported when reading. */
  34.     LZMA(new byte[] { (byte) 0x03, (byte) 0x01, (byte) 0x01 }),

  35.     /** LZMA2. */
  36.     LZMA2(new byte[] { (byte) 0x21 }),

  37.     /** Deflate. */
  38.     DEFLATE(new byte[] { (byte) 0x04, (byte) 0x01, (byte) 0x08 }),

  39.     /**
  40.      * Deflate64.
  41.      *
  42.      * @since 1.16
  43.      */
  44.     DEFLATE64(new byte[] { (byte) 0x04, (byte) 0x01, (byte) 0x09 }),

  45.     /** BZIP2. */
  46.     BZIP2(new byte[] { (byte) 0x04, (byte) 0x02, (byte) 0x02 }),

  47.     /**
  48.      * AES encryption with a key length of 256 bit using SHA256 for hashes - only supported when reading.
  49.      */
  50.     AES256SHA256(new byte[] { (byte) 0x06, (byte) 0xf1, (byte) 0x07, (byte) 0x01 }),

  51.     /**
  52.      * BCJ x86 platform version 1.
  53.      *
  54.      * @since 1.8
  55.      */
  56.     BCJ_X86_FILTER(new byte[] { 0x03, 0x03, 0x01, 0x03 }),

  57.     /**
  58.      * BCJ PowerPC platform.
  59.      *
  60.      * @since 1.8
  61.      */
  62.     BCJ_PPC_FILTER(new byte[] { 0x03, 0x03, 0x02, 0x05 }),

  63.     /**
  64.      * BCJ I64 platform.
  65.      *
  66.      * @since 1.8
  67.      */
  68.     BCJ_IA64_FILTER(new byte[] { 0x03, 0x03, 0x04, 0x01 }),

  69.     /**
  70.      * BCJ ARM platform.
  71.      *
  72.      * @since 1.8
  73.      */
  74.     BCJ_ARM_FILTER(new byte[] { 0x03, 0x03, 0x05, 0x01 }),

  75.     /**
  76.      * BCJ ARM Thumb platform.
  77.      *
  78.      * @since 1.8
  79.      */
  80.     BCJ_ARM_THUMB_FILTER(new byte[] { 0x03, 0x03, 0x07, 0x01 }),

  81.     /**
  82.      * BCJ Sparc platform.
  83.      *
  84.      * @since 1.8
  85.      */
  86.     BCJ_SPARC_FILTER(new byte[] { 0x03, 0x03, 0x08, 0x05 }),

  87.     /**
  88.      * Delta filter.
  89.      *
  90.      * @since 1.8
  91.      */
  92.     DELTA_FILTER(new byte[] { 0x03 });

  93.     static SevenZMethod byId(final byte[] id) {
  94.         for (final SevenZMethod method : SevenZMethod.class.getEnumConstants()) {
  95.             if (Arrays.equals(method.id, id)) {
  96.                 return method;
  97.             }
  98.         }
  99.         return null;
  100.     }

  101.     private final byte[] id;

  102.     SevenZMethod(final byte[] id) {
  103.         this.id = id;
  104.     }

  105.     byte[] getId() {
  106.         return Arrays.copyOf(id, id.length);
  107.     }
  108. }