SevenZFileOptions.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. /**
  19.  * Collects options for reading 7z archives.
  20.  *
  21.  * @since 1.19
  22.  * @Immutable
  23.  * @deprecated Use {@link SevenZFile.Builder}.
  24.  */
  25. @Deprecated
  26. public class SevenZFileOptions {

  27.     /**
  28.      * Mutable builder for the immutable {@link SevenZFileOptions}.
  29.      *
  30.      * @since 1.19
  31.      */
  32.     public static class Builder {

  33.         private int maxMemoryLimitKb = SevenZFile.Builder.MEMORY_LIMIT_IN_KB;
  34.         private boolean useDefaultNameForUnnamedEntries = SevenZFile.Builder.USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES;
  35.         private boolean tryToRecoverBrokenArchives = SevenZFile.Builder.TRY_TO_RECOVER_BROKEN_ARCHIVES;

  36.         /**
  37.          * Builds the {@link SevenZFileOptions}.
  38.          *
  39.          * @return configured {@link SevenZFileOptions}.
  40.          */
  41.         public SevenZFileOptions build() {
  42.             return new SevenZFileOptions(maxMemoryLimitKb, useDefaultNameForUnnamedEntries, tryToRecoverBrokenArchives);
  43.         }

  44.         /**
  45.          * Sets the maximum amount of memory to use for parsing the archive and during extraction.
  46.          * <p>
  47.          * Not all codecs will honor this setting. Currently only LZMA and LZMA2 are supported.
  48.          * </p>
  49.          *
  50.          * @param maxMemoryLimitKb limit of the maximum amount of memory to use
  51.          * @return the reconfigured builder
  52.          */
  53.         public Builder withMaxMemoryLimitInKb(final int maxMemoryLimitKb) {
  54.             this.maxMemoryLimitKb = maxMemoryLimitKb;
  55.             return this;
  56.         }

  57.         /**
  58.          * Sets whether {@link SevenZFile} will try to recover broken archives where the CRC of the file's metadata is 0.
  59.          * <p>
  60.          * This special kind of broken archive is encountered when mutli volume archives are closed prematurely. If you enable this option SevenZFile will trust
  61.          * data that looks as if it could contain metadata of an archive and allocate big amounts of memory. It is strongly recommended to not enable this
  62.          * option without setting {@link #withMaxMemoryLimitInKb} at the same time.
  63.          * </p>
  64.          *
  65.          * @param tryToRecoverBrokenArchives if true SevenZFile will try to recover archives that are broken in the specific way
  66.          * @return the reconfigured builder
  67.          * @since 1.21
  68.          */
  69.         public Builder withTryToRecoverBrokenArchives(final boolean tryToRecoverBrokenArchives) {
  70.             this.tryToRecoverBrokenArchives = tryToRecoverBrokenArchives;
  71.             return this;
  72.         }

  73.         /**
  74.          * Sets whether entries without a name should get their names set to the archive's default file name.
  75.          *
  76.          * @param useDefaultNameForUnnamedEntries if true the name of unnamed entries will be set to the archive's default name
  77.          * @return the reconfigured builder
  78.          */
  79.         public Builder withUseDefaultNameForUnnamedEntries(final boolean useDefaultNameForUnnamedEntries) {
  80.             this.useDefaultNameForUnnamedEntries = useDefaultNameForUnnamedEntries;
  81.             return this;
  82.         }
  83.     }

  84.     /**
  85.      * The default options.
  86.      * <ul>
  87.      * <li>no memory limit</li>
  88.      * <li>don't modify the name of unnamed entries</li>
  89.      * </ul>
  90.      */
  91.     public static final SevenZFileOptions DEFAULT = new SevenZFileOptions(SevenZFile.Builder.MEMORY_LIMIT_IN_KB,
  92.             SevenZFile.Builder.USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES, SevenZFile.Builder.TRY_TO_RECOVER_BROKEN_ARCHIVES);

  93.     /**
  94.      * Obtains a builder for SevenZFileOptions.
  95.      *
  96.      * @return a builder for SevenZFileOptions.
  97.      */
  98.     public static Builder builder() {
  99.         return new Builder();
  100.     }

  101.     private final int maxMemoryLimitKb;
  102.     private final boolean useDefaultNameForUnnamedEntries;
  103.     private final boolean tryToRecoverBrokenArchives;

  104.     private SevenZFileOptions(final int maxMemoryLimitKb, final boolean useDefaultNameForUnnamedEntries, final boolean tryToRecoverBrokenArchives) {
  105.         this.maxMemoryLimitKb = maxMemoryLimitKb;
  106.         this.useDefaultNameForUnnamedEntries = useDefaultNameForUnnamedEntries;
  107.         this.tryToRecoverBrokenArchives = tryToRecoverBrokenArchives;
  108.     }

  109.     /**
  110.      * Gets the maximum amount of memory to use for parsing the archive and during extraction.
  111.      * <p>
  112.      * Not all codecs will honor this setting. Currently only LZMA and LZMA2 are supported.
  113.      * </p>
  114.      *
  115.      * @return the maximum amount of memory to use for extraction
  116.      */
  117.     public int getMaxMemoryLimitInKb() {
  118.         return maxMemoryLimitKb;
  119.     }

  120.     /**
  121.      * Whether {@link SevenZFile} shall try to recover from a certain type of broken archive.
  122.      *
  123.      * @return whether SevenZFile shall try to recover from a certain type of broken archive.
  124.      * @since 1.21
  125.      */
  126.     public boolean getTryToRecoverBrokenArchives() {
  127.         return tryToRecoverBrokenArchives;
  128.     }

  129.     /**
  130.      * Gets whether entries without a name should get their names set to the archive's default file name.
  131.      *
  132.      * @return whether entries without a name should get their names set to the archive's default file name
  133.      */
  134.     public boolean getUseDefaultNameForUnnamedEntries() {
  135.         return useDefaultNameForUnnamedEntries;
  136.     }
  137. }