UnsupportedZipFeatureException.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */

  17. package org.apache.commons.compress.archivers.zip;

  18. import java.io.Serializable;
  19. import java.util.zip.ZipException;

  20. /**
  21.  * Exception thrown when attempting to read or write data for a ZIP entry that uses ZIP features not supported by this library.
  22.  *
  23.  * @since 1.1
  24.  */
  25. public class UnsupportedZipFeatureException extends ZipException {

  26.     /**
  27.      * ZIP Features that may or may not be supported.
  28.      *
  29.      * @since 1.1
  30.      */
  31.     public static class Feature implements Serializable {

  32.         private static final long serialVersionUID = 4112582948775420359L;
  33.         /**
  34.          * The entry is encrypted.
  35.          */
  36.         public static final Feature ENCRYPTION = new Feature("encryption");
  37.         /**
  38.          * The entry used an unsupported compression method.
  39.          */
  40.         public static final Feature METHOD = new Feature("compression method");
  41.         /**
  42.          * The entry uses a data descriptor.
  43.          */
  44.         public static final Feature DATA_DESCRIPTOR = new Feature("data descriptor");
  45.         /**
  46.          * The archive uses splitting or spanning.
  47.          *
  48.          * @since 1.5
  49.          */
  50.         public static final Feature SPLITTING = new Feature("splitting");
  51.         /**
  52.          * The archive contains entries with unknown compressed size for a compression method that doesn't support detection of the end of the compressed
  53.          * stream.
  54.          *
  55.          * @since 1.16
  56.          */
  57.         public static final Feature UNKNOWN_COMPRESSED_SIZE = new Feature("unknown compressed size");

  58.         private final String name;

  59.         private Feature(final String name) {
  60.             this.name = name;
  61.         }

  62.         @Override
  63.         public String toString() {
  64.             return name;
  65.         }
  66.     }

  67.     private static final long serialVersionUID = 20161219L;
  68.     private final Feature reason;

  69.     private final transient ZipArchiveEntry entry;

  70.     /**
  71.      * Creates an exception when the whole archive uses an unsupported feature.
  72.      *
  73.      * @param reason the feature that is not supported
  74.      * @since 1.5
  75.      */
  76.     public UnsupportedZipFeatureException(final Feature reason) {
  77.         super("Unsupported feature " + reason + " used in archive.");
  78.         this.reason = reason;
  79.         this.entry = null;
  80.     }

  81.     /**
  82.      * Creates an exception.
  83.      *
  84.      * @param reason the feature that is not supported
  85.      * @param entry  the entry using the feature
  86.      */
  87.     public UnsupportedZipFeatureException(final Feature reason, final ZipArchiveEntry entry) {
  88.         super("Unsupported feature " + reason + " used in entry " + entry.getName());
  89.         this.reason = reason;
  90.         this.entry = entry;
  91.     }

  92.     /**
  93.      * Creates an exception for archives that use an unsupported compression algorithm.
  94.      *
  95.      * @param method the method that is not supported
  96.      * @param entry  the entry using the feature
  97.      * @since 1.5
  98.      */
  99.     public UnsupportedZipFeatureException(final ZipMethod method, final ZipArchiveEntry entry) {
  100.         super("Unsupported compression method " + entry.getMethod() + " (" + method.name() + ") used in entry " + entry.getName());
  101.         this.reason = Feature.METHOD;
  102.         this.entry = entry;
  103.     }

  104.     /**
  105.      * The entry using the unsupported feature.
  106.      *
  107.      * @return The entry using the unsupported feature.
  108.      */
  109.     public ZipArchiveEntry getEntry() {
  110.         return entry;
  111.     }

  112.     /**
  113.      * The unsupported feature that has been used.
  114.      *
  115.      * @return The unsupported feature that has been used.
  116.      */
  117.     public Feature getFeature() {
  118.         return reason;
  119.     }
  120. }