扫描JAR包中文件

原创
2017/11/07 11:08
阅读数 1.5K

Talk is cheap,show me the code!

如下代码,搜索lib包下的所有jar包中含有‘System.gc’ 的class文件

public class FindClass {
    public static final String path = "/home/amber/project/demo/WEB-INF/lib/"; //jar包父目录
    public static final String keywords = "System.gc"; //关键字
    public static void main(String[] args){
        File file = new File(path);
        final Pattern p = Pattern.compile(".+\\.jar$");
        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(p.matcher(pathname.getPath()).matches()){
                    return true;
                }else{
                    return false;
                }
            }
        });

        search(files);
    }

    private static void search(File[] files){
        try{
            for(File f : files){

                System.out.println("当前扫描jar:" + f.getPath());

                if(f.isDirectory()){
                    search(f.listFiles());
                }else{
                    ZipFile jar = new ZipFile(f);
                    Enumeration enumration = jar.entries();
                    while(enumration.hasMoreElements()){
                        ZipEntry zipEntry = (ZipEntry)enumration.nextElement();

                        InputStreamReader isr = new InputStreamReader(jar.getInputStream(zipEntry));
                        BufferedReader br = new BufferedReader(isr);
                        String line = br.readLine();
                        int line_num = 1;
                        while(null != line){
                            if(line.contains(keywords)){
                                System.out.println(f.getPath() + ","  +zipEntry.getName() + "," + "line number = " + line_num);
                            }
                            line = br.readLine();
                            line_num++;
                        }
                    }
                }
            }
        }catch(ZipException z_e){
            z_e.printStackTrace();
        }catch(IOException io_e){
            io_e.printStackTrace();
        }
    }
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部