Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This script searches one or more jar or zip files for a specified text stringfile/directory.

Code Block
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()}"
        }
    }