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.zip;
019
020import java.io.Serializable;
021import java.util.zip.ZipException;
022
023/**
024 * Exception thrown when attempting to read or write data for a ZIP entry that uses ZIP features not supported by this library.
025 *
026 * @since 1.1
027 */
028public class UnsupportedZipFeatureException extends ZipException {
029
030    /**
031     * ZIP Features that may or may not be supported.
032     *
033     * @since 1.1
034     */
035    public static class Feature implements Serializable {
036
037        private static final long serialVersionUID = 4112582948775420359L;
038        /**
039         * The entry is encrypted.
040         */
041        public static final Feature ENCRYPTION = new Feature("encryption");
042        /**
043         * The entry used an unsupported compression method.
044         */
045        public static final Feature METHOD = new Feature("compression method");
046        /**
047         * The entry uses a data descriptor.
048         */
049        public static final Feature DATA_DESCRIPTOR = new Feature("data descriptor");
050        /**
051         * The archive uses splitting or spanning.
052         *
053         * @since 1.5
054         */
055        public static final Feature SPLITTING = new Feature("splitting");
056        /**
057         * The archive contains entries with unknown compressed size for a compression method that doesn't support detection of the end of the compressed
058         * stream.
059         *
060         * @since 1.16
061         */
062        public static final Feature UNKNOWN_COMPRESSED_SIZE = new Feature("unknown compressed size");
063
064        private final String name;
065
066        private Feature(final String name) {
067            this.name = name;
068        }
069
070        @Override
071        public String toString() {
072            return name;
073        }
074    }
075
076    private static final long serialVersionUID = 20161219L;
077    private final Feature reason;
078
079    private final transient ZipArchiveEntry entry;
080
081    /**
082     * Creates an exception when the whole archive uses an unsupported feature.
083     *
084     * @param reason the feature that is not supported
085     * @since 1.5
086     */
087    public UnsupportedZipFeatureException(final Feature reason) {
088        super("Unsupported feature " + reason + " used in archive.");
089        this.reason = reason;
090        this.entry = null;
091    }
092
093    /**
094     * Creates an exception.
095     *
096     * @param reason the feature that is not supported
097     * @param entry  the entry using the feature
098     */
099    public UnsupportedZipFeatureException(final Feature reason, final ZipArchiveEntry entry) {
100        super("Unsupported feature " + reason + " used in entry " + entry.getName());
101        this.reason = reason;
102        this.entry = entry;
103    }
104
105    /**
106     * Creates an exception for archives that use an unsupported compression algorithm.
107     *
108     * @param method the method that is not supported
109     * @param entry  the entry using the feature
110     * @since 1.5
111     */
112    public UnsupportedZipFeatureException(final ZipMethod method, final ZipArchiveEntry entry) {
113        super("Unsupported compression method " + entry.getMethod() + " (" + method.name() + ") used in entry " + entry.getName());
114        this.reason = Feature.METHOD;
115        this.entry = entry;
116    }
117
118    /**
119     * The entry using the unsupported feature.
120     *
121     * @return The entry using the unsupported feature.
122     */
123    public ZipArchiveEntry getEntry() {
124        return entry;
125    }
126
127    /**
128     * The unsupported feature that has been used.
129     *
130     * @return The unsupported feature that has been used.
131     */
132    public Feature getFeature() {
133        return reason;
134    }
135}