简体   繁体   English

使用InputStreamReader读取JAR中的目录

[英]Read directory inside JAR with InputStreamReader

So, this question has been asked a million times i believed and I've been reading them for a couple of hours and trying several options given by some people but none of them work for me. 所以,这个问题已被问过我相信的一百万次,而且我已经读了几个小时,尝试了一些人给出的几个选项,但没有一个适合我。

I want to list all the files inside a directory inside the application's JAR, so in IDE this works: 我想列出应用程序JAR中目录中的所有文件,因此在IDE中这有效:

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

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

That gives me all the files inside the directory. 这给了我目录中的所有文件。

Now, i've tried this also: 现在,我也试过这个:

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");

And from IDE it prints ALL the files in the directory. 然后从IDE中打印目录中的所有文件。 Outside IDE prints: "END OF LINE". IDE外部打印:“END OF LINE”。

Now, I can find an entry inside a Jar with this too: 现在,我也可以在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());
        }

That's some horrible coding i had to do to that String. 这是我必须对那个String做的一些可怕的编码。

Anyway... I can't get the list of resources inside the "resources" directory within the Jar... How can I do this??? 无论如何......我无法获得Jar内“resources”目录内的资源列表......我该怎么做?

There's no way to simply get a filtered list of internal resources without first enumerating over the contents of the Jar file. 在没有首先枚举Jar文件的内容的情况下,无法简单地获取内部资源的过滤列表。

Luckily, that's actually not that hard (and luckily for me you've done most of the hardwork). 幸运的是,这实际上并不那么难(幸运的是,你已经完成了大部分的努力工作)。

Basically, once you have a reference to the JarFile , you simple need to ask for its' entries and iterate over that list. 基本上,一旦你有了对JarFile的引用,你就需要简单地询问它的' entries并迭代该列表。

By checking the JarEntry name for the required match (ie resources ), you can filter the elements you want... 通过检查所需匹配的JarEntry名称(即resources ),您可以过滤所需的元素...

For example... 例如...

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) {
            }
        }
    }

}

Caveat 警告

This type of question actually gets ask a bit. 这类问题实际上有点问题。 Rather then trying to read the contents of the Jar at runtime, it would be better to produce some kind of text file which contained a list of the available resources. 而不是尝试在运行时读取Jar的内容,最好生成某种包含可用资源列表的文本文件。

This could be produced by your build process dynamically before the Jar file is created. 这可以由您的构建过程在创建Jar文件之前动态生成。 It would be a much simpler solution to then read this file in (via getClass().getResource() , for example) and then look up each resource list in the text file...IMHO 这将是一个更简单的解决方案然后读取此文件(例如通过getClass().getResource() )然后查找文本文件中的每个资源列表...恕我直言

For Spring Framework users, have a look at PathMatchingResourcePatternResolver to do something like the following: 对于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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM