Browse Source

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
master
Stefan Bodewig 24 years ago
parent
commit
fc7cfffc89
8 changed files with 119 additions and 5 deletions
  1. +6
    -0
      WHATSNEW
  2. +8
    -0
      docs/manual/CoreTasks/ear.html
  3. +8
    -0
      docs/manual/CoreTasks/jar.html
  4. +8
    -0
      docs/manual/CoreTasks/war.html
  5. +14
    -0
      docs/manual/CoreTasks/zip.html
  6. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/Jar.java
  7. +18
    -0
      src/main/org/apache/tools/ant/taskdefs/Zip.java
  8. +56
    -5
      src/main/org/apache/tools/zip/ZipOutputStream.java

+ 6
- 0
WHATSNEW View File

@@ -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 <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.



+ 8
- 0
docs/manual/CoreTasks/ear.html View File

@@ -43,6 +43,14 @@ attributes of zipfilesets in a Zip or Jar task.)</p>
<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 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


+ 8
- 0
docs/manual/CoreTasks/jar.html View File

@@ -65,6 +65,14 @@ include an empty one for you.)</p>
<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 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


+ 8
- 0
docs/manual/CoreTasks/war.html View File

@@ -45,6 +45,14 @@ attributes of zipfilesets in a Zip or Jar task.)</p>
<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 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


+ 14
- 0
docs/manual/CoreTasks/zip.html View File

@@ -38,6 +38,12 @@ If <code>skip</code> (the default), the ZIP is not created and a warning is issu
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>
@@ -60,6 +66,14 @@ which should be recognized as such by compliant ZIP manipulation tools.</p>
<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>
<td valign="top">includes</td>
<td valign="top">comma separated list of patterns of files that must be


+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/Jar.java View File

@@ -75,6 +75,7 @@ public class Jar extends Zip {
super();
archiveType = "jar";
emptyBehavior = "create";
setEncoding("UTF8");
}

public void setJarfile(File jarFile) {


+ 18
- 0
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -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.
*
* <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 @@ 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);


+ 56
- 5
src/main/org/apache/tools/zip/ZipOutputStream.java View File

@@ -173,6 +173,17 @@ public class ZipOutputStream extends DeflaterOutputStream {
*/
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.
*
@@ -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.
*
* <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 @@ 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());
}
}
}

}

Loading…
Cancel
Save