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/11 21:20:52 UTC

[5/5] ant git commit: xz support in untar - PR 60350

xz support in untar - PR 60350


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

Branch: refs/heads/master
Commit: 14ad5c43cd9daa609aae8a4849362d18b8b69f0a
Parents: 0e3026c
Author: Stefan Bodewig <bo...@apache.org>
Authored: Wed Jan 11 22:20:16 2017 +0100
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Wed Jan 11 22:20:16 2017 +0100

----------------------------------------------------------------------
 .../org/apache/tools/ant/taskdefs/Untar.java    | 32 +++++++++-
 src/tests/antunit/taskdefs/untar-test.xml       | 61 ++++++++++++++++++++
 2 files changed, 92 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/14ad5c43/src/main/org/apache/tools/ant/taskdefs/Untar.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/Untar.java b/src/main/org/apache/tools/ant/taskdefs/Untar.java
index 8343aec..8e841c0 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Untar.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Untar.java
@@ -18,6 +18,8 @@
 
 package org.apache.tools.ant.taskdefs;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -190,6 +192,11 @@ public class Untar extends Expand {
          *  BZIP2 compression
          */
         private static final String BZIP2 = "bzip2";
+        /**
+         *  XZ compression
+         * @since 1.10.1
+         */
+        private static final String XZ = "xz";
 
 
         /**
@@ -206,7 +213,7 @@ public class Untar extends Expand {
          * @return valid values
          */
         public String[] getValues() {
-            return new String[] {NONE, GZIP, BZIP2};
+            return new String[] {NONE, GZIP, BZIP2, XZ};
         }
 
         /**
@@ -226,6 +233,8 @@ public class Untar extends Expand {
             final String v = getValue();
             if (GZIP.equals(v)) {
                 return new GZIPInputStream(istream);
+            } else if (XZ.equals(v)) {
+                return newXZInputStream(istream);
             } else {
                 if (BZIP2.equals(v)) {
                     final char[] magic = new char[] {'B', 'Z'};
@@ -240,5 +249,26 @@ public class Untar extends Expand {
             }
             return istream;
         }
+
+        private static InputStream newXZInputStream(InputStream istream)
+            throws BuildException {
+            try {
+                Class<? extends InputStream> clazz =
+                    Class.forName("org.tukaani.xz.XZInputStream")
+                    .asSubclass(InputStream.class);
+                Constructor<? extends InputStream> c =
+                    clazz.getConstructor(InputStream.class);
+                return c.newInstance(istream);
+            } catch (ClassNotFoundException ex) {
+                throw new BuildException("xz uncompression requires the XZ for Java library",
+                                         ex);
+            } catch (NoSuchMethodException
+                     | InstantiationException
+                     | IllegalAccessException
+                     | InvocationTargetException
+                     ex) {
+                throw new BuildException("failed to create XZInputStream", ex);
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/ant/blob/14ad5c43/src/tests/antunit/taskdefs/untar-test.xml
----------------------------------------------------------------------
diff --git a/src/tests/antunit/taskdefs/untar-test.xml b/src/tests/antunit/taskdefs/untar-test.xml
new file mode 100644
index 0000000..4c04bfe
--- /dev/null
+++ b/src/tests/antunit/taskdefs/untar-test.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<project name="untar-test" default="antunit"
+         xmlns:au="antlib:org.apache.ant.antunit">
+  <import file="../antunit-base.xml" />
+
+  <target name="setUp">
+    <mkdir dir="${input}"/>
+    <mkdir dir="${output}/expected" />
+    <mkdir dir="${output}/actual" />
+    <available property="xz.present" classname="org.tukaani.xz.XZOutputStream"/>
+  </target>
+
+  <target name="testGzipCompression" depends="setUp">
+    <untar src="../../../etc/testcases/taskdefs/expected/asf-logo.gif.tar"
+           dest="${output}/expected"/>
+    <untar src="../../../etc/testcases/taskdefs/expected/asf-logo.gif.tar.gz"
+           compression="gzip"
+           dest="${output}/actual"/>
+    <au:assertFilesMatch expected="${output}/expected/asf-logo.gif"
+                         actual="${output}/actual/asf-logo.gif"/>
+  </target>
+
+  <target name="testBzip2Compression" depends="setUp">
+    <untar src="../../../etc/testcases/taskdefs/expected/asf-logo.gif.tar"
+           dest="${output}/expected"/>
+    <untar src="../../../etc/testcases/taskdefs/expected/asf-logo.gif.tar.bz2"
+           compression="bzip2"
+           dest="${output}/actual"/>
+    <au:assertFilesMatch expected="${output}/expected/asf-logo.gif"
+                         actual="${output}/actual/asf-logo.gif"/>
+  </target>
+
+  <target name="testXZCompression" depends="setUp" if="xz.present">
+    <untar src="../../../etc/testcases/taskdefs/expected/asf-logo.gif.tar"
+           dest="${output}/expected"/>
+    <xz destfile="${input}/asf-logo.gif.tar.xz"
+        src="../../../etc/testcases/taskdefs/expected/asf-logo.gif.tar"/>
+    <untar src="${input}/asf-logo.gif.tar.xz"
+           compression="xz"
+           dest="${output}/actual"/>
+    <au:assertFilesMatch expected="${output}/expected/asf-logo.gif"
+                         actual="${output}/actual/asf-logo.gif"/>
+  </target>
+</project>