如何访问jar文件中的文件夹内容? [英] How do I access the content of folders inside a jar file?

查看:113
本文介绍了如何访问jar文件中的文件夹内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要查看特定包中的文件名。目前,我正在执行以下操作:

I need to take a look at the names of files inside a specific package. Currently, I'm doing the following:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL packageUrl = loader.getResource(getPackage().replace('.', '/'));
File packageDir = new File(packageUrl.getFile());

// Work with the files inside the directory

这实际上只是工作在Eclipse中很好,因为默认情况下它不打包我的应用程序。但是,当我从jar文件运行它时,我在 packageUrl 上得到 NullPointerException

This actually works just fine in Eclipse, since it doesn't package my application by default. When I run it from a jar file, however, I get a NullPointerException on packageUrl.

那么,我如何获得jar中包的内容列表?我的研究建议使用 getResourceAsStream ,但我不太确定如何使用 InputStream 来探索目录,如果是的话甚至可能。

So, how do I obtain a list of the contents of a package that's inside a jar? My research suggested using getResourceAsStream, but I'm not quite sure how to explore a directory with an InputStream, if that's even possible.

推荐答案

您可以获取jar文件的路径并使用ZipInputStream列出该jar文件中的所有文件。

You could get path to the jar file and open it with ZipInputStream list all the files inside that jar.

要知道正在运行的jar的路径,请尝试使用:

To know the path of the running jar, try using:

InputStream in = MyClass
                .class
                .getProtectionDomain()
                .getCodeSource()
                .getLocation()
                .openStream();

参见:如何列出JAR文件中的文件?

更新

我编写了一个运行你的解决方案并且运作完美:

I've compiled an ran your solution and works perfect:

C:\java\injar>dir
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 22A8-203B

 Directorio de C:\java\injar

21/02/2011  06:23 p.m.    <DIR>          .
21/02/2011  06:23 p.m.    <DIR>          ..
21/02/2011  06:23 p.m.             1,752 i18n.jar
21/02/2011  06:23 p.m.    <DIR>          src
21/02/2011  06:21 p.m.    <DIR>          x

C:\java\injar>jar -tf i18n.jar
META-INF/
META-INF/MANIFEST.MF
I18n.class
x/
x/y/
x/y/z/
x/y/z/hola.txt

C:\java\injar>type src\I18n.java
import java.util.*;
import java.net.*;
import java.util.jar.*;
class I18n {
    public static void main( String ... args ) {
        getLocaleListFromJar();
    }
    private static List<Locale> getLocaleListFromJar() {
        List<Locale> locales = new ArrayList<Locale>();
        try {
            URL packageUrl = I18n.class.getProtectionDomain().getCodeSource().getLocation();
            JarInputStream jar = new JarInputStream(packageUrl.openStream());
            while (true) {
                JarEntry entry = jar.getNextJarEntry();
                if (entry == null) {
                    System.out.println( "entry was null ");
                    break;
                }
                String name = entry.getName();
                System.out.println( "found : " +name );
                /*if (resourceBundlePattern.matcher(name).matches()) {
                    addLocaleFromResourceBundle(name, locales);
                }*/
           }
        } catch (Exception e) {
            System.err.println(e);
            return null;
            //return getLocaleListFromFile(); // File based implementation in case resources are not in jar
        }
        return locales;
    }
}


C:\java\injar>java -jar i18n.jar
found : I18n.class
found : x/
found : x/y/
found : x/y/z/
found : x/y/z/hola.txt
entry was null

这篇关于如何访问jar文件中的文件夹内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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