java-commons/src/main/java/de/nclazz/commons/io/FileUtils.java

132 lines
3.9 KiB
Java

package de.nclazz.commons.io;
import lombok.NonNull;
import lombok.SneakyThrows;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.Objects;
import java.util.function.Consumer;
public abstract class FileUtils {
private FileUtils() { /* no-op */ }
/**
* Deletes a directory recursively.
* @param dir Directory to delete
* @throws NullPointerException if {@code dir} is {@code null}
* @throws IllegalArgumentException if {@code dir} is not a directory
*/
public static void deleteDirectoryRecursively(@NonNull File dir) {
if(!dir.exists()) return;
if(!dir.isDirectory()) {
throw new IllegalArgumentException(String.format("File '%s' is not a directory!", dir.getAbsolutePath()));
}
for(File file : Objects.requireNonNull(dir.listFiles())) {
if(file.isDirectory()) {
deleteDirectoryRecursively(file);
}else {
file.delete();
}
}
dir.delete();
}
/**
* Creates a temporary directory and passes it to {@code dirConsumer}.
* After the consumer returns the directory will be deleted.
*
* Attention: never store the given {@code File} instance in an external
* variable, as it will definately be deleted, after the {@code Consumer} returns!
* @param dirConsumer
*/
@SneakyThrows
public static void withTempDirectory(String prefix, @NonNull Consumer<File> dirConsumer) {
Path path = Files.createTempDirectory(prefix);
File dir = path.toFile();
dirConsumer.accept(dir);
if(dir.exists()) {
deleteDirectoryRecursively(dir);
}
}
public static void withTempDirectory(Consumer<File> dirConsumer) {
withTempDirectory("nclazz", dirConsumer);
}
/**
* Creates a temporary file and passes it to {@code fileConsumer}.
* After the consumer returns the directory will be deleted.
*
* Attention: never store the given {@code File} instance in an external
* variable, as it will definitely be deleted, after the {@code Consumer} returns!
* @param fileConsumer
*/
@SneakyThrows
public static void withTempFile(String prefix, String suffix, @NonNull Consumer<File> fileConsumer) {
Path path = Files.createTempFile(prefix, suffix);
File file = path.toFile();
fileConsumer.accept(file);
if(file.exists()) {
file.delete();
}
}
/**
* Creates the specified file and passes a {@link FileWriter} to the
* {@code Consumer} to write the file content.
*
* @param file
* @param writerConsumer
* @return created File
*/
@SneakyThrows
public static File writeFile(File file, Consumer<Writer> writerConsumer) {
if(!file.exists()) file.createNewFile();
try(FileWriter writer = new FileWriter(file)) {
writerConsumer.accept(writer);
}catch(Exception e) {
throw new RuntimeException(e);
}
return file;
}
/**
* Creates a hash from the file content of given file with given{@link MessageDigest}
* @param file to hash
* @param md hash algorithm
* @return hex {@code String} hash
*/
public static String digestFileContent(@NonNull File file, @NonNull MessageDigest md) {
try(FileInputStream stream = new FileInputStream(file)) {
return StreamUtils.digestStream(stream, md);
}catch(Exception e) {
throw new RuntimeException(e);
}
}
@SneakyThrows
public static String generateSHA1FromFile(File file) {
return digestFileContent(file, MessageDigest.getInstance("SHA-1"));
}
@SneakyThrows
public static String generateMD5FromFile(File file) {
return digestFileContent(file, MessageDigest.getInstance("MD5"));
}
}