You 首先提取嵌套罐子。
编辑:
oracle论坛上的答案没有给出明确的原因为什么你必须先提取jar。
这是报价from https://blogs.oracle.com/rajendrag/entry/zip_file_system_providerRajendra Gutupalli 的博客(com.sun.nio.zipfs 的作者):
假设我们有一个嵌套在 Zip 中的 Jar 文件。以下程序打印嵌套 jarCompress1.jar 文件内的 MANIFEST.MF 文件的内容。
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);
}
}
}
还有另一个one https://blogs.oracle.com/rajendrag/entry/zip_file_system_provider_implementation:
这里需要注意的重要一点是,zip 文件路径可以扩展到文件路径名中的嵌套 zip 或 jar。例如,/home/userA/zipfile.zip/DirA/dirB/jarFile.jar/META-INF/MANIFEST.MF 访问 Zip 文件“/home/userA/zipfile.zip”内的 jar 文件“jarFile.jar”。
我无法重现所声称的行为。下一个代码:
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)
可能有人会确认这是一个错误或指出我的代码中的错误。
同时回到你原来的问题。您无法在 zip(jar) 中创建文件系统,因为没有FileSystemProvider
(查看源代码newFileSystem
方法)可以创建FileSystem
来自 ZipPath 的实例。因此,您必须选择从外部 zip 中提取或编写自己的FileSystemProvider
执行。