copying, zipping selecting files with Groovy

Today I had to select some files from different directories copy in another. Despite it seemed a simple task, it wasn’t. The files were a lot, so I had to pack it.

Basically the structure of the directories was something like:


and I had to select the N% of the files from each of the second level directories (AA,AB, AC…and so on).

I wrote this short script that select only N% (parameter inside the script) of the files from the directory, pack it (without any structure information), and copy on the destination directory:


def createZip(destination, fileList) {
        byte[] buffer = new byte[1024];

        ZipOutputStream out = new ZipOutputStream(new
        fileList.each { selected ->
            FileInputStream in_ = new FileInputStream(selected);
            //the new File(selected).getName() return just the name of the file,
            //in order to remove the directory structure inside.
            out.putNextEntry(new ZipEntry(new File(selected).getName()));

            int len;
            while ((len = > 0) {
                out.write(buffer, 0, len);
    catch (IllegalArgumentException iae)
    catch (FileNotFoundException fnfe)
    catch (IOException ioe)


  * Script
def rootDirectory = /C:\inputDirectory/
def outputDirectory = /C:\outputDirectory/

if ((!new File(rootDirectory).list()) || (! new File(outputDirectory).list())){
    println "Error. Input or Output directory are not valid."
    throw new RuntimeException() //TODO: understand how to exit without raising an exception

def directories = ['A', 'B', 'C', 'D']
def percentageRules = 5    //5 = 5% of the rules for each country

directories.each{ subDirectory ->
    println "listing ${rootDirectory}\\${subDirectory}"
    def directoryList = new File("${rootDirectory}\\${subDirectory}").list()
    if( directoryList ){
        directoryList.each{ elem ->
            def fileList = new File("${rootDirectory}\\${subDirectory}\\${elem}").list().grep{ !it.contains('out') }
            def numberFiles = (fileList.size() * 5 / 100) as Integer
            if ((numberFiles == 0) && (fileList.size() > 0)) {
                numberFiles = fileList.size()
            println "Zipping ${numberFiles} (${percentageRules}%) of files from ${rootDirectory}\\${subDirectory}\\${elem} into ${outputDirectory}\\${subDirectory}-${elem}.zip"

                fileList = fileList.collect{ e ->
                createZip("${outputDirectory}\\${subDirectory}-${elem}.zip", fileList[0..<numberFiles])

                /* The old version copied the files instead of zipping it, using AntBuilder,
                   you can use it if you don't need to zip the files. */
                    ( new AntBuilder ( ) ).copy ( file : "${rootDirectory}\\${subDirectory}\\${elem}\\${inputF}" , tofile : "${outputDirectory}\\${inputF}" )

I wrote it rushing, and I copied somewhere the first method (which was java), so might be errors or better ways. Anyway any suggestion are welcome.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s