Browse Source

update attribute for <zip> and friends - update archive instead of

creating a new one.

PR: 163
Submitted by:	Jon Skeet <jon.skeet@peramon.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269325 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
ec2d68aa71
2 changed files with 85 additions and 3 deletions
  1. +3
    -0
      WHATSNEW
  2. +82
    -3
      src/main/org/apache/tools/ant/taskdefs/Zip.java

+ 3
- 0
WHATSNEW View File

@@ -105,6 +105,9 @@ Other changes:
* New filesonly attribute for <zip> and friends to suppress directory * New filesonly attribute for <zip> and friends to suppress directory
entries. entries.


* New update attribute for <zip> and friends - update an existing
archive instead of creating a new one.

Fixed bugs: Fixed bugs:
----------- -----------




+ 82
- 3
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -78,6 +78,7 @@ public class Zip extends MatchingTask {
private File zipFile; private File zipFile;
private File baseDir; private File baseDir;
private boolean doCompress = true; private boolean doCompress = true;
private boolean doUpdate = true;
private boolean doFilesonly = false; private boolean doFilesonly = false;
protected String archiveType = "zip"; protected String archiveType = "zip";
// For directories: // For directories:
@@ -85,6 +86,7 @@ public class Zip extends MatchingTask {
protected String emptyBehavior = "skip"; protected String emptyBehavior = "skip";
private Vector filesets = new Vector (); private Vector filesets = new Vector ();
private Hashtable addedDirs = new Hashtable(); private Hashtable addedDirs = new Hashtable();
private Vector addedFiles = new Vector();


/** /**
* Encoding to use for filenames, defaults to the platform's * Encoding to use for filenames, defaults to the platform's
@@ -122,6 +124,14 @@ public class Zip extends MatchingTask {
doFilesonly = f; doFilesonly = f;
} }


/**
* Sets whether we want to update the file (if it exists)
* or create a new one.
*/
public void setUpdate(boolean c) {
doUpdate = c;
}

/** /**
* Adds a set of files (nested fileset attribute). * Adds a set of files (nested fileset attribute).
*/ */
@@ -179,6 +189,39 @@ public class Zip extends MatchingTask {
throw new BuildException("You must specify the " + archiveType + " file to create!"); throw new BuildException("You must specify the " + archiveType + " file to create!");
} }


// Renamed version of original file, if it exists
File renamedFile=null;
// Whether or not an actual update is required -
// we don't need to update if the original file doesn't exist
boolean reallyDoUpdate=false;
if (doUpdate && zipFile.exists())
{
reallyDoUpdate=true;
int i;
for (i=0; i < 1000; i++)
{
renamedFile = new File (zipFile.getParent(), "tmp."+i);
if (!renamedFile.exists())
break;
}
if (i==1000)
throw new BuildException
("Can't find temporary filename to rename old file to.");
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");
}
}
// Create the scanners to pass to isUpToDate(). // Create the scanners to pass to isUpToDate().
Vector dss = new Vector (); Vector dss = new Vector ();
if (baseDir != null) if (baseDir != null)
@@ -195,10 +238,12 @@ public class Zip extends MatchingTask {
// can also handle empty archives // can also handle empty archives
if (isUpToDate(scanners, zipFile)) return; if (isUpToDate(scanners, zipFile)) return;


log("Building "+ archiveType +": "+ zipFile.getAbsolutePath());
String action=reallyDoUpdate ? "Updating " : "Building ";
log(action + archiveType +": "+ zipFile.getAbsolutePath());


boolean success = false;
try { try {
boolean success = false;
ZipOutputStream zOut = ZipOutputStream zOut =
new ZipOutputStream(new FileOutputStream(zipFile)); new ZipOutputStream(new FileOutputStream(zipFile));
zOut.setEncoding(encoding); zOut.setEncoding(encoding);
@@ -215,6 +260,24 @@ public class Zip extends MatchingTask {
addFiles(getDirectoryScanner(baseDir), zOut, "", ""); addFiles(getDirectoryScanner(baseDir), zOut, "", "");
// Add the explicit filesets to the archive. // Add the explicit filesets to the archive.
addFiles(filesets, zOut); addFiles(filesets, zOut);
if (reallyDoUpdate)
{
ZipFileSet oldFiles = new ZipFileSet ();
oldFiles.setSrc (renamedFile);
StringBuffer exclusionPattern=new StringBuffer();
for (int i=0; i < addedFiles.size(); i++)
{
if (i != 0)
exclusionPattern.append (",");
exclusionPattern.append
((String) addedFiles.elementAt(i));
}
oldFiles.setExcludes (exclusionPattern.toString());
Vector tmp = new Vector();
tmp.addElement (oldFiles);
addFiles (tmp, zOut);
}
success = true; success = true;
} finally { } finally {
// Close the output stream. // Close the output stream.
@@ -240,10 +303,22 @@ public class Zip extends MatchingTask {
msg += " (and the archive is probably corrupt but I could not delete it)"; msg += " (and the archive is probably corrupt but I could not delete it)";
} }


if (reallyDoUpdate) {
if (!renamedFile.renameTo (zipFile)) {
msg+=" (and I couldn't rename the temporary file "+
renamedFile.getName()+" back)";
}
}
throw new BuildException(msg, ioe, location); throw new BuildException(msg, ioe, location);
} finally { } finally {
cleanUp(); cleanUp();
} }
// If we've been successful on an update, delete the temporary file
if (success && reallyDoUpdate)
if (!renamedFile.delete())
log ("Warning: unable to delete temporary file "+
renamedFile.getName(), Project.MSG_WARN);
} }


/** /**
@@ -514,6 +589,7 @@ public class Zip extends MatchingTask {
} }
count = in.read(buffer, 0, buffer.length); count = in.read(buffer, 0, buffer.length);
} while (count != -1); } while (count != -1);
addedFiles.addElement (vPath);
} }


protected void zipFile(File file, ZipOutputStream zOut, String vPath) protected void zipFile(File file, ZipOutputStream zOut, String vPath)
@@ -612,5 +688,8 @@ public class Zip extends MatchingTask {
* <p>When we get here, the Zip file has been closed and all we * <p>When we get here, the Zip file has been closed and all we
* need to do is to reset some globals.</p> * need to do is to reset some globals.</p>
*/ */
protected void cleanUp() {}
protected void cleanUp() {
addedDirs = new Hashtable();
addedFiles = new Vector();
}
} }

Loading…
Cancel
Save