-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathjava-file-zip.java
More file actions
45 lines (39 loc) · 1.35 KB
/
java-file-zip.java
File metadata and controls
45 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
--------------------------------
关于zip的操作 |
--------------------------------
# FileSystems 静态类获取zip文件系统对象
FileSystem FileSystems.newFileSystem(Path path, ClassLoader loader);
* 使用 FileSystems 工具类,创建压缩包的path创建 FileSystem 对象
* 该对象包含了压缩文件的所有文件
# Path 对象实例获取zip文件系统对象
FileSystem getFileSystem();
* 获取文件系统对象
--------------------------------
读取内部文件 |
--------------------------------
Path fileSystem.getPath(String filePath);
* 文件相对路径
* "/"表示压缩包的根路径
--------------------------------
遍历zip文件 |
--------------------------------
Files.walkFileTree(fileSystem.getPath("/"), new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println(dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
System.out.println(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});