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