From 8264511a27c63509a8201c2a4861eea5b60c4023 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 23 Feb 2009 09:08:11 +0000 Subject: [PATCH] use nio for decoding of names - merge from commons-compress rev 746933 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@746934 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/zip/ZipEncodingHelper.java | 14 ++++++++++++++ src/main/org/apache/tools/zip/ZipFile.java | 12 +++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/org/apache/tools/zip/ZipEncodingHelper.java b/src/main/org/apache/tools/zip/ZipEncodingHelper.java index 8c9066696..92e759d86 100644 --- a/src/main/org/apache/tools/zip/ZipEncodingHelper.java +++ b/src/main/org/apache/tools/zip/ZipEncodingHelper.java @@ -168,4 +168,18 @@ abstract class ZipEncodingHelper { 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 "CP437", + * "UTF-8" is supported in ZIP file + * version 6.3 or later. + */ + static final String decodeName(byte[] name, String encoding) { + Charset cs = Charset.forName(encoding); + return cs.decode(ByteBuffer.wrap(name)).toString(); + } } diff --git a/src/main/org/apache/tools/zip/ZipFile.java b/src/main/org/apache/tools/zip/ZipFile.java index 44a160de0..5c551c1f2 100644 --- a/src/main/org/apache/tools/zip/ZipFile.java +++ b/src/main/org/apache/tools/zip/ZipFile.java @@ -551,9 +551,15 @@ public class ZipFile { 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()); + } } } }