King_V
King_V

Reputation: 285

Zip task in Ant - incuding empty and non-empty subdirectories

Alright, I've been going slightly insane with this one - what bugs me more is that I'm sure a few years back I'd done something similar to what I'm trying to do now.

Let me say right off the bat, I am using ANT version 1.8.2, so I don't have the 1.6.2 issues with zip and empty folders. Most of my searching seems to bring up the 1.6.2 issue.

So, here's my build.xml snippet:

<target name="zip" depends="jar">  
    <echo>Creating zip file...</echo>  
    <zip destfile="${dist}/${zipfilename}">  
        <zipfileset dir="${dist}" includes="${jarfilename}" />  
    </zip>  
    <echo>...complete</echo>  
</target> 

This does what it's supposed to do, take the jar file from the {dist} directory, and put it in the zip so that the {dist} directory is NOT maintained - ie: the jar file gets extracted to wherever the zip file is.

HOWEVER, I would like to know how to do the following:

For that second part with the empty directories, I'm perfectly content having the zip task create those in the zip itself without necessarily creating them on the local file system. In fact, the ideal would be if they'd be in the zip file and NOT be on the local file system - but I'll take whatever method I can get that works.

So, my local file system will look like so:

I want the ZIP file, when unzipped, to result in:

How do I accomplish this? I've tried variants of fileset, dirset, zipfileset, and am failing at every attempt. The best I've managed is to get the files in the {config} directory to get extracted as if the {config} directory doesn't exist... the hierarchy for that is NOT maintained, but I WANT it to be.

Upvotes: 1

Views: 2127

Answers (1)

smooth reggae
smooth reggae

Reputation: 2219

Here's one way you can use zipfileset to handle the config directory:

<zipfileset dir="." includes="config/*" excludes="config/test-config.xml"/>

The only way I can think of getting empty empty folders into the zip is to create them on the file system, add them using a zipfileset and then delete them once you have created the zip file.

Here is a fragment that does what you need (NOTE: I am using folder and file names based on your example; you can replace these with property references as needed):

<tempfile property="temp.file" destDir="." prefix="foo"/>
<mkdir dir="${temp.file}/subdir1"/>
<mkdir dir="${temp.file}/subdir2"/>
<zip destfile="${dist}/${zipfilename}">
  <zipfileset dir="${dist}" includes="${jarfilename}"/>
  <zipfileset dir="${temp.file}" includes="*"/>
  <zipfileset dir="." includes="config/*" excludes="config/test-config.xml"/>
</zip>
<delete dir="${temp.file}"/>

Upvotes: 1

Related Questions