From fc7cfffc89d82da7426c14b0fcd48682b026a56d Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
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.
Not only store data but also compress them, defaults to true | No | |
encoding | +The 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()); + } + } + } + } |