8

所以,我相信这个问题已经被问了一百万次了,我已经阅读了几个小时并尝试了一些人给出的几个选项,但没有一个对我有用。

我想列出应用程序 JAR 内目录中的所有文件,因此在 IDE 中可以使用:

File f = new File(this.getClass().getResource("/resources/").getPath());

for(String s : f.list){
   System.out.println(s);
}

这给了我目录中的所有文件。

现在,我也试过这个:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("resources/");
    InputStreamReader inReader = new InputStreamReader(in);
    Scanner scan = new Scanner(inReader);

    while (scan.hasNext()) {
        String s = scan.next();
        System.out.println("read: " + s);
    }

    System.out.println("END OF LINE");

并从 IDE 打印目录中的所有文件。IDE 外部打印:“END OF LINE”。

现在,我也可以在 Jar 中找到一个条目:

        String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\\\)", "");
        JarFile jar = new JarFile(s);

            JarEntry entry = jar.getJarEntry("resources");

        if (entry != null){
            System.out.println("EXISTS");
            System.out.println(entry.getSize());
        }

那是我必须对那个字符串做的一些可怕的编码。

无论如何...我无法获取 Jar 中“资源”目录中的资源列表...我该怎么做?

4

3 回答 3

10

如果不首先枚举 Jar 文件的内容,就无法简单地获取经过过滤的内部资源列表。

幸运的是,这实际上并不难(幸运的是,你已经完成了大部分艰苦的工作)。

基本上,一旦您引用了JarFile,您只需要询问它的'entries并遍历该列表。

通过检查JarEntry所需匹配的名称(即resources),您可以过滤您想要的元素...

例如...

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

public class ReadMyResources {

    public static void main(String[] args) {
        new ReadMyResources();
    }

    public ReadMyResources() {
        JarFile jf = null;
        try {            
            String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\\\)", "");
            jf = new JarFile(s);

            Enumeration<JarEntry> entries = jf.entries();
            while (entries.hasMoreElements()) {
                JarEntry je = entries.nextElement();
                if (je.getName().startsWith("resources")) {
                    System.out.println(je.getName());
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                jf.close();
            } catch (Exception e) {
            }
        }
    }

}

警告

这种类型的问题实际上会被问到一点。与其尝试在运行时读取 Jar 的内容,不如生成某种包含可用资源列表的文本文件。

这可以在创建 Jar 文件之前由您的构建过程动态生成。getClass().getResource()然后(例如通过 )读取此文件,然后在文本文件中查找每个资源列表,这将是一个更简单的解决方案...恕我直言

于 2013-08-15T07:05:07.077 回答
6

对于Spring Framework用户,请查看PathMatchingResourcePatternResolver执行以下操作:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath:path/to/resource/*.*");

for (Resource resource : resources) {
    InputStream inStream = resource.getInputStream();
    // Do something with the input stream
}
于 2018-05-15T19:56:40.883 回答
0

我的案例是读取资源中的目录:

资源结构

由于我的要求是将资源目录转换为 io.File,最后它看起来像这样:

public static File getResourceDirectory(String resource) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL res = classLoader.getResource(resource);
        File fileDirectory;
        if ("jar".equals(res.getProtocol())) {
            InputStream input = classLoader.getResourceAsStream(resource);
            fileDirectory = Files.createTempDir();
            List<String> fileNames = IOUtils.readLines(input, StandardCharsets.UTF_8);
            fileNames.forEach(name -> {
                String fileResourceName = resource + File.separator + name;
                File tempFile = new File(fileDirectory.getPath() + File.pathSeparator + name);
                InputStream fileInput = classLoader.getResourceAsStream(resourceFileName);
                FileUtils.copyInputStreamToFile(fileInput, tempFile);
            });
            fileDirectory.deleteOnExit();
        } else {
            fileDirectory = new File(res.getFile());
        }

        return fileDirectory;
    }

如果资源在 jar 中,我们将其复制到临时目录,该目录将在应用程序端删除。然后调用getResourceDirectory("migrations")返回给我io.File的目录以供进一步使用。

于 2021-09-16T06:18:00.593 回答