I am trying to use the java.nio-API for traversing a .zip-file, but I get a ProviderNotFoundException
when I try to call FileSystems.newFileSystem
.
I have also tried changing the zip:file:
to jar:file:
, but I get the same kind of exception except that the message says Provider "jar" not found
.
I have also tried using FileSystems.newFileSystem(Path, null)
directly without creating an URI
first.
Output:
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);
}
}
}
How do I get access to the file system of a zip-file with the Java Nio-APIs?
FileSystem.newFileSystem
--ProviderNotFoundException
- if a provider supporting the URI scheme is not installed seems not pertaining to Java9 – Naman Nov 21 '17 at 13:28fs = FileSystems.newFileSystem(zipFile, ClassLoader.getSystemClassLoader())
orfs = FileSystems.newFileSystem(URI.create("jar:" + zipFile.toUri()), Map.of());
. – Alan Bateman Nov 21 '17 at 13:35