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