You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2017/01/12 05:53:30 UTC

ant git commit: xz support in tar - PR 60350

Repository: ant
Updated Branches:
  refs/heads/master 14ad5c43c -> f479b5f52


xz support in tar - PR 60350


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/f479b5f5
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/f479b5f5
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/f479b5f5

Branch: refs/heads/master
Commit: f479b5f52f27fe13bf439be5368ebce699252219
Parents: 14ad5c4
Author: Stefan Bodewig <bo...@apache.org>
Authored: Thu Jan 12 06:53:09 2017 +0100
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Thu Jan 12 06:53:09 2017 +0100

----------------------------------------------------------------------
 src/main/org/apache/tools/ant/taskdefs/Tar.java | 36 +++++++++++++++-
 src/tests/antunit/taskdefs/tar-test.xml         | 44 ++++++++++++++++++++
 2 files changed, 78 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/f479b5f5/src/main/org/apache/tools/ant/taskdefs/Tar.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/Tar.java b/src/main/org/apache/tools/ant/taskdefs/Tar.java
index 9754702..815477a 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Tar.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Tar.java
@@ -24,6 +24,8 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -230,6 +232,7 @@ public class Tar extends MatchingTask {
      * <li>  none - no compression
      * <li>  gzip - Gzip compression
      * <li>  bzip2 - Bzip2 compression
+     * <li>xz - XZ compression, requires XZ for Java
      * </ul>
      * @param mode the compression method.
      */
@@ -971,7 +974,11 @@ public class Tar extends MatchingTask {
          *    BZIP2 compression
          */
         private static final String BZIP2 = "bzip2";
-
+        /**
+         *  XZ compression
+         * @since 1.10.1
+         */
+        private static final String XZ = "xz";
 
         /**
          * Default constructor
@@ -987,7 +994,7 @@ public class Tar extends MatchingTask {
          */
         @Override
         public String[] getValues() {
-            return new String[] {NONE, GZIP, BZIP2 };
+            return new String[] {NONE, GZIP, BZIP2, XZ };
         }
 
         /**
@@ -1003,6 +1010,8 @@ public class Tar extends MatchingTask {
             final String v = getValue();
             if (GZIP.equals(v)) {
                 return new GZIPOutputStream(ostream);
+            } else if (XZ.equals(v)) {
+                return newXZOutputStream(ostream);
             } else {
                 if (BZIP2.equals(v)) {
                     ostream.write('B');
@@ -1012,5 +1021,28 @@ public class Tar extends MatchingTask {
             }
             return ostream;
         }
+
+        private static OutputStream newXZOutputStream(OutputStream ostream)
+            throws BuildException {
+            try {
+                Class<?> fClazz = Class.forName("org.tukaani.xz.FilterOptions");
+                Class<?> oClazz = Class.forName("org.tukaani.xz.LZMA2Options");
+                Class<? extends OutputStream> sClazz =
+                    Class.forName("org.tukaani.xz.XZOutputStream")
+                    .asSubclass(OutputStream.class);
+                Constructor<? extends OutputStream> c =
+                    sClazz.getConstructor(OutputStream.class, fClazz);
+                return c.newInstance(ostream, oClazz.newInstance());
+            } catch (ClassNotFoundException ex) {
+                throw new BuildException("xz compression requires the XZ for Java library",
+                                         ex);
+            } catch (NoSuchMethodException
+                     | InstantiationException
+                     | IllegalAccessException
+                     | InvocationTargetException
+                     ex) {
+                throw new BuildException("failed to create XZOutputStream", ex);
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/ant/blob/f479b5f5/src/tests/antunit/taskdefs/tar-test.xml
----------------------------------------------------------------------
diff --git a/src/tests/antunit/taskdefs/tar-test.xml b/src/tests/antunit/taskdefs/tar-test.xml
index 1aac9a8..d83b5f9 100644
--- a/src/tests/antunit/taskdefs/tar-test.xml
+++ b/src/tests/antunit/taskdefs/tar-test.xml
@@ -24,6 +24,7 @@
   <target name="setUp">
     <mkdir dir="${input}" />
     <mkdir dir="${output}" />
+    <available property="xz.present" classname="org.tukaani.xz.XZOutputStream"/>
   </target>
 
   <target name="testTarFilesetHandlesFilesetReferences" depends="setUp">
@@ -115,4 +116,47 @@
     <untar dest="${output}" src="${output}/x.tar"/>
     <au:assertFileExists file="${output}/${longfile.file.name}"/>
   </target>
+
+  <target name="testGzipCompression" depends="setUp">
+    <au:assertFileExists file="../../../etc/testcases/asf-logo.gif"/>
+    <tar destfile="${input}/asf-logo.gif.tar.gz" compression="gzip">
+      <file file="../../../etc/testcases/asf-logo.gif"/>
+    </tar>
+    <gunzip dest="${input}/asf-logo.gif.tar">
+      <file file="${input}/asf-logo.gif.tar.gz"/>
+    </gunzip>
+    <untar src="${input}/asf-logo.gif.tar" dest="${output}"/>
+    <au:assertFilesMatch
+        expected="../../../etc/testcases/asf-logo.gif"
+        actual="${output}/asf-logo.gif"/>
+  </target>
+
+  <target name="testBzip2Compression" depends="setUp">
+    <au:assertFileExists file="../../../etc/testcases/asf-logo.gif"/>
+    <tar destfile="${input}/asf-logo.gif.tar.bz2" compression="bzip2">
+      <file file="../../../etc/testcases/asf-logo.gif"/>
+    </tar>
+    <bunzip2 dest="${input}/asf-logo.gif.tar">
+      <file file="${input}/asf-logo.gif.tar.bz2"/>
+    </bunzip2>
+    <untar src="${input}/asf-logo.gif.tar" dest="${output}"/>
+    <au:assertFilesMatch
+        expected="../../../etc/testcases/asf-logo.gif"
+        actual="${output}/asf-logo.gif"/>
+  </target>
+
+  <target name="testXzCompression" depends="setUp" if="xz.present">
+    <au:assertFileExists file="../../../etc/testcases/asf-logo.gif"/>
+    <tar destfile="${input}/asf-logo.gif.tar.xz" compression="xz">
+      <file file="../../../etc/testcases/asf-logo.gif"/>
+    </tar>
+    <unxz dest="${input}/asf-logo.gif.tar">
+      <file file="${input}/asf-logo.gif.tar.xz"/>
+    </unxz>
+    <untar src="${input}/asf-logo.gif.tar" dest="${output}"/>
+    <au:assertFilesMatch
+        expected="../../../etc/testcases/asf-logo.gif"
+        actual="${output}/asf-logo.gif"/>
+  </target>
+
 </project>