如何使用 Java NIO 从嵌套的 zip 文件中读取文件 [英] How to read a file from a nested zip file using Java NIO

查看:34
本文介绍了如何使用 Java NIO 从嵌套的 zip 文件中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从嵌套的 zip 文件(另一个 zip 中的一个 zip 文件)中读取

I want to read from a nested zip file (a zip file inside another zip)

a.zip -> b.zip -> c.txt

a.zip -> b.zip -> c.txt

使用 Java NIO 但我得到一个 NullPointerException 为内部 zip 文件创建文件系统.

using Java NIO but I get a NullPointerException creating the FileSystem for the inner zip file.

Java NIO 支持吗?你有什么提示我做错了什么吗?

Is this supported by Java NIO? Do you have any hints what am I doing wrong?

这是测试程序:

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)

推荐答案

第二次需要使用 p (Path) 作为参数:

You need use p (Path) as argument second time:

try (FileSystem zipP = FileSystems.newFileSystem(p, null)) {
                                System.out.println("Success :-)");
                            } 

这篇关于如何使用 Java NIO 从嵌套的 zip 文件中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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