java 读取 JAR 文件的内容(在运行时)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1463192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:37:49  来源:igfitidea点击:

Reading content of a JAR file (at runtime)?

apijarjava

提问by ivan_ivanovich_ivanoff

I have read the posts:

我已经阅读了以下帖子:

Viewing contents of a .jar file
and
How do I list the files inside a JAR file?

查看 .jar 文件的内容
以及
如何列出 JAR 文件中的文件?

But I, sadly, couldn't find a good solution to actually reada JAR's content (file by file).

但遗憾的是,我找不到一个好的解决方案来实际读取JAR 的内容(逐个文件)。

Furthermore, could someone give me a hint, or point to a resource, where my problem is discussed?

此外,有人可以给我一个提示,或指向一个资源,在那里讨论我的问题?

I just could think of a not-so-straight-forward-way to do this:
I could somehow convert the list of a JAR's resources to a list of inner-JAR URLs, which I then could open using openConnection().

我只能想到一种不那么直接的方法来做到这一点:
我可以以某种方式将 JAR 资源列表转换为内部 JAR URL 列表,然后我可以使用 openConnection() 打开它。

回答by NawaMan

You use JarFileto open a Jar file. With it you can get ZipEntry or JarEntry (they can be seen as the same thing) by using 'getEntry(String name)' or 'entires'. Once you get an Entry, you can use it to get InputStream by calling 'JarFile.getInputStream(ZipEntry ze)'. Well you can read data from the stream.

您可以使用JarFile打开 Jar 文件。有了它,您可以通过使用“getEntry(String name)”或“entires”来获得 ZipEntry 或 JarEntry(它们可以被视为相同的东西)。获得 Entry 后,您可以使用它通过调用“ JarFile.getInputStream(ZipEntry ze)”来获取 InputStream 。那么你可以从流中读取数据。

See a tutorial here.

请参阅此处的教程。

回答by ZZ Coder

Here is how I read it as a ZIP file,

这是我将其作为 ZIP 文件读取的方式,

   try {
        ZipInputStream is = new ZipInputStream(new FileInputStream("file.jar"));
        ZipEntry ze;

        byte[] buf = new byte[4096];
        int len;

        while ((ze = is.getNextEntry()) != null) {

            System.out.println("----------- " + ze);
            len = ze.getSize();

            // Dump len bytes to the file
            ...
        }
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

This is more efficient than JarFile approach if you want decompress the whole file.

如果您想解压缩整个文件,这比 JarFile 方法更有效。

回答by Bathakarai

Here is the complete code which reads all the file contents inside the jar file.

这是读取 jar 文件中所有文件内容的完整代码。

public class ListJar {
    private static void process(InputStream input) throws IOException {
        InputStreamReader isr = new InputStreamReader(input);
        BufferedReader reader = new BufferedReader(isr);
        String line;

        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }

    public static void main(String arg[]) throws IOException {
        JarFile jarFile = new JarFile("/home/bathakarai/gold/click-0.15.jar");

        final Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (entry.getName().contains(".")) {
                System.out.println("File : " + entry.getName());
                JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
                InputStream input = jarFile.getInputStream(fileEntry);
                process(input);
            }
        }
    }
}