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

View File

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

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