Skip to main content

Gradle : How to list files inside a Jar, Zip Tar Files in Gradle Task

How to load and read files in Gradle Project.

The Gradle Project Api has the following methods for using files.

  1. file() - to load and read a particular file
  2. files() - to load and read multiple files
  3. fileTree() - to load and read file hierarchy in a directory or folder
  4. zipTree() - to load and read a zip file (includes Jar , War and Ear files)
  5. tarTree() - to load and read a tar file

How to list files inside a Zip file Jar File and Tar file

Lets say we have a file structure as shown in image below.

File Structure

Following Gradle task will list the files inside a zip file, jar file and a tar file at the location "src/main/resources/archives/"

task showFiles(){
doLast{
FileTree zipFiles = zipTree("src/main/resources/archives/resources.zip");
FileTree jarFiles = zipTree("src/main/resources/archives/resources.jar");
FileTree tarFiles = tarTree("src/main/resources/archives/resources.tar.gz");

println " \n#Contents of Zip File"
zipFiles.each {
println "${it.name}"
}

println " \n#Contents of Jar File"
jarFiles.each {
println "${it.name}"
}

println " \n#Contents of Tar File"
tarFiles.each {
println "${it.name}"
}
}
}

When we run the above gradle task using command gradle -q showFiles, it will give the following output.

 
#Contents of Zip File
file1.properties
sample1.txt
sample2txt

#Contents of Jar File
file1.properties
sample1.txt
sample2txt

#Contents of Tar File
sample1.txt
file1.properties
sample2txt

Read More

Comments