umagesh 02/04/23 20:09:06
Modified: . WHATSNEW
docs/manual/CoreTasks tar.html unzip.html
src/etc/testcases/taskdefs tar.xml untar.xml
src/main/org/apache/tools/ant/taskdefs Tar.java Untar.java
src/main/org/apache/tools/bzip2 CBZip2InputStream.java
src/testcases/org/apache/tools/ant/taskdefs TarTest.java
UntarTest.java
Added: src/etc/testcases/taskdefs/expected asf-logo.gif.tar.bz2
asf-logo.gif.tar.gz
Log:
Implement and document a compression attribute for the tar and untar tasks.
Submitted by: Curt Arnold <[EMAIL PROTECTED]>
Revision Changes Path
1.258 +5 -0 jakarta-ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
retrieving revision 1.257
retrieving revision 1.258
diff -u -r1.257 -r1.258
--- WHATSNEW 22 Apr 2002 13:22:56 -0000 1.257
+++ WHATSNEW 24 Apr 2002 03:09:06 -0000 1.258
@@ -109,6 +109,11 @@
Other changes:
--------------
+* Gzip and Bzip2 files can now be constructed in the fly when using
+ the tar task without having to create the intermediate tar file on
+ disk. The Untar task can also untar GZip and BZip2 files on the fly
+ without creating the intermediate tar file.
+
* New optional type, <classfileset> added.
* <ejbjar> now allows control over which additional classes and interfaces
1.16 +9 -2 jakarta-ant/docs/manual/CoreTasks/tar.html
Index: tar.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/tar.html,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- tar.html 4 Feb 2002 21:50:42 -0000 1.15
+++ tar.html 24 Apr 2002 03:09:06 -0000 1.16
@@ -41,8 +41,8 @@
that it produces a warning for each file path encountered that does not match
the limit.</p>
-<p>Note that this task does not perform compression. You might want to use
the
-<a href="gzip.html">GZip</a> task to prepare a .tar.gz package.</p>
+<p>This task can perform compression by setting the compression attribute to
"gzip"
+or "bzip2".</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
@@ -97,6 +97,13 @@
<td valign="top">defaultexcludes</td>
<td valign="top">indicates whether default excludes should be used or not
("yes"/"no"). Default excludes are used when
omitted.</td>
+ <td valign="top" align="center">No</td>
+ </tr>
+ <tr>
+ <td valign="top">compression</td>
+ <td valign="top">compression method. Allowable values are
+ "none", "gzip" and "bzip2". Default is
+ "none".</td>
<td valign="top" align="center">No</td>
</tr>
</table>
1.9 +9 -1 jakarta-ant/docs/manual/CoreTasks/unzip.html
Index: unzip.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/unzip.html,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- unzip.html 3 Feb 2002 22:00:42 -0000 1.8
+++ unzip.html 24 Apr 2002 03:09:06 -0000 1.9
@@ -44,6 +44,14 @@
true).</td>
<td align="center" valign="top">No</td>
</tr>
+ <tr>
+ <td valign="top">compression</td>
+ <td valign="top">compression method for untar. Allowable values are
+ "none", "gzip" and "bzip2". Default is
+ "none".</td>
+ <td valign="top" align="center">No</td>
+ </tr>
+
</table>
<h3>Examples</h3>
<blockquote>
@@ -82,7 +90,7 @@
</pre></p>
</blockquote>
<hr>
-<p align="center">Copyright © 2001 Apache Software Foundation. All
rights
+<p align="center">Copyright © 2001-2002 Apache Software Foundation. All
rights
Reserved.</p>
</body>
1.10 +35 -0 jakarta-ant/src/etc/testcases/taskdefs/tar.xml
Index: tar.xml
===================================================================
RCS file: /home/cvs/jakarta-ant/src/etc/testcases/taskdefs/tar.xml,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- tar.xml 4 Feb 2002 20:44:16 -0000 1.9
+++ tar.xml 24 Apr 2002 03:09:06 -0000 1.10
@@ -53,6 +53,28 @@
<untar src="test8.tar" dest="."/>
</target>
+ <target name="test9">
+ <tar destfile="blah" compression="Foo"/>
+ </target>
+
+ <target name="test10">
+ <tar destfile="test10.tar.gz" compression="gzip">
+ <tarfileset dir="." fullpath="/test10.xml">
+ <include name="tar.xml"/>
+ </tarfileset>
+ </tar>
+ <untar src="test10.tar.gz" dest="." compression="gzip"/>
+ </target>
+
+ <target name="test11">
+ <tar destfile="test11.tar.bz2" compression="bzip2">
+ <tarfileset dir="." fullpath="/test11.xml">
+ <include name="tar.xml"/>
+ </tarfileset>
+ </tar>
+ <untar src="test11.tar.bz2" dest="." compression="bzip2"/>
+ </target>
+
<target name="cleanup">
<delete file="test4.tar"/>
<delete file="test5.tar"/>
@@ -63,12 +85,25 @@
<delete file="test7.tar"/>
<delete file="test8.tar"/>
<delete file="test8.xml"/>
+ <delete file="test10.tar.gz"/>
+ <delete file="test10.xml"/>
+ <delete file="test11.tar.bz2"/>
+ <delete file="test11.xml"/>
</target>
<target name="feather">
<tar destfile="asf-logo.gif.tar"
basedir=".."
includes="asf-logo.gif" />
+ <tar destfile="asf-logo.gif.tar.gz"
+ basedir=".."
+ includes="asf-logo.gif"
+ compression="gzip"/>
+ <tar destfile="asf-logo.gif.tar.bz2"
+ basedir=".."
+ includes="asf-logo.gif"
+ compression="bzip2" />
</target>
+
</project>
1.3 +21 -0 jakarta-ant/src/etc/testcases/taskdefs/untar.xml
Index: untar.xml
===================================================================
RCS file: /home/cvs/jakarta-ant/src/etc/testcases/taskdefs/untar.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- untar.xml 21 Nov 2001 22:36:12 -0000 1.2
+++ untar.xml 24 Apr 2002 03:09:06 -0000 1.3
@@ -12,9 +12,30 @@
<ant antfile="tar.xml" target="cleanup" />
</target>
+ <target name="testGzipTarTask">
+ <ant antfile="tar.xml" target="feather" />
+ <untar src="asf-logo.gif.tar.gz" dest="." compression="gzip" />
+ <ant antfile="tar.xml" target="cleanup" />
+ </target>
+
+ <target name="testBzip2TarTask">
+ <ant antfile="tar.xml" target="feather" />
+ <untar src="asf-logo.gif.tar.bz2" dest="." compression="bzip2"/>
+ <ant antfile="tar.xml" target="cleanup" />
+ </target>
+
<target name="realTest">
<untar src="expected/asf-logo.gif.tar" dest="." />
</target>
+
+ <target name="realGzipTest">
+ <untar src="expected/asf-logo.gif.tar.gz" dest="." compression="gzip" />
+ </target>
+
+ <target name="realBzip2Test">
+ <untar src="expected/asf-logo.gif.tar.bz2" dest="." compression="bzip2"/>
+ </target>
+
<target name="srcDirTest">
<untar src="." dest="." />
1.1
jakarta-ant/src/etc/testcases/taskdefs/expected/asf-logo.gif.tar.bz2
<<Binary file>>
1.1
jakarta-ant/src/etc/testcases/taskdefs/expected/asf-logo.gif.tar.gz
<<Binary file>>
1.32 +97 -13
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Tar.java
Index: Tar.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Tar.java,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -r1.31 -r1.32
--- Tar.java 15 Apr 2002 15:33:09 -0000 1.31
+++ Tar.java 24 Apr 2002 03:09:06 -0000 1.32
@@ -57,7 +57,9 @@
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
+import java.io.OutputStream;
import java.io.FileInputStream;
+import java.io.BufferedOutputStream;
import java.util.Vector;
import java.util.Enumeration;
import org.apache.tools.ant.BuildException;
@@ -70,11 +72,15 @@
import org.apache.tools.tar.TarOutputStream;
import org.apache.tools.tar.TarConstants;
import org.apache.tools.tar.TarEntry;
+import java.util.zip.GZIPOutputStream;
+import org.apache.tools.bzip2.CBZip2OutputStream;
+
+
/**
* Creates a TAR archive.
*
- * @author Stefano Mazzocchi
+ * @author Stefano Mazzocchi
* <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Stefan Bodewig</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Magesh Umasankar</a>
@@ -125,6 +131,8 @@
*/
private boolean longWarningGiven = false;
+ private TarCompressionMethod compression = new TarCompressionMethod();
+
public TarFileSet createTarFileSet() {
TarFileSet fileset = new TarFileSet();
filesets.addElement(fileset);
@@ -191,6 +199,18 @@
this.longFileMode = mode;
}
+ /**
+ * Set compression method.
+ *
+ * Allowable values are
+ * none - no compression
+ * gzip - Gzip compression
+ * bzip2 - Bzip2 compression
+ */
+ public void setCompression(TarCompressionMethod mode) {
+ this.compression = mode;
+ }
+
public void execute() throws BuildException {
if (tarFile == null) {
throw new BuildException("tarfile attribute must be set!",
@@ -211,7 +231,7 @@
try {
if (baseDir != null) {
if (!baseDir.exists()) {
- throw new BuildException("basedir does not exist!",
+ throw new BuildException("basedir does not exist!",
location);
}
@@ -226,7 +246,7 @@
+ "attribute or some nested
filesets.",
location);
}
-
+
// check if tar is out of date with respect to each
// fileset
boolean upToDate = true;
@@ -239,7 +259,7 @@
}
for (int i = 0; i < files.length; ++i) {
- if (tarFile.equals(new File(fs.getDir(project),
+ if (tarFile.equals(new File(fs.getDir(project),
files[i]))) {
throw new BuildException("A tar file cannot include "
+ "itself", location);
@@ -257,7 +277,10 @@
TarOutputStream tOut = null;
try {
- tOut = new TarOutputStream(new FileOutputStream(tarFile));
+ tOut = new TarOutputStream(
+ compression.compress(
+ new BufferedOutputStream(
+ new FileOutputStream(tarFile))));
tOut.setDebug(true);
if (longFileMode.isTruncateMode()) {
tOut.setLongFileMode(TarOutputStream.LONGFILE_TRUNCATE);
@@ -270,13 +293,13 @@
}
longWarningGiven = false;
- for (Enumeration e = filesets.elements();
+ for (Enumeration e = filesets.elements();
e.hasMoreElements();) {
TarFileSet fs = (TarFileSet) e.nextElement();
String[] files = fs.getFiles(project);
if (files.length > 1 && fs.getFullpath().length() > 0) {
throw new BuildException("fullpath attribute may
only "
- + "be specified for "
+ + "be specified for "
+ "filesets that specify a "
+ "single file.");
}
@@ -315,11 +338,11 @@
if (vPath.length() <= 0) {
return;
}
-
+
if (file.isDirectory() && !vPath.endsWith("/")) {
vPath += "/";
}
-
+
String prefix = tarFileSet.getPrefix();
// '/' is appended for compatibility with the zip task.
if (prefix.length() > 0 && !prefix.endsWith("/")) {
@@ -333,7 +356,7 @@
if (l <= 1) {
// we would end up adding "" to the archive
return;
- }
+ }
vPath = vPath.substring(1, l);
}
@@ -344,11 +367,11 @@
return;
} else if (longFileMode.isWarnMode()) {
log("Entry: " + vPath + " longer than " +
- TarConstants.NAMELEN + " characters.",
+ TarConstants.NAMELEN + " characters.",
Project.MSG_WARN);
if (!longWarningGiven) {
log("Resulting tar file can only be processed "
- + "successfully by GNU compatible tar commands",
+ + "successfully by GNU compatible tar commands",
Project.MSG_WARN);
longWarningGiven = true;
}
@@ -406,7 +429,7 @@
private String prefix = "";
private String fullpath = "";
private boolean preserveLeadingSlashes = false;
-
+
public TarFileSet(FileSet fileset) {
super(fileset);
}
@@ -526,6 +549,67 @@
public boolean isOmitMode() {
return OMIT.equalsIgnoreCase(getValue());
+ }
+ }
+
+ /**
+ * Valid Modes for Compression attribute to Tar Task
+ *
+ */
+ public static final class TarCompressionMethod extends
EnumeratedAttribute {
+
+ // permissable values for compression attribute
+ /**
+ * No compression
+ */
+ private static final String NONE = "none";
+ /**
+ * GZIP compression
+ */
+ private static final String GZIP = "gzip";
+ /**
+ * BZIP2 compression
+ */
+ private static final String BZIP2 = "bzip2";
+
+
+ /**
+ * Default constructor
+ */
+ public TarCompressionMethod() {
+ super();
+ setValue(NONE);
+ }
+
+ /**
+ * Get valid enumeration values.
+ * @return valid enumeration values
+ */
+ public String[] getValues() {
+ return new String[] { NONE, GZIP, BZIP2 };
+ }
+
+ /**
+ * This method wraps the output stream with the
+ * corresponding compression method
+ *
+ * @param ostream output stream
+ * @return output stream with on-the-fly compression
+ * @exception IOException thrown if file is not writable
+ */
+ private OutputStream compress(final OutputStream ostream)
+ throws IOException {
+ final String value = getValue();
+ if (GZIP.equals(value)) {
+ return new GZIPOutputStream(ostream);
+ } else {
+ if (BZIP2.equals(value)) {
+ ostream.write('B');
+ ostream.write('Z');
+ return new CBZip2OutputStream(ostream);
+ }
+ }
+ return ostream;
}
}
}
1.26 +100 -2
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Untar.java
Index: Untar.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Untar.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- Untar.java 15 Apr 2002 13:36:17 -0000 1.25
+++ Untar.java 24 Apr 2002 03:09:06 -0000 1.26
@@ -61,7 +61,13 @@
import org.apache.tools.ant.util.FileUtils;
import java.io.File;
import java.io.FileInputStream;
+import java.io.BufferedInputStream;
+import java.io.InputStream;
import java.io.IOException;
+import java.util.zip.GZIPInputStream;
+import org.apache.tools.bzip2.CBZip2InputStream;
+import org.apache.tools.ant.types.EnumeratedAttribute;
+
/**
@@ -75,13 +81,33 @@
* @ant.task category="packaging"
*/
public class Untar extends Expand {
+ /**
+ * compression method
+ */
+ private UntarCompressionMethod compression = new
UntarCompressionMethod();
+
+ /**
+ * Set compression method.
+ *
+ * Allowable values are
+ * none - no compression
+ * gzip - Gzip compression
+ * bzip2 - Bzip2 compression
+ *
+ * @param method compression method
+ */
+ public void setCompression(UntarCompressionMethod method) {
+ compression = method;
+ }
protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
TarInputStream tis = null;
try {
log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
-
- tis = new TarInputStream(new FileInputStream(srcF));
+ tis = new TarInputStream(
+ compression.decompress(srcF,
+ new BufferedInputStream(
+ new FileInputStream(srcF))));
TarEntry te = null;
while ((te = tis.getNextEntry()) != null) {
@@ -99,6 +125,78 @@
tis.close();
} catch (IOException e) {}
}
+ }
+ }
+
+ /**
+ * Valid Modes for Compression attribute to Untar Task
+ *
+ */
+ public static final class UntarCompressionMethod
+ extends EnumeratedAttribute {
+
+ // permissable values for compression attribute
+ /**
+ * No compression
+ */
+ private static final String NONE = "none";
+ /**
+ * GZIP compression
+ */
+ private static final String GZIP = "gzip";
+ /**
+ * BZIP2 compression
+ */
+ private static final String BZIP2 = "bzip2";
+
+
+ /**
+ * Constructor
+ */
+ public UntarCompressionMethod() {
+ super();
+ setValue(NONE);
+ }
+
+ /**
+ * Get valid enumeration values
+ *
+ * @return valid values
+ */
+ public String[] getValues() {
+ return new String[] { NONE, GZIP, BZIP2 };
+ }
+
+ /**
+ * This method wraps the input stream with the
+ * corresponding decompression method
+ *
+ * @param file provides location information for BuildException
+ * @param istream input stream
+ * @return input stream with on-the-fly decompression
+ * @exception IOException thrown by GZIPInputStream constructor
+ * @exception BuildException thrown if bzip stream does not
+ * start with expected magic values
+ */
+ private InputStream decompress(final File file,
+ final InputStream istream)
+ throws IOException, BuildException {
+ final String value = getValue();
+ if (GZIP.equals(value)) {
+ return new GZIPInputStream(istream);
+ } else {
+ if (BZIP2.equals(value)) {
+ final char[] magic = new char[] { 'B', 'Z' };
+ for (int i = 0; i < magic.length; i++) {
+ if (istream.read() != magic[i]) {
+ throw new BuildException(
+ "Invalid bz2 file." + file.toString());
+ }
+ }
+ return new CBZip2InputStream(istream);
+ }
+ }
+ return istream;
}
}
}
1.11 +9 -1
jakarta-ant/src/main/org/apache/tools/bzip2/CBZip2InputStream.java
Index: CBZip2InputStream.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/bzip2/CBZip2InputStream.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- CBZip2InputStream.java 15 Apr 2002 14:56:34 -0000 1.10
+++ CBZip2InputStream.java 24 Apr 2002 03:09:06 -0000 1.11
@@ -298,7 +298,15 @@
}
private void bsFinishedWithStream() {
- bsStream = null;
+ try {
+ if (this.bsStream != null) {
+ if (this.bsStream != System.in) {
+ this.bsStream.close();
+ this.bsStream= null;
+ }
+ }
+ } catch (IOException ioe) {
+ }
}
private void bsSetStream(InputStream f) {
1.10 +23 -0
jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java
Index: TarTest.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- TarTest.java 4 Feb 2002 20:44:16 -0000 1.9
+++ TarTest.java 24 Apr 2002 03:09:06 -0000 1.10
@@ -124,6 +124,29 @@
}
}
+ public void test9() {
+ expectBuildException("test9", "Invalid value specified for
compression attribute.");
+ }
+
+ public void test10() {
+ executeTarget("test10");
+ java.io.File f1
+ = new java.io.File("src/etc/testcases/taskdefs/test10.xml");
+ if (! f1.exists()) {
+ fail("The fullpath attribute or the preserveLeadingSlashes
attribute does not work propertly");
+ }
+ }
+
+ public void test11() {
+ executeTarget("test11");
+ java.io.File f1
+ = new java.io.File("src/etc/testcases/taskdefs/test11.xml");
+ if (! f1.exists()) {
+ fail("The fullpath attribute or the preserveLeadingSlashes
attribute does not work propertly");
+ }
+ }
+
+
public void tearDown() {
executeTarget("cleanup");
}
1.3 +30 -2
jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/UntarTest.java
Index: UntarTest.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/UntarTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- UntarTest.java 21 Nov 2001 22:36:12 -0000 1.2
+++ UntarTest.java 24 Apr 2002 03:09:06 -0000 1.3
@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -59,7 +59,7 @@
/**
* @author <a href="mailto:[EMAIL PROTECTED]">Stefan Bodewig</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class UntarTest extends BuildFileTest {
@@ -82,9 +82,37 @@
project.resolveFile("asf-logo.gif")));
}
+ public void testRealGzipTest() throws java.io.IOException {
+ FileUtils fileUtils = FileUtils.newFileUtils();
+ executeTarget("realGzipTest");
+
assertTrue(fileUtils.contentEquals(project.resolveFile("../asf-logo.gif"),
+
project.resolveFile("asf-logo.gif")));
+ }
+
+ public void testRealBzip2Test() throws java.io.IOException {
+ FileUtils fileUtils = FileUtils.newFileUtils();
+ executeTarget("realBzip2Test");
+
assertTrue(fileUtils.contentEquals(project.resolveFile("../asf-logo.gif"),
+
project.resolveFile("asf-logo.gif")));
+ }
+
public void testTestTarTask() throws java.io.IOException {
FileUtils fileUtils = FileUtils.newFileUtils();
executeTarget("testTarTask");
+
assertTrue(fileUtils.contentEquals(project.resolveFile("../asf-logo.gif"),
+
project.resolveFile("asf-logo.gif")));
+ }
+
+ public void testTestGzipTarTask() throws java.io.IOException {
+ FileUtils fileUtils = FileUtils.newFileUtils();
+ executeTarget("testGzipTarTask");
+
assertTrue(fileUtils.contentEquals(project.resolveFile("../asf-logo.gif"),
+
project.resolveFile("asf-logo.gif")));
+ }
+
+ public void testTestBzip2TarTask() throws java.io.IOException {
+ FileUtils fileUtils = FileUtils.newFileUtils();
+ executeTarget("testBzip2TarTask");
assertTrue(fileUtils.contentEquals(project.resolveFile("../asf-logo.gif"),
project.resolveFile("asf-logo.gif")));
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>