62
63 @Override
64 public FileVisitResult preVisitDirectory(Path dir,
65 BasicFileAttributes attr) throws IOException {
66 if (Platform.getPlatform() == Platform.WINDOWS) {
67 Files.setAttribute(dir, "dos:readonly", false);
68 }
69 return FileVisitResult.CONTINUE;
70 }
71
72 @Override
73 public FileVisitResult postVisitDirectory(Path dir, IOException e)
74 throws IOException {
75 Files.delete(dir);
76 return FileVisitResult.CONTINUE;
77 }
78 });
79 }
80
81 public static void copyRecursive(Path src, Path dest) throws IOException {
82 Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
83 @Override
84 public FileVisitResult preVisitDirectory(final Path dir,
85 final BasicFileAttributes attrs) throws IOException {
86 Files.createDirectories(dest.resolve(src.relativize(dir)));
87 return FileVisitResult.CONTINUE;
88 }
89
90 @Override
91 public FileVisitResult visitFile(final Path file,
92 final BasicFileAttributes attrs) throws IOException {
93 Files.copy(file, dest.resolve(src.relativize(file)));
94 return FileVisitResult.CONTINUE;
95 }
96 });
97 }
98
99 public static void copyRecursive(Path src, Path dest,
100 final List<String> excludes) throws IOException {
101 Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
102 @Override
103 public FileVisitResult preVisitDirectory(final Path dir,
104 final BasicFileAttributes attrs) throws IOException {
105 if (excludes.contains(dir.toFile().getName())) {
106 return FileVisitResult.SKIP_SUBTREE;
107 } else {
108 Files.createDirectories(dest.resolve(src.relativize(dir)));
109 return FileVisitResult.CONTINUE;
110 }
111 }
112
113 @Override
114 public FileVisitResult visitFile(final Path file,
115 final BasicFileAttributes attrs) throws IOException {
116 if (!excludes.contains(file.toFile().getName())) {
129 destFile.delete();
130 destFile.createNewFile();
131
132 try (FileChannel source = new FileInputStream(sourceFile).getChannel();
133 FileChannel destination =
134 new FileOutputStream(destFile).getChannel()) {
135
136 if (destination != null && source != null) {
137 destination.transferFrom(source, 0, source.size());
138 }
139 }
140
141 //preserve executable bit!
142 if (sourceFile.canExecute()) {
143 destFile.setExecutable(true, false);
144 }
145 if (!sourceFile.canWrite()) {
146 destFile.setReadOnly();
147 }
148 destFile.setReadable(true, false);
149 }
150
151 public static long getFolderSize(File folder) {
152 long foldersize = 0;
153
154 File[] children = folder.listFiles();
155 if (children != null) {
156 for (File f : children) {
157 if (f.isDirectory()) {
158 foldersize += getFolderSize(f);
159 } else {
160 foldersize += f.length();
161 }
162 }
163 }
164
165 return foldersize;
166 }
167
168 // run "launcher paramfile" in the directory where paramfile is kept
169 public static void run(String launcher, File paramFile)
170 throws IOException {
171 if (paramFile != null && paramFile.exists()) {
172 ProcessBuilder pb =
173 new ProcessBuilder(launcher, paramFile.getName());
174 pb = pb.directory(paramFile.getParentFile());
175 exec(pb);
176 }
177 }
178
179 public static void exec(ProcessBuilder pb)
180 throws IOException {
181 exec(pb, false, null);
182 }
183
184 public static void exec(ProcessBuilder pb, boolean testForPresenseOnly,
185 PrintStream consumer) throws IOException {
|
62
63 @Override
64 public FileVisitResult preVisitDirectory(Path dir,
65 BasicFileAttributes attr) throws IOException {
66 if (Platform.getPlatform() == Platform.WINDOWS) {
67 Files.setAttribute(dir, "dos:readonly", false);
68 }
69 return FileVisitResult.CONTINUE;
70 }
71
72 @Override
73 public FileVisitResult postVisitDirectory(Path dir, IOException e)
74 throws IOException {
75 Files.delete(dir);
76 return FileVisitResult.CONTINUE;
77 }
78 });
79 }
80
81 public static void copyRecursive(Path src, Path dest) throws IOException {
82 copyRecursive(src, dest, List.of());
83 }
84
85 public static void copyRecursive(Path src, Path dest,
86 final List<String> excludes) throws IOException {
87 Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
88 @Override
89 public FileVisitResult preVisitDirectory(final Path dir,
90 final BasicFileAttributes attrs) throws IOException {
91 if (excludes.contains(dir.toFile().getName())) {
92 return FileVisitResult.SKIP_SUBTREE;
93 } else {
94 Files.createDirectories(dest.resolve(src.relativize(dir)));
95 return FileVisitResult.CONTINUE;
96 }
97 }
98
99 @Override
100 public FileVisitResult visitFile(final Path file,
101 final BasicFileAttributes attrs) throws IOException {
102 if (!excludes.contains(file.toFile().getName())) {
115 destFile.delete();
116 destFile.createNewFile();
117
118 try (FileChannel source = new FileInputStream(sourceFile).getChannel();
119 FileChannel destination =
120 new FileOutputStream(destFile).getChannel()) {
121
122 if (destination != null && source != null) {
123 destination.transferFrom(source, 0, source.size());
124 }
125 }
126
127 //preserve executable bit!
128 if (sourceFile.canExecute()) {
129 destFile.setExecutable(true, false);
130 }
131 if (!sourceFile.canWrite()) {
132 destFile.setReadOnly();
133 }
134 destFile.setReadable(true, false);
135 }
136
137 // run "launcher paramfile" in the directory where paramfile is kept
138 public static void run(String launcher, File paramFile)
139 throws IOException {
140 if (paramFile != null && paramFile.exists()) {
141 ProcessBuilder pb =
142 new ProcessBuilder(launcher, paramFile.getName());
143 pb = pb.directory(paramFile.getParentFile());
144 exec(pb);
145 }
146 }
147
148 public static void exec(ProcessBuilder pb)
149 throws IOException {
150 exec(pb, false, null);
151 }
152
153 public static void exec(ProcessBuilder pb, boolean testForPresenseOnly,
154 PrintStream consumer) throws IOException {
|