我有一个 zip 文件,其中包含 zip 文件(它们本身可能包含 zip 文件)。
parent.zip
|- child_1.zip
| |- foo.txt
|
|- child_2.zip
| |- bar.txt
|
|- baz.txt
使用 ZipFile,我可以获取父 zip 文件的 ZipEntries,并查看子文件(child_1.zip、child_2.zip、baz.txt),但我无法找到一种方法来检查这些子 zip 文件(foo.txt、bar.txt)的内容而不膨胀父 zip。
这可能吗,还是我需要膨胀parent.zip?
最佳答案
可以使用zip file system使用 jar:file: 协议(protocol):
URI uri = new URI(
"jar:file:/home/.../.../external.zip!/.../internal.zip!/");
Map<String, ?> env = new HashMap<>();
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path rootPath2 = zipfs.getPath("/");
Files.walk(rootPath2).forEach(p -> {
System.out.printf("Path %s%n", p.toString());
});
}
对于递归遍历,必须创建带有“!/”的 URI,并自行执行递归。
使用文件
可以将文件复制到zip文件系统中。 (这里我有一些疑问。)
关于java - 在 Java 中,您可以浏览嵌套 zip 文件的内容而不膨胀父文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54030188/