在另一个jar中获取jar的FileSystems [英] get FileSystems of a jar inside another jar

查看:180
本文介绍了在另一个jar中获取jar的FileSystems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我要做的事情:

FileSystem fs1 = FileSystems.newFileSystem(Paths.get("f1.jar"), null);
FileSystem fs2 = FileSystems.newFileSystem(fs1.getPath("/f2.jar"), null);

但我收到 java.nio.file.ProviderNotFoundException 在第二行由 FileSystems.newFileSystem()抛出。

but I get a java.nio.file.ProviderNotFoundException thrown by FileSystems.newFileSystem() on the second line.

我做错了什么?

谢谢!

推荐答案

必须先提取嵌套的jar。

You have to extract nested jar first.

编辑:
oracle论坛上的答案没有说清楚为什么你必须先提取jar。

Answers on oracle forum don't give clear reason why you have to extract jar first.

这里是引用来自 Rajendra Gutupalli的博客(com.sun.nio.zipfs的作者):

Here is quote from Rajendra Gutupalli's blog (author of com.sun.nio.zipfs):


假设我们在Zip中嵌套了一个Jar文件。以下程序打印MANIFEST.MF文件的内容,该文件位于嵌套的jarCompress1.jar文件中。

Let's assume that we have a Jar file nested inside a Zip. The following program prints the contents of the MANIFEST.MF file which is inside nested jarCompress1.jar file.

import java.io.BufferedInputStream; 
import java.nio.file.*; 
import java.util.HashMap; 
import java.util.Map;

public class ReadEntry {

    public static void main(String... args) throws Exception {
        Path zipfile = Path.get("c:/zips/zip1.zip");
        Map<String, String> env = new HashMap();
        FileSystem manager = FileSystems.newFileSystem(zipfile, env,null);
        Path path = manager.getPath("/jarCompress1.jar/META-INF/MANIFEST.MF");
        System.out.println("Reading input stream");
        BufferedInputStream bis = new BufferedInputStream(path.newInputStream());
        int ch = -1;
        while ((ch = bis.read()) != -1) {
            System.out.print((char) ch);
        }
    } 
}


另一个一个

这里要注意的重要一点是,zip文件路径可以扩展到文件路径名中的嵌套拉链或罐子。例如,/ home / userA / zipfile.zip / RemoteA / dirB / jarFile.jar / META-INF / MANIFEST.MF访问Zip文件/home/userA/zipfile.zip中的jar文件jarFile.jar。

Important point to note here is, zip file path can expand to nested zips or jars in the file's path name. For example, /home/userA/zipfile.zip/DirA/dirB/jarFile.jar/META-INF/MANIFEST.MF accesses the jar file "jarFile.jar" inside Zip file "/home/userA/zipfile.zip".

我无法重现声明的行为。下一个代码:

I couldn't reproduce claimed behavior. Next code:

try (FileSystem fs1 = FileSystems.newFileSystem(Paths.get("f1.zip"), null)) {
    Path path = fs1.getPath("/f2.zip/test.txt");
    Files.lines(path).forEach(System.out::println);
}

给予例外

Exception in thread "main" java.nio.file.NoSuchFileException: f2.zip/test.txt
  at com.sun.nio.zipfs.ZipFileSystem.newInputStream(ZipFileSystem.java:544)
  at com.sun.nio.zipfs.ZipPath.newInputStream(ZipPath.java:645)
  at com.sun.nio.zipfs.ZipFileSystemProvider.newInputStream(ZipFileSystemProvider.java:278)
  at java.nio.file.Files.newInputStream(Files.java:152)
  at java.nio.file.Files.newBufferedReader(Files.java:2781)
  at java.nio.file.Files.lines(Files.java:3741)
  at java.nio.file.Files.lines(Files.java:3782)

可能有人会确认这是一个错误或指向我的代码中的错误。

May be someone would confirm that it's a bug or point to the error in my code.

同时返回到您的原始问题。您无法在zip(jar)中创建FileSystem,因为没有 FileSystemProvider (查看源代码 newFileSystem 方法)哪个可以从ZipPath创建 FileSystem 实例。因此,您必须从外部zip中提取选项或编写您自己的 FileSystemProvider 实现。

Meanwhile returning to your original question. You cannot create FileSystem inside zip(jar) because there is no FileSystemProvider (look source code of newFileSystem method) which can create FileSystem instance from ZipPath. Hence you have to options extract the from outer zip or write your own FileSystemProvider implementation.

这篇关于在另一个jar中获取jar的FileSystems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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