Posted tagged ‘delete from zip’

Ant: Deleting files from ZIP

14. Oktober 2008

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.*"/>