slowly moving to the new file handling api (File -> Path)

2.x-1.17
Szum123321 2022-06-17 21:15:50 +02:00
parent a4bf645ef7
commit 6897b94afc
3 changed files with 34 additions and 36 deletions

View File

@ -32,6 +32,8 @@ import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecomp
import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
public class RestoreBackupRunnable implements Runnable {
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
@ -63,21 +65,21 @@ public class RestoreBackupRunnable implements Runnable {
).run();
}
File worldFile = Utilities.getWorldFolder(ctx.getServer());
Path worldFile = Utilities.getWorldFolder(ctx.getServer()).toPath();
log.info("Deleting old world...");
if(!deleteDirectory(worldFile))
log.error("Something went wrong while deleting old world!");
worldFile.mkdirs();
Files.createDirectories(worldFile);
log.info("Starting decompression...");
if(ctx.getFile().getArchiveFormat() == ConfigPOJO.ArchiveFormat.ZIP)
ZipDecompressor.decompress(ctx.getFile().getFile(), worldFile);
ZipDecompressor.decompress(ctx.getFile().getFile().toPath(), worldFile);
else
GenericTarDecompressor.decompress(ctx.getFile().getFile(), worldFile);
GenericTarDecompressor.decompress(ctx.getFile().getFile().toPath(), worldFile);
if(config.get().deleteOldBackupAfterRestore) {
log.info("Deleting old backup");

View File

@ -29,16 +29,17 @@ import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
public class GenericTarDecompressor {
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
public static void decompress(File input, File target) {
public static void decompress(Path input, Path target) {
Instant start = Instant.now();
try (InputStream fileInputStream = new FileInputStream(input);
try (InputStream fileInputStream = Files.newInputStream(input);
InputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
InputStream compressorInputStream = getCompressorInputStream(bufferedInputStream);
TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(compressorInputStream)) {
@ -50,22 +51,17 @@ public class GenericTarDecompressor {
continue;
}
File file = target.toPath().resolve(entry.getName()).toFile();
Path file = target.resolve(entry.getName());
if(entry.isDirectory()) {
file.mkdirs();
Files.createDirectories(file);
} else {
File parent = file.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
log.error("Failed to create {}", parent);
} else {
try (OutputStream outputStream = Files.newOutputStream(file.toPath());
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
IOUtils.copy(archiveInputStream, bufferedOutputStream);
} catch (IOException e) {
log.error("An exception occurred while trying to decompress file: {}", file.getName(), e);
}
Files.createDirectories(file.getParent());
try (OutputStream outputStream = Files.newOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
IOUtils.copy(archiveInputStream, bufferedOutputStream);
} catch (IOException e) {
log.error("An exception occurred while trying to decompress file: {}", file.getFileName().toString(), e);
}
}
}

View File

@ -21,41 +21,41 @@ package net.szum123321.textile_backup.core.restore.decompressors;
import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.TextileLogger;
import net.szum123321.textile_backup.core.Utilities;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.Iterator;
public class ZipDecompressor {
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
public static void decompress(File inputFile, File target) {
public static void decompress(Path inputFile, Path target) {
Instant start = Instant.now();
try(ZipFile zipFile = new ZipFile(inputFile)) {
zipFile.getEntries().asIterator().forEachRemaining(entry -> {
File file = target.toPath().resolve(entry.getName()).toFile();
try(ZipFile zipFile = new ZipFile(inputFile.toFile())) {
for (Iterator<ZipArchiveEntry> it = zipFile.getEntries().asIterator(); it.hasNext(); ) {
ZipArchiveEntry entry = it.next();
Path file = target.resolve(entry.getName());
if(entry.isDirectory()) {
file.mkdirs();
Files.createDirectories(file);
} else {
File parent = file.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
log.error("Failed to create {}", parent);
} else {
try (OutputStream outputStream = Files.newOutputStream(file.toPath());
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
IOUtils.copy(zipFile.getInputStream(entry), bufferedOutputStream);
} catch (IOException e) {
log.error("An exception occurred while trying to decompress file: {}", entry.getName(), e);
}
Files.createDirectories(file.getParent());
try (OutputStream outputStream = Files.newOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
IOUtils.copy(zipFile.getInputStream(entry), bufferedOutputStream);
} catch (IOException e) {
log.error("An exception occurred while trying to decompress file: {}", entry.getName(), e);
}
}
});
}
} catch (IOException e) {
log.error("An exception occurred! ", e);
}