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: Ant, delete from zip, unzip
You can comment below, or link to this permanent URL from your own site.
15. Oktober 2008 um 09:46
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.
15. Oktober 2008 um 10:06
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
24. Juli 2009 um 13:35
Great macro !
31. August 2009 um 12:02
[...] Ant: Deleting files from ZIP « Jan’s Blog (tags: ant zip delete files macro) [...]
23. September 2009 um 10:36
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.
24. Oktober 2009 um 18:55
Yeah, the zipfileset makes it easier: http://xmedeko.blogspot.com/2009/09/ant-removing-file-from-zip-or-jar.html
3. Januar 2013 um 09:10
It is a wonderful macro! This is why I googled a lot! Thank you very much!