Skip to content

Instantly share code, notes, and snippets.

@rody
Created November 4, 2011 13:34
Show Gist options
  • Save rody/1339323 to your computer and use it in GitHub Desktop.
Save rody/1339323 to your computer and use it in GitHub Desktop.
findJar
import java.util.jar.*
/**
* Searches entries (file name and directory, if any) in one or more named
* compressed (jar/zip) files, or in all compressed files in a named directory.
* To specify a search that includes special characters, like a period, use
* a backslash: \.xml
*/
if( args.size() < 2){
println "Required parameters: searchString filePath [filePath]\n"
println "NOTE: filePath may be a directory, which will search all jar/zip"
println " files in all subdirectories"
return
}
def searchstr = args[0]
args[1..-1].each{searchInputFile(searchstr, new File(it))}
def searchInputFile(text, inputFile){
def filePattern = ~/.*\.(jar|zip)$/
if(inputFile.isDirectory()){
inputFile.eachFileRecurse{
if(!it.isDirectory() && it.getName() =~ filePattern)
searchCompressedFile(text, it)
}
}else{
if(inputFile.getName() =~ filePattern){
searchCompressedFile(text, inputFile)
}
}
}
def searchCompressedFile(text, file){
try{
new JarFile(file).entries().each{ entry ->
if ( entry.name =~ text){
println "\n$entry.name : $file.canonicalPath"
}
}
}catch(Throwable t){
println "\nFailed to open $file.canonicalPath: ${t.toString()}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment