@@ -1,7 +1,7 @@
/*
/*
* The Apache Software License, Version 1.1
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
* reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* Redistribution and use in source and binary forms, with or without
@@ -9,7 +9,7 @@
* are met:
* are met:
*
*
* 1. Redistributions of source code must retain the above copyright
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer.
*
*
* 2. Redistributions in binary form must reproduce the above copyright
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* notice, this list of conditions and the following disclaimer in
@@ -17,15 +17,15 @@
* distribution.
* distribution.
*
*
* 3. The end-user documentation included with the redistribution, if
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
* if and wherever such third-party acknowlegements normally appear.
*
*
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
* permission, please contact apache@apache.org.
*
*
* 5. Products derived from this software may not be called "Apache"
* 5. Products derived from this software may not be called "Apache"
@@ -57,25 +57,41 @@ package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.PatternSet;
import java.io.BufferedWriter;
import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.IOException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Vector;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipEntry;
/**
/**
* Unzip a file.
* Unzip a file.
*
*
* @author costin@dnt.ro
* @author costin@dnt.ro
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
*/
*/
public class Expand extends MatchingTask {
public class Expand extends MatchingTask {
private File dest; // req
private File source; // req
protected File dest;
protected File source; // req
protected File outFile;
protected boolean overwrite = true;
protected boolean verbose;
protected PrintWriter pw = null;
protected BufferedWriter bw = null;
protected FileWriter fw = null;
protected Vector patternsets = new Vector();
protected Vector filesets = new Vector();
private boolean overwrite = true;
/**
/**
* Do the work.
* Do the work.
*
*
@@ -86,84 +102,118 @@ public class Expand extends MatchingTask {
if ("expand".equals(taskType)) {
if ("expand".equals(taskType)) {
log("!! expand is deprecated. Use unzip instead. !!");
log("!! expand is deprecated. Use unzip instead. !!");
}
}
if (source == null && filesets.size() == 0) {
throw new BuildException("src attribute and/or filesets must be specified");
}
if (dest == null && outFile == null) {
throw new BuildException(
"Dest and/or the OutFile attribute " +
"must be specified");
}
if (dest != null && dest.exists() && !dest.isDirectory()) {
throw new BuildException("Dest must be a directory.", location);
}
if (verbose && outFile == null) {
throw new BuildException(
"Verbose can be set only when OutFile is " +
"specified");
}
Touch touch = (Touch) project.createTask("touch");
Touch touch = (Touch) project.createTask("touch");
touch.setOwningTarget(target);
touch.setOwningTarget(target);
touch.setTaskName(getTaskName());
touch.setTaskName(getTaskName());
touch.setLocation(getLocation());
touch.setLocation(getLocation());
if (source == null) {
throw new BuildException("Source attribute must be specified");
try {
if (outFile != null) {
if (outFile.isDirectory()) {
throw new BuildException("Outfile " + outFile
+ " must not be a directory.");
}
if (!outFile.exists()) {
File parent = new File(outFile.getParent());
if (!parent.exists()) {
if (!parent.mkdirs()) {
throw new BuildException("Unable to create "
+ outFile);
}
}
}
fw = new FileWriter(outFile);
bw = new BufferedWriter(fw);
pw = new PrintWriter(bw, true);
}
} catch (IOException ioe) {
throw new BuildException(ioe.getMessage(), location);
}
}
if (dest == null) {
throw new BuildException("Dest attribute must be specified");
if (source != null) {
if (source.isDirectory()) {
// get all the files in the descriptor directory
DirectoryScanner ds = super.getDirectoryScanner(source);
String[] files = ds.getIncludedFiles();
for (int i = 0; i < files.length; ++i) {
File file = new File(source, files[i]);
expandFile(touch, file, dest);
}
}
else {
expandFile(touch, source, dest);
}
}
}
if (filesets.size() > 0) {
for (int j=0; j < filesets.size(); j++) {
FileSet fs = (FileSet) filesets.elementAt(j);
DirectoryScanner ds = fs.getDirectoryScanner(project);
File fromDir = fs.getDir(project);
if (source.isDirectory()) {
// get all the files in the descriptor directory
DirectoryScanner ds = super.getDirectoryScanner(source);
String[] files = ds.getIncludedFiles();
for (int i = 0; i < files.length; ++i) {
File file = new File(source, files[i]);
expandFile(touch, file, dest);
String[] files = ds.getIncludedFiles();
for (int i = 0; i < files.length; ++i) {
File file = new File(fromDir, files[i]);
expandFile(touch, file, dest);
}
}
}
}
}
else {
expandFile(touch, source, dest);
try {
if (pw != null) {
pw.close();
}
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException ioe1) {
//Oh, well! We did our best
}
}
}
}
private void expandFile(Touch touch, File srcF, File dir) {
/*
* This method is to be overridden by extending unarchival tasks.
*/
protected void expandFile(Touch touch, File srcF, File dir) {
ZipInputStream zis = null;
ZipInputStream zis = null;
try {
try {
log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
// code from WarExpand
// code from WarExpand
zis = new ZipInputStream(new FileInputStream(srcF));
zis = new ZipInputStream(new FileInputStream(srcF));
ZipEntry ze = null;
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
while ((ze = zis.getNextEntry()) != null) {
File f = new File(dir, project.translatePath(ze.getName()));
try {
if (!overwrite && f.exists()
&& f.lastModified() >= ze.getTime()) {
log("Skipping " + f + " as it is up-to-date",
Project.MSG_DEBUG);
continue;
}
log("expanding " + ze.getName() + " to "+ f,
Project.MSG_VERBOSE);
// create intermediary directories - sometimes zip don't add them
File dirF=new File(f.getParent());
dirF.mkdirs();
if (ze.isDirectory()) {
f.mkdirs();
} else {
byte[] buffer = new byte[1024];
int length = 0;
FileOutputStream fos = new FileOutputStream(f);
while ((length = zis.read(buffer)) >= 0) {
fos.write(buffer, 0, length);
}
fos.close();
}
if (project.getJavaVersion() != Project.JAVA_1_1) {
touch.setFile(f);
touch.setMillis(ze.getTime());
touch.touch();
}
} catch( FileNotFoundException ex ) {
log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
}
extractFile(touch, srcF, dir, zis,
ze.getName(), ze.getSize(),
new Date(ze.getTime()),
ze.isDirectory());
}
}
log("expand complete", Project.MSG_VERBOSE );
if (dest != null) {
log("expand complete", Project.MSG_VERBOSE );
}
} catch (IOException ioe) {
} catch (IOException ioe) {
throw new BuildException("Error while expanding " + srcF.getPath(), ioe);
throw new BuildException("Error while expanding " + srcF.getPath(), ioe);
} finally {
} finally {
@@ -175,7 +225,131 @@ public class Expand extends MatchingTask {
}
}
}
}
}
}
protected void extractFile(Touch touch, File srcF, File dir,
InputStream compressedInputStream,
String entryName, long entrySize,
Date entryDate, boolean isDirectory)
throws IOException {
extractFile(touch, srcF, dir, compressedInputStream,
entryName, entrySize, entryDate, isDirectory,
null, null);
}
protected void extractFile(Touch touch, File srcF, File dir,
InputStream compressedInputStream,
String entryName, long entrySize,
Date entryDate, boolean isDirectory,
String modeStr, String userGroup)
throws IOException {
if (patternsets != null && patternsets.size() > 0) {
String name = entryName;
boolean included = false;
for (int v = 0; v < patternsets.size(); v++) {
PatternSet p = (PatternSet) patternsets.elementAt(v);
String[] incls = p.getIncludePatterns(project);
if (incls != null) {
for (int w = 0; w < incls.length; w++) {
boolean isIncl = DirectoryScanner.match(incls[w], name);
if (isIncl) {
included = true;
break;
}
}
}
String[] excls = p.getExcludePatterns(project);
if (excls != null) {
for (int w = 0; w < excls.length; w++) {
boolean isExcl = DirectoryScanner.match(excls[w], name);
if (isExcl) {
included = false;
break;
}
}
}
}
if (!included) {
//Do not process this file
return;
}
}
if (dest != null) {
log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
}
if (outFile != null) {
if (verbose) {
StringBuffer sb = new StringBuffer();
if (modeStr != null) {
sb.append(modeStr);
sb.append(' ');
}
if (userGroup != null) {
sb.append(userGroup);
sb.append(' ');
}
String s = Long.toString(entrySize);
int len = s.length();
for(int i = 6 - len; i > 0; i--) {
sb.append(' ');
}
sb.append(s)
.append(' ')
.append(entryDate.toString());
sb.append(' ')
.append(entryName);
pw.println(sb);
} else {
pw.println(entryName);
}
}
if (dest != null) {
File f = new File(dir, project.translatePath(entryName));
try {
if (!overwrite && f.exists()
&& f.lastModified() >= entryDate.getTime()) {
log("Skipping " + f + " as it is up-to-date",
Project.MSG_DEBUG);
return;
}
log("expanding " + entryName + " to "+ f,
Project.MSG_VERBOSE);
// create intermediary directories - sometimes zip don't add them
File dirF=new File(f.getParent());
dirF.mkdirs();
if (isDirectory) {
f.mkdirs();
} else {
byte[] buffer = new byte[1024];
int length = 0;
FileOutputStream fos = new FileOutputStream(f);
while ((length =
compressedInputStream.read(buffer)) >= 0) {
fos.write(buffer, 0, length);
}
fos.close();
}
if (project.getJavaVersion() != Project.JAVA_1_1) {
touch.setFile(f);
touch.setMillis(entryDate.getTime());
touch.touch();
}
} catch( FileNotFoundException ex ) {
log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
}
}
}
/**
/**
* Set the destination directory. File will be unzipped into the
* Set the destination directory. File will be unzipped into the
* destination directory.
* destination directory.
@@ -203,4 +377,34 @@ public class Expand extends MatchingTask {
overwrite = b;
overwrite = b;
}
}
/**
* Set the output file to be used to store the list of the
* archive's contents.
*
* @param outFile the output file to be used.
*/
public void setOutfile(File outFile) {
this.outFile = outFile;
}
/**
* Set the verbose mode for the contents-list file.
*/
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
/**
* Add a patternset
*/
public void addPatternset(PatternSet set) {
patternsets.addElement(set);
}
/**
* Add a fileset
*/
public void addFileset(FileSet set) {
filesets.addElement(set);
}
}
}