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