본문 바로가기
장난감

Jar정보 검색

by umaking 2010. 8. 12.
 
JarScan j = new JarScan("C:/tmp");
j.search("umaUtils");
}
------------------------------

import java.io.File;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarScan {
	private String path = "";
	private String ptn = "";
	private int cnt = 0;
	public JarScan(String path) {
		this.path = path;
	}
	
	public void search(String findString) {
		this.ptn = findString;
		subScanDir(path);
		System.out.println("Complete: " + cnt + "Files");
	}
	
	private void subScanDir(String path) {
		File f = new File(path);
		File[] fileList = f.listFiles();
		if(fileList != null) {
			for(File fs : fileList) {
				if(fs.isDirectory()) {
				subScanDir(path + "/" + fs.getName());
			}else {
				String fileName = fs.getName().toLowerCase();
				if(fileName.lastIndexOf(".jar") != -1) {
					readJarFile(path + "/" + fs.getName());
				}
				}
			}
		}
	}
	
	private void readJarFile(String fileName) {
		JarFile jar = null;
		boolean flag = false;
		try {
			jar = new JarFile(fileName);
			if(jar != null) {
				Enumeration<JarEntry> entry = jar.entries();
				if(entry != null) {
					while(entry.hasMoreElements()) {
						JarEntry e = entry.nextElement();
						if(!e.isDirectory()) {
							if(e.getName().indexOf(this.ptn) != -1) {
								if(!flag) {
									System.out.println(++cnt + ") " + fileName);
									flag = true;
								}
								System.out.println("   " + e.getName());
							}
						}
					}
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(jar != null) try { if(jar != null) jar.close(); } catch(Exception e1) { }
		}
	}
}