From fc7cfffc89d82da7426c14b0fcd48682b026a56d Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 5 Jul 2001 13:10:26 +0000 Subject: [PATCH] Add encoding attribute to the zip tasks and the ZipOutputStream to make the classes flexible enough to deal with non US-ASCII filenames either consistent with the command line ZIP tools (it has been since it uses the org.apache classes instead of java.util.zip) or the jar command (which it has been up to Ant 1.3). git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269272 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 6 ++ docs/manual/CoreTasks/ear.html | 8 +++ docs/manual/CoreTasks/jar.html | 8 +++ docs/manual/CoreTasks/war.html | 8 +++ docs/manual/CoreTasks/zip.html | 14 +++++ .../org/apache/tools/ant/taskdefs/Jar.java | 1 + .../org/apache/tools/ant/taskdefs/Zip.java | 18 ++++++ .../org/apache/tools/zip/ZipOutputStream.java | 61 +++++++++++++++++-- 8 files changed, 119 insertions(+), 5 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 24ece6acd..248f083f0 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -12,6 +12,12 @@ Changes that could break older environments: * several Zip methods have changed their signature as we now use a Zip package of our own that handles Unix permissions for directories. + Furthermore will now use the platform's default character + encoding for filenames - this is consistent with the command line + ZIP tools, but causes problems if you try to open them from within + Java and your filenames contain non US-ASCII characters. Use the new + encoding attribute of the task and set it to UTF8 to get the old + behavior. * The task has been move to a package of its own. diff --git a/docs/manual/CoreTasks/ear.html b/docs/manual/CoreTasks/ear.html index e99a924f7..7372b8e64 100644 --- a/docs/manual/CoreTasks/ear.html +++ b/docs/manual/CoreTasks/ear.html @@ -43,6 +43,14 @@ attributes of zipfilesets in a Zip or Jar task.)

Not only store data but also compress them, defaults to true No + + encoding + The character encoding to use for filenames + inside the archive. Defaults to UTF8. It is not + recommended to change this value as the created archive will most + likely be unreadable for Java otherwise. + No + includes comma separated list of patterns of files that must be diff --git a/docs/manual/CoreTasks/jar.html b/docs/manual/CoreTasks/jar.html index 02f362b74..c9848cba8 100644 --- a/docs/manual/CoreTasks/jar.html +++ b/docs/manual/CoreTasks/jar.html @@ -65,6 +65,14 @@ include an empty one for you.)

Not only store data but also compress them, defaults to true No + + encoding + The character encoding to use for filenames + inside the archive. Defaults to UTF8. It is not + recommended to change this value as the created archive will most + likely be unreadable for Java otherwise. + No + includes comma separated list of patterns of files that must be diff --git a/docs/manual/CoreTasks/war.html b/docs/manual/CoreTasks/war.html index 056eee039..996796f1a 100644 --- a/docs/manual/CoreTasks/war.html +++ b/docs/manual/CoreTasks/war.html @@ -45,6 +45,14 @@ attributes of zipfilesets in a Zip or Jar task.)

Not only store data but also compress them, defaults to true No + + encoding + The character encoding to use for filenames + inside the archive. Defaults to UTF8. It is not + recommended to change this value as the created archive will most + likely be unreadable for Java otherwise. + No + includes comma separated list of patterns of files that must be diff --git a/docs/manual/CoreTasks/zip.html b/docs/manual/CoreTasks/zip.html index 5b89b48a7..0aff1d699 100644 --- a/docs/manual/CoreTasks/zip.html +++ b/docs/manual/CoreTasks/zip.html @@ -38,6 +38,12 @@ If skip (the default), the ZIP is not created and a warning is issu If fail, the ZIP is not created and the build is halted with an error. If create, an empty ZIP file (explicitly zero entries) is created, which should be recognized as such by compliant ZIP manipulation tools.

+

This task will now use the platform's default character encoding +for filenames - this is consistent with the command line ZIP tools, +but causes problems if you try to open them from within Java and your +filenames contain non US-ASCII characters. Use the encoding attribute +and set it to UTF8 to create zip files that can savely be read by +Java.

Parameters

@@ -60,6 +66,14 @@ which should be recognized as such by compliant ZIP manipulation tools.

+ + + + +
Not only store data but also compress them, defaults to true No
encodingThe character encoding to use for filenames + inside the zip file. For a list of possible values see http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html. + Defaults to the platform's default character encoding.No
includes comma separated list of patterns of files that must be diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java index 8134e6661..2c95bfae7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Jar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java @@ -75,6 +75,7 @@ public class Jar extends Zip { super(); archiveType = "jar"; emptyBehavior = "create"; + setEncoding("UTF8"); } public void setJarfile(File jarFile) { diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index 7a63bf4e2..110b862cc 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -85,6 +85,12 @@ public class Zip extends MatchingTask { private Vector filesets = new Vector (); private Hashtable addedDirs = new Hashtable(); + /** + * Encoding to use for filenames, defaults to the platform's + * default encoding. + */ + private String encoding = null; + /** * This is the name/location of where to * create the .zip file. @@ -144,6 +150,17 @@ public class Zip extends MatchingTask { emptyBehavior = we.getValue(); } + /** + * Encoding to use for filenames, defaults to the platform's + * default encoding. + * + *

For a list of possible values see http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html.

+ */ + public void setEncoding(String encoding) { + this.encoding = encoding; + } + public void execute() throws BuildException { if (baseDir == null && filesets.size() == 0 && "zip".equals(archiveType)) { throw new BuildException( "basedir attribute must be set, or at least " + @@ -176,6 +193,7 @@ public class Zip extends MatchingTask { boolean success = false; ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(zipFile)); + zOut.setEncoding(encoding); try { if (doCompress) { zOut.setMethod(ZipOutputStream.DEFLATED); diff --git a/src/main/org/apache/tools/zip/ZipOutputStream.java b/src/main/org/apache/tools/zip/ZipOutputStream.java index 868819f62..086235284 100644 --- a/src/main/org/apache/tools/zip/ZipOutputStream.java +++ b/src/main/org/apache/tools/zip/ZipOutputStream.java @@ -173,6 +173,17 @@ public class ZipOutputStream extends DeflaterOutputStream { */ private Hashtable offsets = new Hashtable(); + /** + * The encoding to use for filenames and the file comment. + * + *

For a list of possible values see http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html. + * Defaults to the platform's default character encoding.

+ * + * @since 1.3 + */ + private String encoding = null; + /** * Compression method for deflated entries. * @@ -196,10 +207,32 @@ public class ZipOutputStream extends DeflaterOutputStream { super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true)); } + /** + * The encoding to use for filenames and the file comment. + * + *

For a list of possible values see http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html. + * Defaults to the platform's default character encoding.

+ * + * @since 1.3 + */ + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + /** + * The encoding to use for filenames and the file comment. + * + * @return null if using the platform's default character encoding. + * + * @since 1.3 + */ + public String getEncoding() {return encoding;} + /* * Found out by experiment, that DeflaterOutputStream.close() * will call finish() - so we don't need to override close - * ourselves.

+ * ourselves. */ /** @@ -426,7 +459,7 @@ public class ZipOutputStream extends DeflaterOutputStream { written += 12; // file name length - byte[] name = ze.getName().getBytes(); + byte[] name = getBytes(ze.getName()); out.write((new ZipShort(name.length)).getBytes()); written += 2; @@ -507,7 +540,7 @@ public class ZipOutputStream extends DeflaterOutputStream { written += 12; // file name length - byte[] name = ze.getName().getBytes(); + byte[] name = getBytes(ze.getName()); out.write((new ZipShort(name.length)).getBytes()); written += 2; @@ -521,7 +554,7 @@ public class ZipOutputStream extends DeflaterOutputStream { if (comm == null) { comm = ""; } - byte[] comment = comm.getBytes(); + byte[] comment = getBytes(comm); out.write((new ZipShort(comment.length)).getBytes()); written += 2; @@ -576,7 +609,7 @@ public class ZipOutputStream extends DeflaterOutputStream { out.write(cdOffset.getBytes()); // ZIP file comment - byte[] data = comment.getBytes(); + byte[] data = getBytes(comment); out.write((new ZipShort(data.length)).getBytes()); out.write(data); } @@ -616,4 +649,22 @@ public class ZipOutputStream extends DeflaterOutputStream { return new ZipLong(result); } + /** + * Retrieve the bytes for the given String in the encoding set for + * this Stream. + * + * @since 1.3 + */ + protected byte[] getBytes(String name) throws ZipException { + if (encoding == null) { + return name.getBytes(); + } else { + try { + return name.getBytes(encoding); + } catch (UnsupportedEncodingException uee) { + throw new ZipException(uee.getMessage()); + } + } + } + }