1 package fr.ifremer.tutti.ui.swing.updater;
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 import java.io.IOException;
28 import java.nio.file.FileVisitResult;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.PathMatcher;
32 import java.nio.file.SimpleFileVisitor;
33 import java.nio.file.attribute.BasicFileAttributes;
34
35
36
37
38
39
40
41 public class DeleteHelper {
42
43 public static void deleteDirectories(Path path, String glob) throws IOException {
44
45 PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob);
46 if (Files.isDirectory(path)) {
47
48 DeleteDirectories deleteDirectories = new DeleteDirectories(matcher);
49 Files.walkFileTree(path, deleteDirectories);
50 }
51
52 }
53
54 public static void deleteFiles(Path path, String glob) throws IOException {
55 if (Files.isDirectory(path)) {
56
57 PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob);
58 DeleteFiles deleteFiles = new DeleteFiles(matcher);
59 Files.walkFileTree(path, deleteFiles);
60 }
61
62 }
63
64 public static void deleteDirectory(Path path) throws IOException {
65 if (Files.isDirectory(path)) {
66 DeleteDirectory deleteDirectory = new DeleteDirectory();
67 Files.walkFileTree(path, deleteDirectory);
68 }
69 }
70
71 public static void deleteDirectoryOnExit(Path path) throws IOException {
72 if (Files.isDirectory(path)) {
73 DeleteDirectoryOnExit deleteDirectory = new DeleteDirectoryOnExit();
74 Files.walkFileTree(path, deleteDirectory);
75 }
76 }
77
78
79
80
81 public static class DeleteDirectory extends SimpleFileVisitor<Path> {
82
83 @Override
84 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
85 Files.deleteIfExists(file);
86 return FileVisitResult.CONTINUE;
87 }
88
89 @Override
90 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
91 System.out.println("Delete directory: " + dir);
92 Files.deleteIfExists(dir);
93 return FileVisitResult.CONTINUE;
94 }
95
96 }
97
98
99
100
101 public static class DeleteDirectoryOnExit extends SimpleFileVisitor<Path> {
102
103 @Override
104 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
105 System.out.println("Delete directory on exit: " + dir);
106 dir.toFile().deleteOnExit();
107 return FileVisitResult.CONTINUE;
108 }
109
110 @Override
111 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
112 file.toFile().deleteOnExit();
113 return FileVisitResult.CONTINUE;
114 }
115
116 }
117
118
119
120
121 public static class DeleteFiles extends SimpleFileVisitor<Path> {
122
123 private final PathMatcher matcher;
124
125 protected DeleteFiles(PathMatcher matcher) {
126 this.matcher = matcher;
127 }
128
129 @Override
130 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
131
132
133 if (matcher.matches(file.getFileName())) {
134 System.out.println("Delete file: " + file);
135 Files.deleteIfExists(file);
136 }
137 return FileVisitResult.CONTINUE;
138 }
139
140 }
141
142
143
144
145 public static class DeleteDirectories extends SimpleFileVisitor<Path> {
146
147 private final PathMatcher matcher;
148
149 protected DeleteDirectories(PathMatcher matcher) {
150 this.matcher = matcher;
151 }
152
153 private Path deleteDir;
154
155 @Override
156 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
157
158 if (deleteDir == null) {
159 if (matcher.matches(dir.getFileName())) {
160 System.out.println("Delete directory: " + dir);
161 deleteDir = dir;
162 }
163 }
164 return FileVisitResult.CONTINUE;
165
166 }
167
168 @Override
169 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
170
171 if (deleteDir != null) {
172 Files.deleteIfExists(file);
173 }
174 return FileVisitResult.CONTINUE;
175
176 }
177
178 @Override
179 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
180
181 if (deleteDir != null) {
182
183 Files.deleteIfExists(dir);
184
185 if (dir.equals(deleteDir)) {
186 deleteDir = null;
187 }
188
189 }
190
191 return FileVisitResult.CONTINUE;
192
193 }
194
195 }
196 }