Ant: Deleting files from ZIP

A common problem is deleting single files from a ZIP. An old workaround is „unzip+zip“. This snippet implements a macro. The <selectors/> statement in line 21 together with the <element> definition in line 14 is the placeholder for all selectors you pass in.

<project default="test">

    <target name="prepare">
        <mkdir dir="zip"/>
        <echo file="zip/one.txt"   message="one"/>
        <echo file="zip/two.txt"   message="two"/>
        <echo file="zip/three.txt" message="three"/>
        <zip destfile="zip.zip" basedir="zip"/>
        <delete dir="zip"/>
    </target>

    <macrodef name="delFromZip">
        <attribute name="zip"/>
        <element   name="selectors" implicit="true" optional="true"/>
        <sequential>
            <mkdir dir="__tmp__"/>
            <unzip dest="__tmp__" src="@{zip}"/>
            <delete file="@{zip}"/>
            <delete>
                <fileset dir="__tmp__">
                    <selectors/>
                </fileset>
            </delete>
            <zip destfile="@{zip}" basedir="__tmp__"/>
            <delete dir="__tmp__"/>
        </sequential>
    </macrodef>

    <target name="test">
        <delFromZip zip="zip.zip">
            <include name="two.*"/> 
        </delFromZip>
    </target>

</project>
Explore posts in the same categories: Ant, en

Schlagwörter: , ,

You can comment below, or link to this permanent URL from your own site.

7 Kommentare - “Ant: Deleting files from ZIP”

  1. Odi Says:

    That’s what I hate about Ant: It takes away most the power that you have with a bash script. It’s one the worst scripting languages ever.

  2. janmaterne Says:

    Thats why Ant was never designed to be scripting language. If you want the power of scripting language together with the functionality of Ant, you could use Groovys AntBuilder: http://groovy.codehaus.org/Using+Ant+from+Groovy

  3. Jeremy Says:

    Great macro !


  4. […] Ant: Deleting files from ZIP « Jan’s Blog (tags: ant zip delete files macro) […]

  5. Frank Says:

    Have a look at http://www.nabble.com/RE%3A-Remove-entru-from-ZIP-file-using-ANT-p15433222.html. The trick used there is to use a zipfileset as input for the zip task. That should avoid some temporary files.

  6. Peter Horvath Says:

    It is a wonderful macro! This is why I googled a lot! Thank you very much!


Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s


%d Bloggern gefällt das: