View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.archivers.zip;
21  
22  import java.io.Serializable;
23  import java.util.zip.ZipException;
24  
25  /**
26   * Exception thrown when attempting to read or write data for a ZIP entry that uses ZIP features not supported by this library.
27   *
28   * @since 1.1
29   */
30  public class UnsupportedZipFeatureException extends ZipException {
31  
32      /**
33       * ZIP Features that may or may not be supported.
34       *
35       * @since 1.1
36       */
37      public static class Feature implements Serializable {
38  
39          private static final long serialVersionUID = 4112582948775420359L;
40          /**
41           * The entry is encrypted.
42           */
43          public static final Feature ENCRYPTION = new Feature("encryption");
44          /**
45           * The entry used an unsupported compression method.
46           */
47          public static final Feature METHOD = new Feature("compression method");
48          /**
49           * The entry uses a data descriptor.
50           */
51          public static final Feature DATA_DESCRIPTOR = new Feature("data descriptor");
52          /**
53           * The archive uses splitting or spanning.
54           *
55           * @since 1.5
56           */
57          public static final Feature SPLITTING = new Feature("splitting");
58          /**
59           * The archive contains entries with unknown compressed size for a compression method that doesn't support detection of the end of the compressed
60           * stream.
61           *
62           * @since 1.16
63           */
64          public static final Feature UNKNOWN_COMPRESSED_SIZE = new Feature("unknown compressed size");
65  
66          private final String name;
67  
68          private Feature(final String name) {
69              this.name = name;
70          }
71  
72          @Override
73          public String toString() {
74              return name;
75          }
76      }
77  
78      private static final long serialVersionUID = 20161219L;
79      private final Feature reason;
80  
81      private final transient ZipArchiveEntry entry;
82  
83      /**
84       * Creates an exception when the whole archive uses an unsupported feature.
85       *
86       * @param reason the feature that is not supported
87       * @since 1.5
88       */
89      public UnsupportedZipFeatureException(final Feature reason) {
90          super("Unsupported feature " + reason + " used in archive.");
91          this.reason = reason;
92          this.entry = null;
93      }
94  
95      /**
96       * Creates an exception.
97       *
98       * @param reason the feature that is not supported
99       * @param entry  the entry using the feature
100      */
101     public UnsupportedZipFeatureException(final Feature reason, final ZipArchiveEntry entry) {
102         super("Unsupported feature " + reason + " used in entry " + entry.getName());
103         this.reason = reason;
104         this.entry = entry;
105     }
106 
107     /**
108      * Creates an exception for archives that use an unsupported compression algorithm.
109      *
110      * @param method the method that is not supported
111      * @param entry  the entry using the feature
112      * @since 1.5
113      */
114     public UnsupportedZipFeatureException(final ZipMethod method, final ZipArchiveEntry entry) {
115         super("Unsupported compression method " + entry.getMethod() + " (" + 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      *
123      * @return The entry using the unsupported feature.
124      */
125     public ZipArchiveEntry getEntry() {
126         return entry;
127     }
128 
129     /**
130      * The unsupported feature that has been used.
131      *
132      * @return The unsupported feature that has been used.
133      */
134     public Feature getFeature() {
135         return reason;
136     }
137 }