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="" basedir="zip"/>
        <delete dir="zip"/>

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

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

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:

  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 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!

Kommentar verfassen

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

Du kommentierst mit Deinem Abmelden /  Ändern )

Google Foto

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


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


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

Verbinde mit %s

%d Bloggern gefällt das: