Ant: Zip the contents of a directory, without the top-level directory itself

antzip

How can I use Ant to create zip containing all the files and nested directories inside some root directory, but exclude the top-level directory itself from the zip.

For example, say I have this file structure:

/foo
    splat.js
    /bar
        wheee.css

I want the zip to contain splat, and wheee inside /bar, but I don't want all that to be contained inside a 'foo' directory. In other words, unzipping this into /honk should not create a foo directory; splat and bar should end up at the root of /honk.

I'm currently using this (extraneous details removed):

<zip destfile="${zipfile}" basedir="" includes="${distRoot}/**/*.*" />

What kind of fileset select can replace that 'includes' spec to achieve this?

Best Answer

It's not dynamic, but using the fullpath attribute allows you to define the path struture of the zip file. See ant documentation: http://ant.apache.org/manual/Types/zipfileset.html

<zip destfile="YourZipName.zip">
    <zipfileset fullpath="splat.js" dir="foo" includes="splat.js"/>
    <zipfileset fullpath="bar/wheee.css" dir="foo/bar" includes="wheee.css"/>
</zip>

This may get you want you want dynamically. It should pull in everything in the foo dir then zip it up. It's just a few extra steps.

<mkdir dir="DirToBeZipped"/>
<copy todir="DirToBeZipped">
    <fileset dir="foo" includes="*"/>
</copy>
<zip destfile="YourZipName.zip" basedir="DirToBeZipped"/>