如何计算JAR中文件夹中的文件数?

时间:2013-09-12 07:30:09

标签: java

我花了一些时间试图找到一种方法来计算JAR中文件夹中的文件数。我汇总了几个代码的示例,这些代码用于不同的目的以使其工作。当我通过Eclipse运行代码时它很好,但是在导出到JAR后它失败并返回0.在这种情况下,我使用的文件夹路径只是“rules /”。我将不胜感激任何建议或样品。感谢。

public static int countFiles(String folderPath) throws IOException { //Counts the number of files in a specified folder
    ClassLoader loader = ToolSet.class.getClassLoader();
    InputStream is = loader.getResourceAsStream(folderPath);
    try {
        byte[] c = new byte[1024];
        int count = 0;
        int readChars = 0;
        boolean empty = true;
        while ((readChars = is.read(c)) != -1) {
            empty = false;
            for (int i = 0; i < readChars; ++i) {
                if (c[i] == '\n') {
                    ++count;
                }
            }
        }
        return (count == 0 && !empty) ? 1 : count;
    } finally {
        is.close();
    }
}

修改 以下内容与我原来的问题不完全一致,但多亏了MadProgrammer,我能够减少代码并消除了对文件进行计数的需要。代码打击搜索我的JAR中的每个文件,查找以“.rules”结尾的文件,打开文件,在文件中搜索与“searchBox.getText()”匹配的字符串,附加结果,然后继续下一个“ .rules“文件。

    StringBuilder results = new StringBuilder();
    int count = 0;
    JarFile jf = null;
    try {
        String path = ToolSet.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String decodedPath = URLDecoder.decode(path, "UTF-8");
        jf = new JarFile(new File(decodedPath));
        Enumeration<JarEntry> entries = jf.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (entry.getName().endsWith(".rules")) {
                String name = entry.getName();
                InputStream in = ToolSet.class.getResourceAsStream(name);
                InputStreamReader isr = new InputStreamReader(in);
                BufferedReader bf = new BufferedReader(isr);
                String line;
                while ((line = bf.readLine()) != null) {
                    String lowerText = line.toLowerCase();
                    if(lowerText.indexOf(searchBox.getText().toLowerCase()) > 0) {
                        results.append(line + "\n");
                        count++;
                    }
                }
                bf.close();
            }
        }
    } catch (IOException ex) {
        try {
            jf.close();
        } catch (Exception e2) {
        }
    }
    if(count>0) {
        logBox.setText(results.toString());
    } else {
        logBox.setText("No matches could be found");
    }

2 个答案:

答案 0 :(得分:4)

Jar文件本质上是一个带有清单的Zip文件。

Jar / Zip文件实际上没有磁盘这样的目录概念。他们只是有一个有名字的条目列表。这些名称可能包含某种类型的路径分隔符,并且某些条目实际上可能被标记为目录(并且往往没有与它们相关联的任何字节,仅用作标记)

如果要查找给定路径中的所有资源,您将需要打开Jar文件并自行检查它的条目,例如......

JarFile jf = null;
try {
    String path = "resources";
    jf = new JarFile(new File("dist/ResourceFolderCounter.jar"));
    Enumeration<JarEntry> entries = jf.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        if (!entry.isDirectory()) {
            String name = entry.getName();
            name = name.replace(path + "/", "");
            if (!name.contains("/")) {
                System.out.println(name);
            }
        }
    }
} catch (IOException ex) {
    try {
        jf.close();
    } catch (Exception e) {
    }
}

现在,这需要您知道要使用的Jar文件的名称,这可能会有问题,因为您可能希望列出来自多个不同JAR的资源...

更好的解决方案是在构建时生成某种“资源查找”文件,其中包含您可能需要的所有资源名称,甚至可以键入特定名称...

这样你可以简单地使用......

BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsInputStream("/resources/MasterResourceList.txt")));
    String name = null;
    while ((name = br.readLine()) != null) {
        URL url = getClass().getResource(name);
    }
} finally {
    try {
        br.close();
    } catch (Exception exp) {
    }
}

例如......

您甚至可以使用资源数量播种文件;)

答案 1 :(得分:0)

这是一个简单的解决方案:

InputStream is = loader.getResourceAsStream(folderPath);

//open zip
ZipInputStream zip = new ZipInputStream(is);

//count number of files
while ((zip.getNextEntry()) != null ) {
    UnzipCounter++;
}
相关问题