Search code examples
groovyantbuilder

Zip specific files with AntBuilder


I need to zip a collection of files from different location into one zip, keeping their initial relations. For example, I need only a1 and b2 from the following folder structure

Top -- A -- a1
         -- a2
    -- B -- b1
            b2

and i want the zip file to look like:

Top -- A -- a1
    -- B -- b2

How can I do that using AntBuilder? I've tried:

def deploymentFiles = [ "$HOME/Songs/a.tsv", "$HOME/Songs/b.tsv", ]

def ant = new AntBuilder()

def zipFile = new File("deployment_zipFile.zip")

ant.zip( destFile: "${zipFile.getAbsolutePath()}" ) { fileset( dir: "$HOME" ) { deploymentFiles.each {f -> includes: deploymentFiles.join(",") } } }

but this just zipped the entire HOME folder.


Solution

  • Given a directory structure like this:

    -- home
       |-- Songs
       |   |-- A
           |   |-- a1.tsv
           |   \-- a2.tsv
           |-- B
               |-- b1.tsv
               \-- b2.tsv
    

    Then, this code:

    def HOME = 'home'
    def deploymentFiles = [ 'Songs/A/a1.tsv', 'Songs/B/b1.tsv' ]
    def zipFile = new File("deployment_zipFile.zip")
    new AntBuilder().zip( basedir: HOME,
                          destFile: zipFile.absolutePath,
                          includes: deploymentFiles.join( ' ' ) )
    

    Creates a zip file which when extracted contains:

    unzip ../deployment_zipFile.zip 
    Archive:  ../deployment_zipFile.zip
       creating: Songs/
       creating: Songs/A/
      inflating: Songs/A/a1.tsv          
       creating: Songs/B/
      inflating: Songs/B/b1.tsv