From 272ec48fce817f88b217db240d51bb6c01ce9b91 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 5 Mar 2009 04:38:07 +0000 Subject: [PATCH] ensure the same encoding is used for name and comment in all places. Submitted by Wolfgang Glas git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@750311 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/zip/ZipOutputStream.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/org/apache/tools/zip/ZipOutputStream.java b/src/main/org/apache/tools/zip/ZipOutputStream.java index bdc2f825c..0ddb17a7d 100644 --- a/src/main/org/apache/tools/zip/ZipOutputStream.java +++ b/src/main/org/apache/tools/zip/ZipOutputStream.java @@ -682,12 +682,16 @@ public class ZipOutputStream extends FilterOutputStream { protected void writeLocalFileHeader(ZipEntry ze) throws IOException { boolean encodable = zipEncoding.canEncode(ze.getName()); - ByteBuffer name; + + final ZipEncoding entryEncoding; + if (!encodable && fallbackToUTF8) { - name = ZipEncodingHelper.UTF8_ZIP_ENCODING.encode(ze.getName()); + entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING; } else { - name = zipEncoding.encode(ze.getName()); + entryEncoding = zipEncoding; } + + ByteBuffer name = entryEncoding.encode(ze.getName()); if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER) { @@ -706,7 +710,7 @@ public class ZipOutputStream extends FilterOutputStream { if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS || !commentEncodable) { - ByteBuffer commentB = this.zipEncoding.encode(comm); + ByteBuffer commentB = entryEncoding.encode(comm); ze.addExtraField(new UnicodeCommentExtraField(comm, commentB.array(), commentB.arrayOffset(), @@ -836,12 +840,16 @@ public class ZipOutputStream extends FilterOutputStream { // CheckStyle:MagicNumber ON // file name length - ByteBuffer name; + final ZipEncoding entryEncoding; + if (!encodable && fallbackToUTF8) { - name = ZipEncodingHelper.UTF8_ZIP_ENCODING.encode(ze.getName()); + entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING; } else { - name = zipEncoding.encode(ze.getName()); + entryEncoding = zipEncoding; } + + ByteBuffer name = entryEncoding.encode(ze.getName()); + writeOut(ZipShort.getBytes(name.limit())); written += SHORT; @@ -855,12 +863,9 @@ public class ZipOutputStream extends FilterOutputStream { if (comm == null) { comm = ""; } - ByteBuffer commentB; - if (!encodable && fallbackToUTF8) { - commentB = ZipEncodingHelper.UTF8_ZIP_ENCODING.encode(comm); - } else { - commentB = zipEncoding.encode(comm); - } + + ByteBuffer commentB = entryEncoding.encode(comm); + writeOut(ZipShort.getBytes(commentB.limit())); written += SHORT;