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 */
017package org.apache.commons.compress.archivers.sevenz;
018
019/**
020 * Collects options for reading 7z archives.
021 *
022 * @since 1.19
023 * @Immutable
024 * @deprecated Use {@link SevenZFile.Builder}.
025 */
026@Deprecated
027public class SevenZFileOptions {
028
029    /**
030     * Mutable builder for the immutable {@link SevenZFileOptions}.
031     *
032     * @since 1.19
033     */
034    public static class Builder {
035
036        private int maxMemoryLimitKb = SevenZFile.Builder.MEMORY_LIMIT_IN_KB;
037        private boolean useDefaultNameForUnnamedEntries = SevenZFile.Builder.USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES;
038        private boolean tryToRecoverBrokenArchives = SevenZFile.Builder.TRY_TO_RECOVER_BROKEN_ARCHIVES;
039
040        /**
041         * Builds the {@link SevenZFileOptions}.
042         *
043         * @return configured {@link SevenZFileOptions}.
044         */
045        public SevenZFileOptions build() {
046            return new SevenZFileOptions(maxMemoryLimitKb, useDefaultNameForUnnamedEntries, tryToRecoverBrokenArchives);
047        }
048
049        /**
050         * Sets the maximum amount of memory to use for parsing the archive and during extraction.
051         * <p>
052         * Not all codecs will honor this setting. Currently only LZMA and LZMA2 are supported.
053         * </p>
054         *
055         * @param maxMemoryLimitKb limit of the maximum amount of memory to use
056         * @return the reconfigured builder
057         */
058        public Builder withMaxMemoryLimitInKb(final int maxMemoryLimitKb) {
059            this.maxMemoryLimitKb = maxMemoryLimitKb;
060            return this;
061        }
062
063        /**
064         * Sets whether {@link SevenZFile} will try to recover broken archives where the CRC of the file's metadata is 0.
065         * <p>
066         * This special kind of broken archive is encountered when mutli volume archives are closed prematurely. If you enable this option SevenZFile will trust
067         * 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
068         * option without setting {@link #withMaxMemoryLimitInKb} at the same time.
069         * </p>
070         *
071         * @param tryToRecoverBrokenArchives if true SevenZFile will try to recover archives that are broken in the specific way
072         * @return the reconfigured builder
073         * @since 1.21
074         */
075        public Builder withTryToRecoverBrokenArchives(final boolean tryToRecoverBrokenArchives) {
076            this.tryToRecoverBrokenArchives = tryToRecoverBrokenArchives;
077            return this;
078        }
079
080        /**
081         * Sets whether entries without a name should get their names set to the archive's default file name.
082         *
083         * @param useDefaultNameForUnnamedEntries if true the name of unnamed entries will be set to the archive's default name
084         * @return the reconfigured builder
085         */
086        public Builder withUseDefaultNameForUnnamedEntries(final boolean useDefaultNameForUnnamedEntries) {
087            this.useDefaultNameForUnnamedEntries = useDefaultNameForUnnamedEntries;
088            return this;
089        }
090    }
091
092    /**
093     * The default options.
094     * <ul>
095     * <li>no memory limit</li>
096     * <li>don't modify the name of unnamed entries</li>
097     * </ul>
098     */
099    public static final SevenZFileOptions DEFAULT = new SevenZFileOptions(SevenZFile.Builder.MEMORY_LIMIT_IN_KB,
100            SevenZFile.Builder.USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES, SevenZFile.Builder.TRY_TO_RECOVER_BROKEN_ARCHIVES);
101
102    /**
103     * Obtains a builder for SevenZFileOptions.
104     *
105     * @return a builder for SevenZFileOptions.
106     */
107    public static Builder builder() {
108        return new Builder();
109    }
110
111    private final int maxMemoryLimitKb;
112    private final boolean useDefaultNameForUnnamedEntries;
113    private final boolean tryToRecoverBrokenArchives;
114
115    private SevenZFileOptions(final int maxMemoryLimitKb, final boolean useDefaultNameForUnnamedEntries, final boolean tryToRecoverBrokenArchives) {
116        this.maxMemoryLimitKb = maxMemoryLimitKb;
117        this.useDefaultNameForUnnamedEntries = useDefaultNameForUnnamedEntries;
118        this.tryToRecoverBrokenArchives = tryToRecoverBrokenArchives;
119    }
120
121    /**
122     * Gets the maximum amount of memory to use for parsing the archive and during extraction.
123     * <p>
124     * Not all codecs will honor this setting. Currently only LZMA and LZMA2 are supported.
125     * </p>
126     *
127     * @return the maximum amount of memory to use for extraction
128     */
129    public int getMaxMemoryLimitInKb() {
130        return maxMemoryLimitKb;
131    }
132
133    /**
134     * Whether {@link SevenZFile} shall try to recover from a certain type of broken archive.
135     *
136     * @return whether SevenZFile shall try to recover from a certain type of broken archive.
137     * @since 1.21
138     */
139    public boolean getTryToRecoverBrokenArchives() {
140        return tryToRecoverBrokenArchives;
141    }
142
143    /**
144     * Gets whether entries without a name should get their names set to the archive's default file name.
145     *
146     * @return whether entries without a name should get their names set to the archive's default file name
147     */
148    public boolean getUseDefaultNameForUnnamedEntries() {
149        return useDefaultNameForUnnamedEntries;
150    }
151}