如何使用Java NIO从嵌套的zip文件中读取文件

3

我想要从嵌套的zip文件(一个zip文件内包含另一个zip)中读取内容,使用Java NIO但是我创建内部zip文件系统时遇到了NullPointerException错误。

请问Java NIO支持这种操作吗?你有什么提示或建议吗?

以下是测试程序:

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class NestedZipTest
{

    public static void main(String[] args)
        throws IOException, URISyntaxException
    {
        Path path = Paths.get("c:\\temp\\a.zip");
        System.out.println("Open: " + path.toUri());
        try (FileSystem zipfs = FileSystems.newFileSystem(path, null))
        {
            for (Path rootPath : zipfs.getRootDirectories())
            {
                Files.walk(rootPath).forEach(p -> {
                    System.out.println("Root-Path " + rootPath + " File: " + p);
                    if (p.toString().endsWith(".zip"))
                    {
                        System.out.println("Try to open nested zip file " + p);
                        try
                        {
                            URI u = p.toUri();

                            System.out.println("Nested zip file URI: " + u);
                            try (FileSystem zipP = FileSystems.newFileSystem(u, null))
                            {
                                System.out.println("Success :-)");
                            }
                        }
                        catch (Throwable exp)
                        {
                            System.err.println("Creating file system for nested zip file failed :-(");
                            exp.printStackTrace();
                        }
                    }
                });
            }
        }
    }
}

输出结果为:

Open: file:///c:/temp/a.zip
Root-Path / File: /
Root-Path / File: /b.zip
Try to open nested zip file /b.zip
Nested zip file URI: jar:file:///c:/temp/a.zip!/b.zip
Creating file system for nested zip file failed :-(
java.lang.NullPointerException
    at com.sun.nio.zipfs.ZipFileSystem.<init>(ZipFileSystem.java:103)
    at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:117)
    at java.nio.file.FileSystems.newFileSystem(FileSystems.java:326)
    at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276)
    at NestedZipTest.lambda$0(NestedZipTest.java:32)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at NestedZipTest.main(NestedZipTest.java:22)
1个回答

0
你需要第二次使用 p(路径)作为参数:
try (FileSystem zipP = FileSystems.newFileSystem(p, null)) {
                                System.out.println("Success :-)");
                            } 

该项目在此处 https://github.com/sbzDev/stackoverflow/tree/master/question56124117 。Java 12 Mac。 - Sergey Bzhezitskiy
你是在Linux还是Windows下尝试过了吗?也许它在Linux下可以工作,但在Windows下不能? - Uli
我没有Windows...只有MacOS。 - Sergey Bzhezitskiy
我的程序在Windows和Java 8下无法运行。 我的程序在Linux和Java 8下无法运行。 你的程序在Windows和Java 8下无法运行。 所以可能是操作系统特定问题或者需要升级到Java 12来解决。 - Uli
我无法在任何地方找到关于它的文档,但这似乎在Java 8中不受支持。 - woggioni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接