Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

This script will search a text in a jar file or a directory full of jar files recursively.

package tools

import java.util.jar.*

class FindJarFile{
	def static main(args){
		new FindJarFile().run(args)
	}
	def run(args){
		//Usage check.
		if(args.size() <2){
			println "usage: ${this.class.simpleName} searchstr jarfile [jarfile]"
			println "  jarfile can be a directory, which will include all jar files."
			return
		}

		//Search all input args as file
		def searchstr = args[0]
		args[1..-1].each{ arg->
			def file = new File(arg)
			searchInputFile(searchstr, file)
		}
	}
	def searchTextInJarFile(text, file){
		def jarfile = new JarFile(file)
		jarfile.entries().each{ entry->
			if (entry.name =~ searchstr){
				println "$entry.name : $file.canonicalPath"
			}
		}
	}
	def searchInputFile(text, inputFile){
		def filePattern = ~/.*\.(jar|zip)$/
		if(inputFile.isDirectory()){
			inputFile.eachFileRecurse{ subFile ->
				searchInputFile(text, subFile)
			}
		}else{
			if(inputFile.getName() =~ filePattern){
				searchTextInJarFile(text, inputFile)
			}
		}
	}

}

  • No labels