Search code examples
antzipunzipmapper

How do I flatten the top level folder of a zip file with ant?


A lot of zip files have a root folder, how do I unpack the zip file and get rid of the root folder?

I know there is the globmapper:

<unzip dest="${dest.path}">
    <fileset dir="${source.path}">
        <include name="**/zipfile*.*.zip" />
    </fileset>
    <mapper>
        <globmapper from="rootFolder/*" to="*" />
    </mapper>
</unzip>

But what if I don't know the name of the root folder? Wildcards are not working e.g.

<globmapper from="root*Folder/*" to="*" />

Is there a way to use wildcards or a mapper/function that upacks without the root folder?


Solution

  • There's actually a separate mapper specifically made for this called cutdirsmapper. Give this a try:

    <unzip dest="${dest.path}">
        <fileset dir="${source.path}">
            <include name="**/zipfile*.*.zip" />
        </fileset>
        <cutdirsmapper dirs="1" />
    </unzip>