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()); + } } } }