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