View Javadoc
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  
18  package org.apache.commons.compress.archivers.zip;
19  
20  import java.io.Serializable;
21  import java.util.zip.ZipException;
22  
23  /**
24   * Exception thrown when attempting to read or write data for a ZIP entry that uses ZIP features not supported by this library.
25   *
26   * @since 1.1
27   */
28  public class UnsupportedZipFeatureException extends ZipException {
29  
30      /**
31       * ZIP Features that may or may not be supported.
32       *
33       * @since 1.1
34       */
35      public static class Feature implements Serializable {
36  
37          private static final long serialVersionUID = 4112582948775420359L;
38          /**
39           * The entry is encrypted.
40           */
41          public static final Feature ENCRYPTION = new Feature("encryption");
42          /**
43           * The entry used an unsupported compression method.
44           */
45          public static final Feature METHOD = new Feature("compression method");
46          /**
47           * The entry uses a data descriptor.
48           */
49          public static final Feature DATA_DESCRIPTOR = new Feature("data descriptor");
50          /**
51           * The archive uses splitting or spanning.
52           *
53           * @since 1.5
54           */
55          public static final Feature SPLITTING = new Feature("splitting");
56          /**
57           * The archive contains entries with unknown compressed size for a compression method that doesn't support detection of the end of the compressed
58           * stream.
59           *
60           * @since 1.16
61           */
62          public static final Feature UNKNOWN_COMPRESSED_SIZE = new Feature("unknown compressed size");
63  
64          private final String name;
65  
66          private Feature(final String name) {
67              this.name = name;
68          }
69  
70          @Override
71          public String toString() {
72              return name;
73          }
74      }
75  
76      private static final long serialVersionUID = 20161219L;
77      private final Feature reason;
78  
79      private final transient ZipArchiveEntry entry;
80  
81      /**
82       * Creates an exception when the whole archive uses an unsupported feature.
83       *
84       * @param reason the feature that is not supported
85       * @since 1.5
86       */
87      public UnsupportedZipFeatureException(final Feature reason) {
88          super("Unsupported feature " + reason + " used in archive.");
89          this.reason = reason;
90          this.entry = null;
91      }
92  
93      /**
94       * Creates an exception.
95       *
96       * @param reason the feature that is not supported
97       * @param entry  the entry using the feature
98       */
99      public UnsupportedZipFeatureException(final Feature reason, final ZipArchiveEntry entry) {
100         super("Unsupported feature " + reason + " used in entry " + entry.getName());
101         this.reason = reason;
102         this.entry = entry;
103     }
104 
105     /**
106      * Creates an exception for archives that use an unsupported compression algorithm.
107      *
108      * @param method the method that is not supported
109      * @param entry  the entry using the feature
110      * @since 1.5
111      */
112     public UnsupportedZipFeatureException(final ZipMethod method, final ZipArchiveEntry entry) {
113         super("Unsupported compression method " + entry.getMethod() + " (" + method.name() + ") used in entry " + entry.getName());
114         this.reason = Feature.METHOD;
115         this.entry = entry;
116     }
117 
118     /**
119      * The entry using the unsupported feature.
120      *
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      *
130      * @return The unsupported feature that has been used.
131      */
132     public Feature getFeature() {
133         return reason;
134     }
135 }