001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.commons.compress.archivers.zip;
021
022import java.io.Serializable;
023import java.util.zip.ZipException;
024
025/**
026 * Exception thrown when attempting to read or write data for a ZIP entry that uses ZIP features not supported by this library.
027 *
028 * @since 1.1
029 */
030public class UnsupportedZipFeatureException extends ZipException {
031
032    /**
033     * ZIP Features that may or may not be supported.
034     *
035     * @since 1.1
036     */
037    public static class Feature implements Serializable {
038
039        private static final long serialVersionUID = 4112582948775420359L;
040        /**
041         * The entry is encrypted.
042         */
043        public static final Feature ENCRYPTION = new Feature("encryption");
044        /**
045         * The entry used an unsupported compression method.
046         */
047        public static final Feature METHOD = new Feature("compression method");
048        /**
049         * The entry uses a data descriptor.
050         */
051        public static final Feature DATA_DESCRIPTOR = new Feature("data descriptor");
052        /**
053         * The archive uses splitting or spanning.
054         *
055         * @since 1.5
056         */
057        public static final Feature SPLITTING = new Feature("splitting");
058        /**
059         * The archive contains entries with unknown compressed size for a compression method that doesn't support detection of the end of the compressed
060         * stream.
061         *
062         * @since 1.16
063         */
064        public static final Feature UNKNOWN_COMPRESSED_SIZE = new Feature("unknown compressed size");
065
066        private final String name;
067
068        private Feature(final String name) {
069            this.name = name;
070        }
071
072        @Override
073        public String toString() {
074            return name;
075        }
076    }
077
078    private static final long serialVersionUID = 20161219L;
079    private final Feature reason;
080
081    private final transient ZipArchiveEntry entry;
082
083    /**
084     * Creates an exception when the whole archive uses an unsupported feature.
085     *
086     * @param reason the feature that is not supported
087     * @since 1.5
088     */
089    public UnsupportedZipFeatureException(final Feature reason) {
090        super("Unsupported feature " + reason + " used in archive.");
091        this.reason = reason;
092        this.entry = null;
093    }
094
095    /**
096     * Creates an exception.
097     *
098     * @param reason the feature that is not supported
099     * @param entry  the entry using the feature
100     */
101    public UnsupportedZipFeatureException(final Feature reason, final ZipArchiveEntry entry) {
102        super("Unsupported feature " + reason + " used in entry " + entry.getName());
103        this.reason = reason;
104        this.entry = entry;
105    }
106
107    /**
108     * Creates an exception for archives that use an unsupported compression algorithm.
109     *
110     * @param method the method that is not supported
111     * @param entry  the entry using the feature
112     * @since 1.5
113     */
114    public UnsupportedZipFeatureException(final ZipMethod method, final ZipArchiveEntry entry) {
115        super("Unsupported compression method " + entry.getMethod() + " (" + method.name() + ") used in entry " + entry.getName());
116        this.reason = Feature.METHOD;
117        this.entry = entry;
118    }
119
120    /**
121     * The entry using the unsupported feature.
122     *
123     * @return The entry using the unsupported feature.
124     */
125    public ZipArchiveEntry getEntry() {
126        return entry;
127    }
128
129    /**
130     * The unsupported feature that has been used.
131     *
132     * @return The unsupported feature that has been used.
133     */
134    public Feature getFeature() {
135        return reason;
136    }
137}