使用 InputStreamReader 读取 JAR 中的目录 [英] Read directory inside JAR with InputStreamReader

查看:15
本文介绍了使用 InputStreamReader 读取 JAR 中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我相信这个问题已经被问了一百万次,我已经阅读了几个小时,并尝试了一些人提供的几种选择,但没有一个对我有用.

我想列出应用程序 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);而 (scan.hasNext()) {字符串 s = scan.next();System.out.println("读取:" + s);}System.out.println("行尾");

并从 IDE 打印目录中的所有文件.IDE 外打印:END OF LINE".

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

 String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\)", "");JarFile jar = 新的 JarFile(s);JarEntry entry = jar.getJarEntry("resources");如果(条目!= null){System.out.println("存在");System.out.println(entry.getSize());}

那是我不得不对那个字符串做的一些可怕的编码.

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

解决方案

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

幸运的是,这实际上并不难(对我来说幸运的是你已经完成了大部分的努力).

基本上,一旦您获得了对 JarFile 的引用,您只需询问它的条目"并遍历该列表.

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

例如...

import java.io.File;导入 java.io.IOException;导入 java.util.Enumeration;导入 java.util.jar.JarEntry;导入 java.util.jar.JarFile;公共类 ReadMyResources {公共静态无效主(字符串 [] args){新的 ReadMyResources();}公共阅读我的资源(){JarFile jf = null;尝试 {String s = new File(this.getClass().getResource("").getPath()).getParent().replaceAll("(!|file:\\)", "");jf = 新的 JarFile(s);枚举条目 = jf.entries();而 (entries.hasMoreElements()) {JarEntry je = entry.nextElement();if (je.getName().startsWith("resources")) {System.out.println(je.getName());}}} catch (IOException ex) {ex.printStackTrace();} 最后 {尝试 {jf.close();} 捕获(异常 e){}}}}

警告

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

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

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:

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. Outside IDE prints: "END OF LINE".

Now, I can find an entry inside a Jar with this too:

        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.

Anyway... I can't get the list of resources inside the "resources" directory within the Jar... How can I do this???

解决方案

There's no way to simply get a filtered list of internal resources without first enumerating over the contents of the Jar file.

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.

By checking the JarEntry name for the required match (ie resources), you can filter the elements you want...

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.

This could be produced by your build process dynamically before the Jar file is created. 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

这篇关于使用 InputStreamReader 读取 JAR 中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆