Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy - Zipping All Folders In A Directory

I have a folder located C:\Program Files\VideoEditing and inside there I currently have about 30 folders which I would like zip. While zipping it I would like to add an image to each newly zipped folder. (the image location is C:\Program Files\VideoEditing\art.png).

I was wondering if/how this might be possible in groovy?

My goal is to end up with 60 files/folders in my VideoEditing directory. (30 being the originals and 30 zipped versions with the image inside it)

I'm going to continue searching for more information on the topic, but figured I might as well post it in case someone already knows how to go about it.

.

EDIT

Based on andrei1089's suggestion of using AntBuilder, I assume the code would look something like:

File file = new File('C:\\Program Files\\VideoEditing')

fileDir = []
def ant = new AntBuilder()
int i = 0

file.eachDir {
    fileDir << it
}

fileDir.each { 
    ant.zip(//new file name = VidFolder_$i,
        //include folder,
        //include art.png,)    
}

What I don't know is how to include specifically each directory rather than certain file types.

like image 766
StartingGroovy Avatar asked Dec 09 '25 15:12

StartingGroovy


2 Answers

a fileset will work just fine for simple cases

new AntBuilder().with {
  new File('src').eachDir {dir->
    zip destfile: "${dir.name}.zip", {
      fileset dir: dir
      fileset file: 'src/file.txt'
    }
  }
}

several examples that apply almost directly can be found in

  • http://ant.apache.org/manual/Tasks/zip.html
  • http://ant.apache.org/manual/Types/fileset.html
  • http://ant.apache.org/manual/Types/zipfileset.html
like image 181
jpertino Avatar answered Dec 11 '25 19:12

jpertino


You could try to use AntBuilder. Some useful examples can be found here

like image 35
andrei1089 Avatar answered Dec 11 '25 19:12

andrei1089



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!