java.nio.file.ProviderNotFoundException:Provider";zip";找不到
我试图使用java.nio-API来遍历.zip文件,但是当我试图调用java.nio.file.ProviderNotFoundException:Provider";zip";找不到,java,Java,我试图使用java.nio-API来遍历.zip文件,但是当我试图调用FileSystems.newFileSystem时,我得到了一个ProviderNotFoundException 我还尝试将zip:file:更改为jar:file:,但我得到了相同的异常,只是消息说Provider“jar”找不到 我还尝试过直接使用FileSystems.newFileSystem(Path,null),而不首先创建URI 输出: Reading zip-file: /home/pyknic/Downl
FileSystems.newFileSystem
时,我得到了一个ProviderNotFoundException
我还尝试将zip:file:
更改为jar:file:
,但我得到了相同的异常,只是消息说Provider“jar”找不到
我还尝试过直接使用FileSystems.newFileSystem(Path,null)
,而不首先创建URI
输出:
Reading zip-file: /home/pyknic/Downloads/Walking.zip
Exception in thread "main" java.nio.file.ProviderNotFoundException: Provider "zip" not found
at java.base/java.nio.file.FileSystems.newFileSystem(FileSystems.java:364)
at java.base/java.nio.file.FileSystems.newFileSystem(FileSystems.java:293)
at com.github.pyknic.zipfs.Main.main(Main.java:19)
Main.java
package com.github.pyknic.zipfs;
import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.util.stream.StreamSupport;
import static java.lang.String.format;
import static java.util.Collections.singletonMap;
public class Main {
public static void main(String... args) {
final Path zipFile = Paths.get(args[0]);
System.out.println("Reading zip-file: " + zipFile);
final URI uri = URI.create("zip:file:" + zipFile.toUri().getPath().replace(" ", "%20"));
try (final FileSystem fs = FileSystems.newFileSystem(uri, singletonMap("create", "true"))) {
final long entriesRead = StreamSupport.stream(fs.getRootDirectories().spliterator(), false)
.flatMap(root -> {
try {
return Files.walk(root);
} catch (final IOException ex) {
throw new RuntimeException(format(
"Error traversing zip file system '%s', root: '%s'.",
zipFile, root), ex);
}
}).mapToLong(file -> {
try {
Files.lines(file).forEachOrdered(System.out::println);
return 1;
} catch (final IOException ex) {
throw new RuntimeException(format(
"Error modifying DAE-file '%s' in zip file system '%s'.",
file, zipFile), ex);
}
}).sum();
System.out.format("A total of %,d entries read.%n", entriesRead);
} catch (final IOException ex) {
throw new RuntimeException(format(
"Error reading zip-file '%s'.", zipFile
), ex);
}
}
}
如何使用Java Nio API访问zip文件的文件系统?这在Java 8中使用过,但现在没有了?我的机器上不再安装JDK 8,但我尝试使用目标级别1.8编译代码,并在IDEA中运行它。同样的问题。不确定它是否与JDK 9相关。
FileSystem.newFileSystem
--ProviderNotFoundException
-如果未安装支持URI方案的提供程序,则它似乎与Java9I无关。我现在已在另一台机器上使用JDK 1.8.0121运行了它,并且我得到了相同的异常。它似乎与jdk9无关。对,没有jdk9特定的内容。代码需要更改为fs=FileSystems.newFileSystem(zipFile,ClassLoader.getSystemClassLoader())
或fs=FileSystems.newFileSystem(URI.create(“jar:+zipFile.toUri()),Map.of())代码>。