Browse Source

xz support in untar - PR 60350

master
Stefan Bodewig 8 years ago
parent
commit
14ad5c43cd
2 changed files with 92 additions and 1 deletions
  1. +31
    -1
      src/main/org/apache/tools/ant/taskdefs/Untar.java
  2. +61
    -0
      src/tests/antunit/taskdefs/untar-test.xml

+ 31
- 1
src/main/org/apache/tools/ant/taskdefs/Untar.java View File

@@ -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);
}
}
}
}

+ 61
- 0
src/tests/antunit/taskdefs/untar-test.xml View File

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

Loading…
Cancel
Save