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