bodewig 01/07/05 06:10:27
Modified: . WHATSNEW
docs/manual/CoreTasks ear.html jar.html war.html zip.html
src/main/org/apache/tools/ant/taskdefs Jar.java Zip.java
src/main/org/apache/tools/zip ZipOutputStream.java
Log:
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).
Revision Changes Path
1.120 +6 -0 jakarta-ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
retrieving revision 1.119
retrieving revision 1.120
diff -u -r1.119 -r1.120
--- WHATSNEW 2001/07/04 15:05:17 1.119
+++ WHATSNEW 2001/07/05 13:10:24 1.120
@@ -12,6 +12,12 @@
* several Zip methods have changed their signature as we now use a Zip
package of our own that handles Unix permissions for directories.
+ Furthermore <zip> 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 <pvcs> task has been move to a package of its own.
1.3 +8 -0 jakarta-ant/docs/manual/CoreTasks/ear.html
Index: ear.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/ear.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ear.html 2001/04/04 13:35:03 1.2
+++ ear.html 2001/07/05 13:10:24 1.3
@@ -44,6 +44,14 @@
<td align="center" valign="top">No</td>
</tr>
<tr>
+ <td valign="top">encoding</td>
+ <td valign="top">The character encoding to use for filenames
+ inside the archive. Defaults to UTF8. <strong>It is not
+ recommended to change this value as the created archive will most
+ likely be unreadable for Java otherwise.</strong></td>
+ <td align="center" valign="top">No</td>
+ </tr>
+ <tr>
<td valign="top">includes</td>
<td valign="top">comma separated list of patterns of files that must be
included. All files are included when omitted.</td>
1.4 +8 -0 jakarta-ant/docs/manual/CoreTasks/jar.html
Index: jar.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/jar.html,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- jar.html 2001/04/04 13:35:04 1.3
+++ jar.html 2001/07/05 13:10:24 1.4
@@ -66,6 +66,14 @@
<td align="center" valign="top">No</td>
</tr>
<tr>
+ <td valign="top">encoding</td>
+ <td valign="top">The character encoding to use for filenames
+ inside the archive. Defaults to UTF8. <strong>It is not
+ recommended to change this value as the created archive will most
+ likely be unreadable for Java otherwise.</strong></td>
+ <td align="center" valign="top">No</td>
+ </tr>
+ <tr>
<td valign="top">includes</td>
<td valign="top">comma separated list of patterns of files that must be
included. All files are included when omitted.</td>
1.4 +8 -0 jakarta-ant/docs/manual/CoreTasks/war.html
Index: war.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/war.html,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- war.html 2001/04/04 13:35:04 1.3
+++ war.html 2001/07/05 13:10:24 1.4
@@ -46,6 +46,14 @@
<td align="center" valign="top">No</td>
</tr>
<tr>
+ <td valign="top">encoding</td>
+ <td valign="top">The character encoding to use for filenames
+ inside the archive. Defaults to UTF8. <strong>It is not
+ recommended to change this value as the created archive will most
+ likely be unreadable for Java otherwise.</strong></td>
+ <td align="center" valign="top">No</td>
+ </tr>
+ <tr>
<td valign="top">includes</td>
<td valign="top">comma separated list of patterns of files that must be
included. All files are included when omitted.</td>
1.3 +14 -0 jakarta-ant/docs/manual/CoreTasks/zip.html
Index: zip.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/zip.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- zip.html 2001/02/13 12:31:52 1.2
+++ zip.html 2001/07/05 13:10:24 1.3
@@ -38,6 +38,12 @@
If <code>fail</code>, the ZIP is not created and the build is halted with an
error.
If <code>create</code>, an empty ZIP file (explicitly zero entries) is
created,
which should be recognized as such by compliant ZIP manipulation tools.</p>
+<p>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.</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@@ -58,6 +64,14 @@
<tr>
<td valign="top">compress</td>
<td valign="top">Not only store data but also compress them, defaults to
true</td>
+ <td align="center" valign="top">No</td>
+ </tr>
+ <tr>
+ <td valign="top">encoding</td>
+ <td valign="top">The character encoding to use for filenames
+ inside the zip file. For a list of possible values see <a
+
href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html</a>.
+ Defaults to the platform's default character encoding.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
1.18 +1 -0
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Jar.java
Index: Jar.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Jar.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- Jar.java 2001/04/23 16:12:08 1.17
+++ Jar.java 2001/07/05 13:10:25 1.18
@@ -75,6 +75,7 @@
super();
archiveType = "jar";
emptyBehavior = "create";
+ setEncoding("UTF8");
}
public void setJarfile(File jarFile) {
1.39 +18 -0
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java
Index: Zip.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -r1.38 -r1.39
--- Zip.java 2001/04/25 14:12:14 1.38
+++ Zip.java 2001/07/05 13:10:26 1.39
@@ -86,6 +86,12 @@
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 @@
emptyBehavior = we.getValue();
}
+ /**
+ * Encoding to use for filenames, defaults to the platform's
+ * default encoding.
+ *
+ * <p>For a list of possible values see <a
+ *
href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html</a>.</p>
+ */
+ 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 @@
boolean success = false;
ZipOutputStream zOut =
new ZipOutputStream(new FileOutputStream(zipFile));
+ zOut.setEncoding(encoding);
try {
if (doCompress) {
zOut.setMethod(ZipOutputStream.DEFLATED);
1.3 +57 -6
jakarta-ant/src/main/org/apache/tools/zip/ZipOutputStream.java
Index: ZipOutputStream.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/zip/ZipOutputStream.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ZipOutputStream.java 2001/04/24 08:41:59 1.2
+++ ZipOutputStream.java 2001/07/05 13:10:26 1.3
@@ -78,7 +78,7 @@
* will be called.</p>
*
* @author <a href="[EMAIL PROTECTED]">Stefan Bodewig</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class ZipOutputStream extends DeflaterOutputStream {
@@ -174,6 +174,17 @@
private Hashtable offsets = new Hashtable();
/**
+ * The encoding to use for filenames and the file comment.
+ *
+ * <p>For a list of possible values see <a
+ *
href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html</a>.
+ * Defaults to the platform's default character encoding.</p>
+ *
+ * @since 1.3
+ */
+ private String encoding = null;
+
+ /**
* Compression method for deflated entries.
*
* @since 1.1
@@ -196,10 +207,32 @@
super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
}
+ /**
+ * The encoding to use for filenames and the file comment.
+ *
+ * <p>For a list of possible values see <a
+ *
href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html</a>.
+ * Defaults to the platform's default character encoding.</p>
+ *
+ * @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.</p>
+ * ourselves.
*/
/**
@@ -426,7 +459,7 @@
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 @@
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 @@
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 @@
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);
}
@@ -614,6 +647,24 @@
result[2] = (byte) ((value & 0xFF0000) >> 16);
result[3] = (byte) ((value & 0xFF000000l) >> 24);
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());
+ }
+ }
}
}