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 2009/02/23 10:02:01 UTC

svn commit: r746933 - in /commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip: ZipEncodingHelper.java ZipFile.java

Author: bodewig
Date: Mon Feb 23 09:01:59 2009
New Revision: 746933

URL: http://svn.apache.org/viewvc?rev=746933&view=rev
Log:
use NIO to decode names as well

Modified:
    commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
    commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java?rev=746933&r1=746932&r2=746933&view=diff
==============================================================================
--- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java (original)
+++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java Mon Feb 23 09:01:59 2009
@@ -168,4 +168,18 @@
 
         return enc.canEncode(name);
     }
+
+    /**
+     * Decode a filename or a comment from a byte array.
+     * 
+     * @param name The filename or comment.
+     * @param encoding A valid encoding name. The standard zip
+     *                 encoding is <code>"CP437"</code>,
+     *                 <code>"UTF-8"</code> is supported in ZIP file
+     *                 version <code>6.3</code> or later.
+     */
+    static final String decodeName(byte[] name, String encoding) {
+        Charset cs = Charset.forName(encoding);
+        return cs.decode(ByteBuffer.wrap(name)).toString();
+    }
 }

Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=746933&r1=746932&r2=746933&view=diff
==============================================================================
--- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java (original)
+++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java Mon Feb 23 09:01:59 2009
@@ -538,9 +538,15 @@
             return new String(bytes);
         } else {
             try {
-                return new String(bytes, enc);
-            } catch (UnsupportedEncodingException uee) {
-                throw new ZipException(uee.getMessage());
+                return ZipEncodingHelper.decodeName(bytes, encoding);
+            } catch (java.nio.charset.UnsupportedCharsetException ex) {
+                // Java 1.4's NIO doesn't recognize a few names that
+                // String.getBytes does
+                try {
+                    return new String(bytes, enc);
+                } catch (UnsupportedEncodingException uee) {
+                    throw new ZipException(uee.getMessage());
+                }
             }
         }
     }