sagioto
sagioto

Reputation: 261

Zipping sub folders into separate zip files

I have a directory structure like this: Parent |->A |->B |->* and I need to zip it like so: Dest |->A.zip |->B.zip |->*.zip Where the * means that I don't know what are the names of the sub-folders.

How can I do this?

I'm working with a maven project but I don't mind using the antrun plugin

Upvotes: 0

Views: 156

Answers (2)

srbs
srbs

Reputation: 634

Here is another solution that I like since it doesn't require antcontrib:

<target name="zipContentPackage">
    <basename property="dir.name" file="${file_name}"/>
    <echo message="Zipping folder: ${dir.name}"/>
    <zip destfile="${zip_dir}/${dir.name}.zip" basedir="${file_name}"/>
</target>

<target name="-wrapper-zipContentPackage">
    <antcall target="zipContentPackage" inheritAll="true">
        <param name="file_name" value="${basedir}" />
    </antcall>
</target>

<target name="packageContent">
    <delete dir="${zip_dir}"/>
    <mkdir dir="${zip_dir}"/>

    <subant genericantfile="${ant.file}" target="-zipContentPackage" inheritall="true">
        <dirset dir="${content_dir}" includes="*" />
    </subant>
</target>

Notes:

  1. I used sagioto's code to illustrate
  2. This isn't the most efficient solution

Upvotes: 2

sagioto
sagioto

Reputation: 261

I ended up using foreach of antcontrib

<target name="init">
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.compile.classpath"/>
</target>


<target name="packageContent" depends="init">
    <delete dir="${zip_dir}"/>
    <mkdir dir="${zip_dir}"/>
    <foreach target="zipContentPackage" param="file_name">
        <path>
            <dirset dir="${content_dir}" includes="*"/>
        </path>
    </foreach>
</target>

<target name="zipContentPackage">
    <basename property="dir.name" file="${file_name}"/>
    <echo message="Zipping folder: ${dir.name}"/>
    <zip destfile="${zip_dir}/${dir.name}.zip" basedir="${file_name}"/>
</target>

Upvotes: 0

Related Questions