搜索jar包中含有指定字符串的文件

package com.lee.others;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 寻找指定路径下jar包中含特定字符串的文件
 * @author 千年独步
 */
public class FindStrInJar {
	
    public String condition;  //查询的条件

    public ArrayList<String> jarFiles = new ArrayList<String>();

    public FindStrInJar() {
    }

    public FindStrInJar(String condition) {
        this.condition = condition;
    }
    
    public FindStrInJar(String condition, String exclude) {
        this.condition = condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }
    
    public List<String> find(String dir, boolean recurse) {
        searchDir(dir, recurse);
        return this.jarFiles;
    }
    
    public List<String> getFilenames() {
        return this.jarFiles;
    }

    protected String getClassName(ZipEntry entry) {
        StringBuffer className = new StringBuffer(entry.getName().replace("/", "."));
        return className.toString();
    }

    @SuppressWarnings("unchecked")
	protected void searchDir(String dir, boolean recurse) {
        try {
            File d = new File(dir);
            if (!d.isDirectory()) {
                return;
            }
            File[] files = d.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (recurse && files[i].isDirectory()) {
                    searchDir(files[i].getAbsolutePath(), true);
                } else {
                    String filename = files[i].getAbsolutePath();
                    if (filename.endsWith(".jar")||filename.endsWith(".zip")) {
                        ZipFile zip = new ZipFile(filename);
                        Enumeration entries = zip.entries();
                        while (entries.hasMoreElements()) {
                            ZipEntry entry = (ZipEntry) entries.nextElement();
                            String thisClassName = getClassName(entry);
                            
                            //不搜索扩展名为.class的文件
                            if(thisClassName.lastIndexOf(".class")==-1){
                            	BufferedReader r = new BufferedReader(new InputStreamReader(zip.getInputStream(entry)));
                            	while(r.read()!=-1){
                            		String tempStr = r.readLine();
                            		if(null!=tempStr && tempStr.indexOf(condition)>-1){
                            			this.jarFiles.add(filename + "  --->  " + thisClassName);
                            			break;
                            		}
                            	}
                            }
                            
                            //获取类名为??的文件
//                            if (thisClassName.equals(condition) || thisClassName.equals(condition + ".class")) {
//                                this.jarFiles.add(filename);
//                            }
                            //获取扩展名为??的文件
//                            if(thisClassName.lastIndexOf("properties")>-1) {
//                              this.jarFiles.add(filename + "  --->  " + thisClassName);
//                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        FindStrInJar findInJar = new FindStrInJar("append"); //要寻找的字符串
        List<String> jarFiles = findInJar.find("D:/other_workspace/idtag4/idtag-API/WebContent/WEB-INF/lib", true);
        if (jarFiles.size() == 0) {
            System.out.println("Not Found");
        } else {
            for (int i = 0; i < jarFiles.size(); i++) {
                System.out.println(jarFiles.get(i));
            }
        }
    }
    
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
原创Eclipse插件: 高级搜索功能 - com.realzyc.search.jar Eclipse Plugin 插件 java 全局 搜索 + 特性: 相比与原有eclipse仅支持的在当前文档的搜索功能, 本插件支持了如下的搜索范围: - 当前文档 原有 查找/替换 已支持 - 当前选 原有 查找/替换 已支持 - 所有打开的文档 原有 查找/替换 不支持 - 当前项目 原有 查找/替换 不支持 - 所有打开的项目 原有 查找/替换 不支持 - 整个工作空间 原有 查找/替换 不支持 仅以在“整个工作空间”搜索为例, 插件提供在整个工作空间(workspace)的全部文档的搜索.即使项目在关闭状态, 也可以获得搜索结果. + 特性: 本搜索插件提供了“搜索全部”功能, 并将结果全部在列表显示.用户可以全局通观搜索结果, 并任意的导航的希望的结果.(而不是像原有的功能一样逐一的导航到结果). + 其他: 插件还提供: 字体支持, 英语言界面切换, 搜索摘要显示等特性. + 本发布包含有文件: com.realzyc.search_1.3.5.jar 插件最小执行文档, 不含源文件/javadoc com.realzyc.search.introduction.en.pdf 插件介绍文档, 英文 com.realzyc.search.introduction.cn.pdf 插件介绍文档, 文 readme.txt 插件介绍摘要 + 声明: 本插件由RealZYC独立开发, 仅用于个人使用. 如果有爱好这方面插件的同志们, 请回复或发邮件联系, 也欢迎大家加入编程Q群(78728985)互相切磋 欢迎大家下载评论.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值