Malvon Malvon - 2 months ago 10
Apache Configuration Question

How to Get a List of Files in ZIP via Apache Ant Task?

I need to get a list of file names in a ZIP file using Apache Ant task without unzipping it first. It also should be OS independent, e.g.: if contains:


The Ant task should return the list above with the relative path + filename.

Answer Source

A solution using zipfileset and pathconvert, wrapped in macrodef for reuse :


<macrodef name="listzipcontents">
 <attribute name="file"/>
 <attribute name="outputproperty"/>

  <zipfileset src="@{file}" id="content"/>
  <pathconvert property="@{outputproperty}" pathsep="${line.separator}">
   <zipfileset refid="content"/>
   <map from="@{file}:" to=""/>

  <listzipcontents file="path/to/|war|jar|ear" outputproperty="foobar"/>

  <echo>$${foobar} => ${foobar}</echo>


Advantage : you may use all fileset attributes, f.e. include/exclude if you need to filter zipfilecontents - simply expand macrodef with additional attributes for that, also zipfileset supports other archives like jar, war, ear.