我在使用 java nio 遍历 zip 文件的 VFS(虚拟文件系统)的文件树时遇到异常,这是我的代码:
public static void list(String zipFilePath) throws IOException{
FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFilePath), null);
Path startPath = fs.getPath("/");
Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("Dir : " + dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("\t->File : " + file);
return FileVisitResult.CONTINUE;
}
});
}
异常表示文件不存在,但我已经解压缩存档(zip),并且文件在那里......知道为什么吗?
这是一个例外:
java.nio.file.NoSuchFileException: /Dir1/Dir2/Dir3/Dir4/ExcelFile.xlsm
at com.sun.nio.zipfs.ZipPath.getAttributes(ZipPath.java:657)
at com.sun.nio.zipfs.ZipFileSystemProvider.readAttributes(ZipFileSystemProvider.java:285)
at java.nio.file.Files.readAttributes(Files.java:1669)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:105)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:69)
at java.nio.file.Files.walkFileTree(Files.java:2585)
at java.nio.file.Files.walkFileTree(Files.java:2618)
顺便说一句:代码确实有效,但它与某些文件崩溃