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