You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2012/07/08 20:55:06 UTC

svn commit: r1358814 - in /commons/proper/compress/trunk/src: changes/ main/java/org/apache/commons/compress/archivers/zip/

Author: bodewig
Date: Sun Jul  8 18:55:06 2012
New Revision: 1358814

URL: http://svn.apache.org/viewvc?rev=1358814&view=rev
Log:
COMPRESS-188 improve exception method for unsupported compression methods in zips.  Submitted by Harald Kuhn

Added:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipMethod.java   (with props)
Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java

Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1358814&r1=1358813&r2=1358814&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Sun Jul  8 18:55:06 2012
@@ -44,6 +44,11 @@ The <action> type attribute can be add,u
   <body>
     <release version="1.5" date="not released, yet"
              description="Release 1.5">
+      <action type="update" date="2012-07-08" issue="COMPRESS-188"
+              due-to="Harald Kuhn">
+        Improved exception message if a zip archive cannot be read
+        because of an unsupported compression method.
+      </action>
       <action type="update" date="2012-07-07" issue="COMPRESS-192"
               due-to="Jukka Zitting">
         ArchiveStreamFactory has a setting for file name encoding that

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java?rev=1358814&r1=1358813&r2=1358814&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java Sun Jul  8 18:55:06 2012
@@ -45,6 +45,21 @@ public class UnsupportedZipFeatureExcept
     }
 
     /**
+     * Creates an exception for archives that use an unsupported
+     * compression algorithm.
+     * @param method the method that is not supported
+     * @param entry the entry using the feature
+     * @since 1.5
+     */
+    public UnsupportedZipFeatureException(ZipMethod method,
+                                          ZipArchiveEntry entry) {
+        super("unsupported feature method '" + method.name()
+              +  "' used in entry " + entry.getName());
+        this.reason = Feature.METHOD;
+        this.entry = entry;
+    }
+
+    /**
      * The unsupported feature that has been used.
      */
     public Feature getFeature() {
@@ -87,4 +102,4 @@ public class UnsupportedZipFeatureExcept
             return name;
         }
     }
-}
\ No newline at end of file
+}

Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipMethod.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipMethod.java?rev=1358814&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipMethod.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipMethod.java Sun Jul  8 18:55:06 2012
@@ -0,0 +1,205 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.zip;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+
+/**
+ * List of known compression methods 
+ * 
+ * Many of these methods are currently not supported by commons compress
+ * 
+ * @since 1.5
+ */
+public enum ZipMethod {
+
+    /**
+     * Compression method 0 for uncompressed entries.
+     * 
+     * @see ZipEntry#STORED
+     */
+    STORED(ZipEntry.STORED),
+
+    /**
+     * UnShrinking.
+     * dynamic Lempel-Ziv-Welch-Algorithm
+     * 
+     * @see http://www.pkware.com/documents/casestudies/APPNOTE.TXT J.
+     * Explanation of fields: compression method: (2 bytes)
+     */
+    UNSHRINKING(1),
+
+    /**
+     * Reduced with compression factor 1.
+     * 
+     * @see http://www.pkware.com/documents/casestudies/APPNOTE.TXT J.
+     * Explanation of fields: compression method: (2 bytes)
+     */
+    EXPANDING_LEVEL_1(2),
+
+    /**
+     * Reduced with compression factor 2.
+     * 
+     * @see http://www.pkware.com/documents/casestudies/APPNOTE.TXT J.
+     * Explanation of fields: compression method: (2 bytes)
+     */
+    EXPANDING_LEVEL_2(3),
+
+    /**
+     * Reduced with compression factor 3.
+     * 
+     * @see http://www.pkware.com/documents/casestudies/APPNOTE.TXT J.
+     * Explanation of fields: compression method: (2 bytes)
+     */
+    EXPANDING_LEVEL_3(4),
+
+    /**
+     * Reduced with compression factor 4.
+     * 
+     * @see http://www.pkware.com/documents/casestudies/APPNOTE.TXT J.
+     * Explanation of fields: compression method: (2 bytes)
+     */
+    EXPANDING_LEVEL_4(5),
+
+    /**
+     * Imploding.
+     * 
+     * @see http://www.pkware.com/documents/casestudies/APPNOTE.TXT J.
+     * Explanation of fields: compression method: (2 bytes)
+     */
+    IMPLODING(6),
+
+    /**
+     * Tokenization.
+     * 
+     * @see http://www.pkware.com/documents/casestudies/APPNOTE.TXT J.
+     * Explanation of fields: compression method: (2 bytes)
+     */
+    TOKENIZATION(7),
+
+    /**
+     * Compression method 8 for compressed (deflated) entries.
+     * 
+     * @see ZipEntry#DEFLATED
+     */
+    DEFLATED(ZipEntry.DEFLATED),
+
+    /**
+     * Compression Method 9 for enhanced deflate.
+     * 
+     * @see http://www.winzip.com/wz54.htm
+     */
+    ENHANCED_DEFLATED(9),
+
+    /**
+     * PKWARE Data Compression Library Imploding.
+     * 
+     * @see http://www.winzip.com/wz54.htm
+     */
+    PKWARE_IMPLODING(10),
+
+    /**
+     * Compression Method 12 for bzip2.
+     * 
+     * @see http://www.winzip.com/wz54.htm
+     */
+    BZIP2(12),
+
+    /**
+     * Compression Method 14 for LZMA.
+     * 
+     * @see http://www.7-zip.org/sdk.html
+     * @see http://www.winzip.com/wz54.htm
+     */
+    LZMA(14),
+
+
+    /**
+     * Compression Method 96 for Jpeg compression.
+     * 
+     * @see http://www.winzip.com/wz54.htm
+     */
+    JPEG(96),
+
+    /**
+     * Compression Method 97 for WavPack.
+     * 
+     * @see http://www.winzip.com/wz54.htm
+     */
+    WAVPACK(97),
+
+    /**
+     * Compression Method 98 for PPMd.
+     * 
+     * @see http://www.winzip.com/wz54.htm
+     */
+    PPMD(98),
+
+
+    /**
+     * Compression Method 99 for AES encryption.
+     * 
+     * @see http://www.winzip.com/wz54.htm
+     */
+    AES_ENCRYPTED(99),
+
+    /**
+     * Unknown compression method.
+     */
+    UNKNOWN(-1);
+
+    private final int code;
+
+    private static final Map<Integer, ZipMethod> codeToEnum =
+        new HashMap<Integer, ZipMethod>();
+
+    static {
+        for (ZipMethod method : values()) {
+            codeToEnum.put(Integer.valueOf(method.getCode()), method);
+        }
+    }
+
+    /**
+     * private constructor for enum style class.
+     */
+    ZipMethod(int code) {
+        this.code = code;
+    }
+
+    /**
+     * the code of the compression method.
+     * 
+     * @see ZipArchiveEntry#getMethod()
+     * 
+     * @return an integer code for the method
+     */
+    public int getCode() {
+        return code;
+    }
+
+
+    /**
+     * returns the {@link ZipMethod} for the given code or null if the
+     * method is not known.
+     */
+    public static ZipMethod getMethodByCode(int code) {
+        return codeToEnum.get(Integer.valueOf(code));
+    }
+}

Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipMethod.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java?rev=1358814&r1=1358813&r2=1358814&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java Sun Jul  8 18:55:06 2012
@@ -225,9 +225,14 @@ public abstract class ZipUtil {
                                                    .Feature.ENCRYPTION, ze);
         }
         if (!supportsMethodOf(ze)) {
-            throw
-                new UnsupportedZipFeatureException(UnsupportedZipFeatureException
-                                                   .Feature.METHOD, ze);
+            ZipMethod m = ZipMethod.getMethodByCode(ze.getMethod());
+            if (m == null) {
+                throw
+                    new UnsupportedZipFeatureException(UnsupportedZipFeatureException
+                                                       .Feature.METHOD, ze);
+            } else {
+                throw new UnsupportedZipFeatureException(m, ze);
+            }
         }
     }
-}
\ No newline at end of file
+}