From 14ad5c43cd9daa609aae8a4849362d18b8b69f0a Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 11 Jan 2017 22:20:16 +0100 Subject: [PATCH] xz support in untar - PR 60350 --- .../org/apache/tools/ant/taskdefs/Untar.java | 32 +++++++++- src/tests/antunit/taskdefs/untar-test.xml | 61 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/tests/antunit/taskdefs/untar-test.xml diff --git a/src/main/org/apache/tools/ant/taskdefs/Untar.java b/src/main/org/apache/tools/ant/taskdefs/Untar.java index 8343aec52..8e841c006 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 clazz = + Class.forName("org.tukaani.xz.XZInputStream") + .asSubclass(InputStream.class); + Constructor 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); + } + } } } diff --git a/src/tests/antunit/taskdefs/untar-test.xml b/src/tests/antunit/taskdefs/untar-test.xml new file mode 100644 index 000000000..4c04bfe8b --- /dev/null +++ b/src/tests/antunit/taskdefs/untar-test.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +