File basedir = scanner.getBasedir();
for (int j = 0; j < files.length; j++) {
log("Adding file " + files[j] + " to fileset",
Project.MSG_VERBOSE);
ZipFileSet zf = new ZipFileSet();
zf.setSrc(new File(basedir, files[j]));
filesets.addElement(zf);
filesetsFromGroupfilesets.addElement(zf);
}
}
// Create the scanners to pass to isUpToDate().
Vector dss = new Vector();
if (baseDir != null) {
dss.addElement(getDirectoryScanner(baseDir));
}
for (int i = 0; i < filesets.size(); i++) {
FileSet fs = (FileSet) filesets.elementAt(i);
dss.addElement (fs.getDirectoryScanner(project));
}
int dssSize = dss.size();
FileScanner[] scanners = new FileScanner[dssSize];
dss.copyInto(scanners);
boolean success = false;
try {
// quick exit if the target is up to date
// can also handle empty archives
if (isUpToDate(scanners, zipFile)) {
return;
}
if (doUpdate) {
FileUtils fileUtils = FileUtils.newFileUtils();
renamedFile =
fileUtils.createTempFile("zip", ".tmp",
fileUtils.getParentFile(zipFile));
try {
if (!zipFile.renameTo(renamedFile)) {
throw new BuildException("Unable to rename old file "
+ "to temporary file");
}
} catch (SecurityException e) {
throw new BuildException("Not allowed to rename old file "
+ "to temporary file");
}
}
String action = doUpdate ? "Updating " : "Building ";
log(action + archiveType + ": " + zipFile.getAbsolutePath());
ZipOutputStream zOut =
new ZipOutputStream(new FileOutputStream(zipFile));
zOut.setEncoding(encoding);
try {
if (doCompress) {
zOut.setMethod(ZipOutputStream.DEFLATED);
} else {
zOut.setMethod(ZipOutputStream.STORED);
}
initZipOutputStream(zOut);
// Add the implicit fileset to the archive.
if (baseDir != null) {
addFiles(getDirectoryScanner(baseDir), zOut, "", "");
}
// Add the explicit filesets to the archive.
addFiles(filesets, zOut);
if (doUpdate) {
addingNewFiles = false;
ZipFileSet oldFiles = new ZipFileSet();
oldFiles.setSrc(renamedFile);
for (int i = 0; i < addedFiles.size(); i++) {
PatternSet.NameEntry ne = oldFiles.createExclude();
ne.setName((String) addedFiles.elementAt(i));
}
Vector tmp = new Vector(1);
tmp.addElement(oldFiles);
addFiles(tmp, zOut);