From 1a584200e27de7867d7f613ff9c910dc0cc9730a Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 21 Apr 2015 21:36:07 +0200 Subject: [PATCH] Add encoding attributes to , and This is a workaround for Bugzilla Issue 57822 https://bz.apache.org/bugzilla/show_bug.cgi?id=57822 --- WHATSNEW | 10 ++++++ manual/Tasks/tar.html | 9 ++++++ manual/Tasks/unzip.html | 7 ++-- manual/Types/tarfileset.html | 9 ++++++ manual/Types/zipfileset.html | 1 - src/etc/testcases/taskdefs/untar.xml | 8 +++-- .../org/apache/tools/ant/taskdefs/Expand.java | 18 ++++++++++- .../org/apache/tools/ant/taskdefs/Tar.java | 23 ++++++++++++- .../org/apache/tools/ant/taskdefs/Untar.java | 19 ++++------- .../tools/ant/types/ArchiveFileSet.java | 30 +++++++++++++++++ .../apache/tools/ant/types/TarFileSet.java | 1 + .../apache/tools/ant/types/TarScanner.java | 4 +-- .../apache/tools/ant/types/ZipFileSet.java | 32 +------------------ src/tests/antunit/types/tarfileset-test.xml | 11 +++++++ .../apache/tools/ant/taskdefs/UntarTest.java | 10 +++--- 15 files changed, 132 insertions(+), 60 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 939c5dc29..e65a1ddac 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -84,6 +84,16 @@ Fixed bugs: * TarEntry's constructor with a File and a String arg didn't normalize the name. + * Between 1.8.4 and 1.9.5 TarArchiveInputStream started to parse file + names using the platform's default encoding rather than as ASCII. + This has been a breaking change that has never been marked as such + (in fact it went unnoticed). In order to allow and + to work on platforms who's encoding doesn't match the + encoding of file names inside the archive, the both now support + encoding attributes. + The attribute has also been added to for symmetry. + Bugzilla Report 57822 + Other changes: -------------- diff --git a/manual/Tasks/tar.html b/manual/Tasks/tar.html index be9bc04d5..78cd966d2 100644 --- a/manual/Tasks/tar.html +++ b/manual/Tasks/tar.html @@ -146,6 +146,15 @@ or "bzip2".

"none". No + + encoding + The character encoding to use for filenames + inside the tar file. For a list of possible values see the Supported Encodings.
+ Defaults to the platform's default character encoding. + Since Ant 1.9.4 + No +

Nested Elements

diff --git a/manual/Tasks/unzip.html b/manual/Tasks/unzip.html index 02df7acf9..8d93b7041 100644 --- a/manual/Tasks/unzip.html +++ b/manual/Tasks/unzip.html @@ -100,12 +100,13 @@ archive.

encoding - Note: This attribute is not available for - the untar task.
+ The character encoding that has been used for filenames inside the zip file. For a list of possible values see the Supported Encodings.
- Defaults to "UTF8", use the magic value + Defaults to "UTF8" for the unzip and the + platform's default encoding for the untar task. Use + the magic value native-encoding for the platform's default character encoding.
See also the discussion in the diff --git a/manual/Types/tarfileset.html b/manual/Types/tarfileset.html index 34355b8d8..499ed7984 100644 --- a/manual/Types/tarfileset.html +++ b/manual/Types/tarfileset.html @@ -130,6 +130,15 @@ directories. Default is 755. No + + encoding + The character encoding to use for filenames + inside the zip file. For a list of possible values see the Supported Encodings. + Defaults to the platform's default character encoding. + Since Ant 1.9.5 + No +

The fullpath attribute can only be set for filesets that diff --git a/manual/Types/zipfileset.html b/manual/Types/zipfileset.html index 486148a70..955e2718e 100644 --- a/manual/Types/zipfileset.html +++ b/manual/Types/zipfileset.html @@ -97,7 +97,6 @@ directories. Default is 755. since Ant 1.5.2. inside the zip file. For a list of possible values see the Supported Encodings. Defaults to the platform's default character encoding. - Only supported by zipfileset. No diff --git a/src/etc/testcases/taskdefs/untar.xml b/src/etc/testcases/taskdefs/untar.xml index e1490d847..04d44b37f 100644 --- a/src/etc/testcases/taskdefs/untar.xml +++ b/src/etc/testcases/taskdefs/untar.xml @@ -57,8 +57,12 @@ - - + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Expand.java b/src/main/org/apache/tools/ant/taskdefs/Expand.java index 8722e2402..cb0c958d5 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Expand.java +++ b/src/main/org/apache/tools/ant/taskdefs/Expand.java @@ -72,12 +72,28 @@ public class Expand extends Task { public static final String NATIVE_ENCODING = "native-encoding"; - private String encoding = "UTF8"; + private String encoding; /** Error message when more that one mapper is defined */ public static final String ERROR_MULTIPLE_MAPPERS = "Cannot define more than one mapper"; private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); + /** + * Creates an Expand instance and sets encoding to UTF-8. + */ + public Expand() { + this("UTF8"); + } + + /** + * Creates an Expand instance and sets the given encoding. + * + * @since Ant 1.9.5 + */ + protected Expand(String encoding) { + this.encoding = encoding; + } + /** * Whether try ing to expand an empty archive would be an error. * diff --git a/src/main/org/apache/tools/ant/taskdefs/Tar.java b/src/main/org/apache/tools/ant/taskdefs/Tar.java index 7f50ddceb..97547022d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tar.java @@ -123,6 +123,12 @@ public class Tar extends MatchingTask { private TarCompressionMethod compression = new TarCompressionMethod(); + /** + * Encoding to use for filenames, defaults to the platform's + * default encoding. + */ + private String encoding; + /** * Add a new fileset with the option to specify permissions * @return the tar fileset to be used as the nested element. @@ -231,6 +237,20 @@ public class Tar extends MatchingTask { this.compression = mode; } + /** + * Encoding to use for filenames, defaults to the platform's + * default encoding. + * + *

For a list of possible values see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html.

+ * @param encoding the encoding name + * + * @since Ant 1.9.5 + */ + public void setEncoding(final String encoding) { + this.encoding = encoding; + } + /** * do the business * @throws BuildException on error @@ -304,7 +324,8 @@ public class Tar extends MatchingTask { tOut = new TarOutputStream( compression.compress( new BufferedOutputStream( - new FileOutputStream(tarFile)))); + new FileOutputStream(tarFile))), + encoding); tOut.setDebug(true); if (longFileMode.isTruncateMode()) { tOut.setLongFileMode(TarOutputStream.LONGFILE_TRUNCATE); diff --git a/src/main/org/apache/tools/ant/taskdefs/Untar.java b/src/main/org/apache/tools/ant/taskdefs/Untar.java index 5e033f7fb..8343aec52 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Untar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Untar.java @@ -58,6 +58,10 @@ public class Untar extends Expand { */ private UntarCompressionMethod compression = new UntarCompressionMethod(); + public Untar() { + super(null); + } + /** * Set decompression algorithm to use; default=none. * @@ -74,18 +78,6 @@ public class Untar extends Expand { compression = method; } - /** - * No encoding support in Untar. - * @param encoding not used - * @throws BuildException always - * @since Ant 1.6 - */ - public void setEncoding(String encoding) { - throw new BuildException("The " + getTaskName() - + " task doesn't support the encoding" - + " attribute", getLocation()); - } - /** * No unicode extra fields in tar. * @@ -157,7 +149,8 @@ public class Untar extends Expand { try { tis = new TarInputStream(compression.decompress(name, - new BufferedInputStream(stream))); + new BufferedInputStream(stream)), + getEncoding()); log("Expanding: " + name + " into " + dir, Project.MSG_INFO); TarEntry te = null; boolean empty = true; diff --git a/src/main/org/apache/tools/ant/types/ArchiveFileSet.java b/src/main/org/apache/tools/ant/types/ArchiveFileSet.java index 2e1659a5b..e9a07303f 100644 --- a/src/main/org/apache/tools/ant/types/ArchiveFileSet.java +++ b/src/main/org/apache/tools/ant/types/ArchiveFileSet.java @@ -72,6 +72,8 @@ public abstract class ArchiveFileSet extends FileSet { private boolean errorOnMissingArchive = true; + private String encoding = null; + /** Constructor for ArchiveFileSet */ public ArchiveFileSet() { super(); @@ -100,6 +102,7 @@ public abstract class ArchiveFileSet extends FileSet { fileModeHasBeenSet = fileset.fileModeHasBeenSet; dirModeHasBeenSet = fileset.dirModeHasBeenSet; errorOnMissingArchive = fileset.errorOnMissingArchive; + encoding = fileset.encoding; } /** @@ -267,6 +270,33 @@ public abstract class ArchiveFileSet extends FileSet { return fullpath; } + /** + * Set the encoding used for this ZipFileSet. + * @param enc encoding as String. + * @since Ant 1.9.5 + */ + public void setEncoding(String enc) { + checkAttributesAllowed(); + this.encoding = enc; + } + + /** + * Get the encoding used for this ZipFileSet. + * @return String encoding. + * @since Ant 1.9.5 + */ + public String getEncoding() { + if (isReference()) { + AbstractFileSet ref = getRef(getProject()); + if (ref instanceof ArchiveFileSet) { + return ((ArchiveFileSet) ref).getEncoding(); + } else { + return null; + } + } + return encoding; + } + /** * Creates a scanner for this type of archive. * @return the scanner. diff --git a/src/main/org/apache/tools/ant/types/TarFileSet.java b/src/main/org/apache/tools/ant/types/TarFileSet.java index eec1dfa0c..6446e9bf0 100644 --- a/src/main/org/apache/tools/ant/types/TarFileSet.java +++ b/src/main/org/apache/tools/ant/types/TarFileSet.java @@ -180,6 +180,7 @@ public class TarFileSet extends ArchiveFileSet { */ protected ArchiveScanner newArchiveScanner() { TarScanner zs = new TarScanner(); + zs.setEncoding(getEncoding()); return zs; } diff --git a/src/main/org/apache/tools/ant/types/TarScanner.java b/src/main/org/apache/tools/ant/types/TarScanner.java index 1bac2f404..a3c7f6d52 100644 --- a/src/main/org/apache/tools/ant/types/TarScanner.java +++ b/src/main/org/apache/tools/ant/types/TarScanner.java @@ -58,7 +58,7 @@ public class TarScanner extends ArchiveScanner { try { try { - ti = new TarInputStream(src.getInputStream()); + ti = new TarInputStream(src.getInputStream(), encoding); } catch (IOException ex) { throw new BuildException("problem opening " + srcFile, ex); } @@ -84,4 +84,4 @@ public class TarScanner extends ArchiveScanner { FileUtils.close(ti); } } -} \ No newline at end of file +} diff --git a/src/main/org/apache/tools/ant/types/ZipFileSet.java b/src/main/org/apache/tools/ant/types/ZipFileSet.java index eb6a694dc..24f0ccd55 100644 --- a/src/main/org/apache/tools/ant/types/ZipFileSet.java +++ b/src/main/org/apache/tools/ant/types/ZipFileSet.java @@ -33,8 +33,6 @@ import org.apache.tools.ant.Project; */ public class ZipFileSet extends ArchiveFileSet { - private String encoding = null; - /** Constructor for ZipFileSet */ public ZipFileSet() { super(); @@ -54,34 +52,6 @@ public class ZipFileSet extends ArchiveFileSet { */ protected ZipFileSet(ZipFileSet fileset) { super(fileset); - encoding = fileset.encoding; - } - - /** - * Set the encoding used for this ZipFileSet. - * @param enc encoding as String. - * @since Ant 1.7 - */ - public void setEncoding(String enc) { - checkZipFileSetAttributesAllowed(); - this.encoding = enc; - } - - /** - * Get the encoding used for this ZipFileSet. - * @return String encoding. - * @since Ant 1.7 - */ - public String getEncoding() { - if (isReference()) { - AbstractFileSet ref = getRef(getProject()); - if (ref instanceof ZipFileSet) { - return ((ZipFileSet) ref).getEncoding(); - } else { - return null; - } - } - return encoding; } /** @@ -90,7 +60,7 @@ public class ZipFileSet extends ArchiveFileSet { */ protected ArchiveScanner newArchiveScanner() { ZipScanner zs = new ZipScanner(); - zs.setEncoding(encoding); + zs.setEncoding(getEncoding()); return zs; } diff --git a/src/tests/antunit/types/tarfileset-test.xml b/src/tests/antunit/types/tarfileset-test.xml index 02a2e6788..3c4d3a981 100644 --- a/src/tests/antunit/types/tarfileset-test.xml +++ b/src/tests/antunit/types/tarfileset-test.xml @@ -34,4 +34,15 @@ + + + + + + + diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/UntarTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/UntarTest.java index 5e197dfbf..e0f7eb2a1 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/UntarTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/UntarTest.java @@ -84,12 +84,10 @@ public class UntarTest { @Test public void testEncoding() { - try { - buildRule.executeTarget("encoding"); - fail(" overrides setEncoding."); - } catch (BuildException ex) { - assertEquals("The untar task doesn't support the encoding attribute", ex.getMessage()); - } + buildRule.executeTarget("encodingTest"); + String filename = buildRule.getProject().getProperty("output") + "/untartestout/foo"; + assertTrue("foo has been properly named", + buildRule.getProject().resolveFile(filename).exists()); } @Test