ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cyril Sagan <>
Subject replace an entry in a zip file?
Date Thu, 14 Jun 2007 00:05:07 GMT

GOAL:  To replace one entry in a zip file with an updated file.

I cannot find an elegant way to do this, short of <unzip>'ing the
archive, replacing the file of interest and then re-<zip>.  It seems
there should be a better way.  Can anyone help?



FWIW, It looks like the Ant 1.6.5 source code *tried* to support this.
Note that the comment describes a the setting "overwrite" for the
duplicate attribute of <zip> task which would provide exactly what I
want, the bad news is that it doesn't seem to be implemented.  :-(

I've included the comment, and the duplicate logic below.

>From src/main/org/apache/tools/ant/taskdefs/

     * Sets behavior for when a duplicate file is about to be added -
     * one of <code>keep</code>, <code>skip</code> or <code>overwrite</code>.
     * Possible values are: <code>keep</code> (keep both
     * of the files); <code>skip</code> (keep the first version
     * of the file found); <code>overwrite</code> overwrite the file
     * with the new file
     * Default for zip tasks is <code>keep</code>
    public void setDuplicate(Duplicate df) {
        duplicate = df.getValue();

     * Adds a new entry to the archive, takes care of duplicates as well.
     * @param in the stream to read data for the entry from.
     * @param zOut the stream to write to.
     * @param vPath the name this entry shall have in the archive.
     * @param lastModified last modification time for the entry.
     * @param fromArchive the original archive we are copying this
     * entry from, will be null if we are not copying from an archive.
     * @param mode the Unix permissions to set.
     * @since Ant 1.5.2
    protected void zipFile(InputStream in, ZipOutputStream zOut, String vPath,
                           long lastModified, File fromArchive, int mode)
        throws IOException {
        if (entries.contains(vPath)) {

            if (duplicate.equals("preserve")) {
                log(vPath + " already added, skipping", Project.MSG_INFO);
            } else if (duplicate.equals("fail")) {
                throw new BuildException("Duplicate file " + vPath
                                         + " was found and the duplicate "
                                         + "attribute is 'fail'.");
            } else {
                // duplicate equal to add, so we continue
                log("duplicate file " + vPath
                    + " found, adding.", Project.MSG_VERBOSE);
        } else {
            log("adding entry " + vPath, Project.MSG_VERBOSE);

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message