搜尋jar包中含有指定字串的檔案

來源:互聯網
上載者:User
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));            }        }    }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.