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:
/A/US/file1
/A/US/file2
/A/US/file3
/A/UK/file1
/A/UK/file2
/A/UK/file3
...
/B/US/file1
/B/US/file2
/B/US/file3
/B/UK/file1
/B/UK/file2
/B/UK/file3
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:
import java.util.zip.*
def createZip(destination, fileList) {
try{
byte[] buffer = new byte[1024];
ZipOutputStream out = new ZipOutputStream(new
FileOutputStream(destination));
out.setLevel(Deflater.DEFAULT_COMPRESSION);
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 = in_.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
in_.close();
}
out.close();
}
catch (IllegalArgumentException iae)
{
iae.printStackTrace();
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
null
}
/**
* 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"
if(numberFiles){
fileList = fileList.collect{ e ->
"${rootDirectory}\\${subDirectory}\\${elem}\\${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. */
/*
fileList[0..<numberFiles].each{
( 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.





