com.github.stormbit.packer.PackerFileUtils Maven / Gradle / Ivy
The newest version!
package com.github.stormbit.packer;
import org.zeroturnaround.zip.commons.FileUtilsV2_2;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
class PackerFileUtils
{
static void mkdirs(final File path) throws IOException {
if (!path.mkdirs()) {
throw new IOException("Can't create folder(s): " + path);
}
}
static void chmodX(final File path) {
if (!path.setExecutable(true)) {
System.err.println("Warning! Failed setting executable flag for: " + path);
}
}
static void delete(final File path) throws IOException {
if (!path.delete()) {
throw new IOException("Can't delete file or folder: " + path);
}
}
static void copyDirectory(final File sourceDirectory, final File targetDirectory) throws IOException {
final Path sourcePath = Paths.get(sourceDirectory.toURI()).toRealPath(new LinkOption[0]);
final Path targetPath = Paths.get(targetDirectory.toURI());
Files.walkFileTree(sourcePath, new SimpleFileVisitor() {
@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
final Path relative = sourcePath.relativize(dir);
final Path target = targetPath.resolve(relative);
final File folder = target.toFile();
if (!folder.exists()) {
PackerFileUtils.mkdirs(folder);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
final Path relative = sourcePath.relativize(file);
final Path target = targetPath.resolve(relative);
Files.copy(file, target, StandardCopyOption.COPY_ATTRIBUTES);
return FileVisitResult.CONTINUE;
}
});
}
static void deleteDirectory(final File directory) throws IOException {
FileUtilsV2_2.deleteDirectory(directory);
}
static void copyFile(final File source, final File target) throws IOException {
final Path sourcePath = Paths.get(source.toURI());
final Path targetPath = Paths.get(target.toURI());
Files.copy(sourcePath, targetPath, StandardCopyOption.COPY_ATTRIBUTES);
}
}