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 My.zip contains:

dir1/path/to/file1.html
dir1/path/to/file2.jpg
dir1/another/path/file3.txt
dir2/some/path/to/file4.png
dir2/file5.doc


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 :

<project>

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

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

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

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

</project>

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.