JVM Rocks

java, groovy, grails, gradle, clojure.

Gradle: Extract a Folder From a Zip File

Let’s say you have a zip file with the containing these two folders(lib, classes) and you want to extract the lib folder to another zip file.

home.zip
-lib
-classes

first extract the zip contents into a folder

1
2
3
4
task extractZip(type: Copy) {
  from zipTree('home.zip')
  destinationDir = file('temp')
}  

then use another task to create the desired zip file

1
2
3
4
5
task createZip(type: Zip, dependsOn: extractZip) {
  baseName = 'lib'
  from ('temp/home') { include 'lib/**'}
  destinationDir = file('dist')
}