1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * https://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.commons.compress.archivers.sevenz; 20 21 /** 22 * Collects options for reading 7z archives. 23 * 24 * @since 1.19 25 * @Immutable 26 * @deprecated Use {@link SevenZFile.Builder}. 27 */ 28 @Deprecated 29 public class SevenZFileOptions { 30 31 /** 32 * Mutable builder for the immutable {@link SevenZFileOptions}. 33 * 34 * @since 1.19 35 */ 36 public static class Builder { 37 38 private int maxMemoryLimitKb = SevenZFile.Builder.MEMORY_LIMIT_IN_KB; 39 private boolean useDefaultNameForUnnamedEntries = SevenZFile.Builder.USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES; 40 private boolean tryToRecoverBrokenArchives = SevenZFile.Builder.TRY_TO_RECOVER_BROKEN_ARCHIVES; 41 42 /** 43 * Builds the {@link SevenZFileOptions}. 44 * 45 * @return configured {@link SevenZFileOptions}. 46 */ 47 public SevenZFileOptions build() { 48 return new SevenZFileOptions(maxMemoryLimitKb, useDefaultNameForUnnamedEntries, tryToRecoverBrokenArchives); 49 } 50 51 /** 52 * Sets the maximum amount of memory to use for parsing the archive and during extraction. 53 * <p> 54 * Not all codecs will honor this setting. Currently only LZMA and LZMA2 are supported. 55 * </p> 56 * 57 * @param maxMemoryLimitKiB limit of the maximum amount of memory to use in kibibytes. 58 * @return the reconfigured builder 59 */ 60 public Builder withMaxMemoryLimitInKb(final int maxMemoryLimitKiB) { 61 this.maxMemoryLimitKb = maxMemoryLimitKiB; 62 return this; 63 } 64 65 /** 66 * Sets whether {@link SevenZFile} will try to recover broken archives where the CRC of the file's metadata is 0. 67 * <p> 68 * This special kind of broken archive is encountered when mutli volume archives are closed prematurely. If you enable this option SevenZFile will trust 69 * 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 70 * option without setting {@link #withMaxMemoryLimitInKb} at the same time. 71 * </p> 72 * 73 * @param tryToRecoverBrokenArchives if true SevenZFile will try to recover archives that are broken in the specific way 74 * @return the reconfigured builder 75 * @since 1.21 76 */ 77 public Builder withTryToRecoverBrokenArchives(final boolean tryToRecoverBrokenArchives) { 78 this.tryToRecoverBrokenArchives = tryToRecoverBrokenArchives; 79 return this; 80 } 81 82 /** 83 * Sets whether entries without a name should get their names set to the archive's default file name. 84 * 85 * @param useDefaultNameForUnnamedEntries if true the name of unnamed entries will be set to the archive's default name 86 * @return the reconfigured builder 87 */ 88 public Builder withUseDefaultNameForUnnamedEntries(final boolean useDefaultNameForUnnamedEntries) { 89 this.useDefaultNameForUnnamedEntries = useDefaultNameForUnnamedEntries; 90 return this; 91 } 92 } 93 94 /** 95 * The default options. 96 * <ul> 97 * <li>no memory limit</li> 98 * <li>don't modify the name of unnamed entries</li> 99 * </ul> 100 */ 101 public static final SevenZFileOptions DEFAULT = new SevenZFileOptions(SevenZFile.Builder.MEMORY_LIMIT_IN_KB, 102 SevenZFile.Builder.USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES, SevenZFile.Builder.TRY_TO_RECOVER_BROKEN_ARCHIVES); 103 104 /** 105 * Obtains a builder for SevenZFileOptions. 106 * 107 * @return a builder for SevenZFileOptions. 108 */ 109 public static Builder builder() { 110 return new Builder(); 111 } 112 113 private final int maxMemoryLimitKiB; 114 private final boolean useDefaultNameForUnnamedEntries; 115 private final boolean tryToRecoverBrokenArchives; 116 117 private SevenZFileOptions(final int maxMemoryLimitKb, final boolean useDefaultNameForUnnamedEntries, final boolean tryToRecoverBrokenArchives) { 118 this.maxMemoryLimitKiB = maxMemoryLimitKb; 119 this.useDefaultNameForUnnamedEntries = useDefaultNameForUnnamedEntries; 120 this.tryToRecoverBrokenArchives = tryToRecoverBrokenArchives; 121 } 122 123 /** 124 * Gets the maximum amount of memory to use for parsing the archive and during extraction. 125 * <p> 126 * Not all codecs will honor this setting. Currently only LZMA and LZMA2 are supported. 127 * </p> 128 * 129 * @return the maximum amount of memory to use for extraction in kibibytes. 130 */ 131 public int getMaxMemoryLimitInKb() { 132 return maxMemoryLimitKiB; 133 } 134 135 /** 136 * Gets whether {@link SevenZFile} shall try to recover from a certain type of broken archive. 137 * 138 * @return whether SevenZFile shall try to recover from a certain type of broken archive. 139 * @since 1.21 140 */ 141 public boolean getTryToRecoverBrokenArchives() { 142 return tryToRecoverBrokenArchives; 143 } 144 145 /** 146 * Gets whether entries without a name should get their names set to the archive's default file name. 147 * 148 * @return whether entries without a name should get their names set to the archive's default file name 149 */ 150 public boolean getUseDefaultNameForUnnamedEntries() { 151 return useDefaultNameForUnnamedEntries; 152 } 153 }