commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r746933 - in /commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip: ZipEncodingHelper.java ZipFile.java
Date Mon, 23 Feb 2009 09:02:01 GMT
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());
+                }
             }
         }
     }



Mime
View raw message