简体   繁体   English

zip 文件在 zip 文件中的文件系统

[英]FileSystem for zip file inside zip file

Can a java.nio.file.FileSystem be created for a zip file that's inside a zip file?可以为java.nio.file.FileSystem文件创建 java.nio.file.FileSystem 文件吗?

If so, what does the URI look like?如果是这样,URI 是什么样的?

If not, I'm presuming I'll have to fall back to using ZipInputStream.如果没有,我假设我将不得不回退到使用 ZipInputStream。

I'm trying to recurse into the method below.我正在尝试递归到下面的方法。 Current implementation creates a URI "jar:jar:...".当前实现创建了一个 URI“jar:jar:...”。 I know that's wrong (and a potentially traumatic reminder of a movie character).我知道那是错误的(并且是对电影角色的潜在创伤性提醒)。 What should it be?应该是什么?

private static void traverseZip(Path zipFile ) {
    // Example: URI uri = URI.create("jar:file:/codeSamples/zipfs/zipfstest.zip");
    
    String sURI = "jar:" + zipFile.toUri().toString();
    URI uri = URI.create(sURI);

    Map<String, String> env = new HashMap<>();

    try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {

        Iterable<Path> rootDirs = fs.getRootDirectories();
        for (Path rootDir : rootDirs) {
            traverseDirectory(rootDir ); // Recurses back into this method for ZIP files
        }

    } catch (IOException e) {
        System.err.println(e);
    }
}

You can use FileSystem.getPath to return a Path suitable for use with another FileSystems.newFileSystem call which opens the nested ZIP/archive.您可以使用FileSystem.getPath返回适合与另一个FileSystems.newFileSystem调用一起使用的Path ,该调用打开嵌套的 ZIP/存档。

For example this code opens a war file and reads the contents of the inner jar file:例如,此代码打开一个 war 文件并读取内部 jar 文件的内容:

Path war = Path.of("webapps.war");
String pathInWar = "WEB-INF/lib/some.jar";

try (FileSystem fs = FileSystems.newFileSystem(war)) {
    
    Path jar = fs.getPath(pathInWar);

    try (FileSystem inner = FileSystems.newFileSystem(jar)) {
        for (Path root : inner.getRootDirectories()) {
            try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE, (p,a) -> true)) {
                stream.forEach(System.out::println);
            }
        }
    }
}

Note also that you code can pass in zipFile without changing to URI:另请注意,您的代码可以传入 zipFile 而不更改为 URI:

try (FileSystem fs = FileSystems.newFileSystem(zipFile, env)) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM