import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
}
}
- private static void copy(File file, ZipOutputStream zout) throws IOException {
+ public static void copy(File file, OutputStream out) throws IOException {
try (InputStream in = new FileInputStream(file)) {
- copy(in, zout);
+ copy(in, out);
}
}
extractZip(fis, dst);
}
}
-
+
+ public static void extractZip(File zipFile, File dst, Charset charset) throws IOException {
+ if (LOGGER.isTraceEnabled())
+ LOGGER.trace("Extracting zip "+zipFile);
+ try (FileInputStream fis = new FileInputStream(zipFile)) {
+ extractZip(fis, dst, charset);
+ }
+ }
/**
* Extract a zip file into a directory
*
* @throws IOException
*/
public static void extractZip(InputStream zipInput, File dst) throws IOException {
+ extractZip(zipInput, dst, StandardCharsets.UTF_8);
+ }
+
+ public static void extractZip(InputStream zipInput, File dst, Charset charset) throws IOException {
byte[] buf = new byte[8192];
- ZipInputStream zis = new ZipInputStream(zipInput);
+ ZipInputStream zis = new ZipInputStream(zipInput, charset);
ZipEntry entry;
entry = zis.getNextEntry();
deleteWithFilter(path, null);
}
+ /**
+ * Empties the specified directory but does not delete the directory itself.
+ * If a non-directory path is given, it is simply deleted.
+ *
+ * @param path
+ * @throws IOException
+ */
+ public static void emptyDirectory(Path path) throws IOException {
+ if (Files.isDirectory(path))
+ Files.walkFileTree(path, new EmptyDirectoryVisitor());
+ else
+ Files.deleteIfExists(path);
+ }
+
public static void copy(Path from, Path to) throws IOException {
Files.walkFileTree(from, new CopyDirectoriesVisitor(from, to));
}
if (filter != null && !filter.test(file)) {
return FileVisitResult.CONTINUE;
}
- if (Files.exists(file)) {
- Files.delete(file);
- }
- if (Files.exists(file)) {
- throw new IOException("Could not delete file " + file.toAbsolutePath().toString());
- }
+ Files.deleteIfExists(file);
return FileVisitResult.CONTINUE;
}
if (filter != null && !filter.test(dir)) {
return FileVisitResult.CONTINUE;
}
- if (Files.exists(dir)) {
- Files.delete(dir);
- }
- if (Files.exists(dir))
- throw new IOException("Could not delete file " + dir.toAbsolutePath().toString());
+ Files.deleteIfExists(dir);
return FileVisitResult.CONTINUE;
}
}
-
+
+ private static class EmptyDirectoryVisitor extends SimpleFileVisitor<Path> {
+ int depth = 0;
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ Files.deleteIfExists(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
+ ++depth;
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+ if (exc != null)
+ throw exc;
+ if (--depth > 0)
+ Files.deleteIfExists(dir);
+ return FileVisitResult.CONTINUE;
+ }
+ }
+
public static class CopyDirectoriesVisitor extends SimpleFileVisitor<Path> {
private final Path fromPath;