Decompressors now take File not FileInputStream
GenericTarDecompressor now guesses correct decompressor
This commit is contained in:
parent
7b6531185d
commit
67fe75667b
@ -19,6 +19,7 @@
|
||||
package net.szum123321.textile_backup.core.restore;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.szum123321.textile_backup.ConfigHandler;
|
||||
import net.szum123321.textile_backup.core.LivingServer;
|
||||
import net.szum123321.textile_backup.Statics;
|
||||
import net.szum123321.textile_backup.core.Utilities;
|
||||
@ -26,13 +27,8 @@ import net.szum123321.textile_backup.core.create.BackupContext;
|
||||
import net.szum123321.textile_backup.core.create.BackupHelper;
|
||||
import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecompressor;
|
||||
import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
|
||||
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
|
||||
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
|
||||
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class RestoreBackupRunnable implements Runnable {
|
||||
@ -70,31 +66,15 @@ public class RestoreBackupRunnable implements Runnable {
|
||||
|
||||
worldFile.mkdirs();
|
||||
|
||||
try(FileInputStream fileInputStream = new FileInputStream(backupFile)) {
|
||||
Statics.LOGGER.info("Starting decompression...");
|
||||
Statics.LOGGER.info("Starting decompression...");
|
||||
|
||||
switch(Utilities.getFileExtension(backupFile).orElseThrow(() -> new NoSuchElementException("Couldn't get file extention!"))) {
|
||||
case ZIP:
|
||||
ZipDecompressor.decompress(fileInputStream, worldFile);
|
||||
break;
|
||||
|
||||
case GZIP:
|
||||
GenericTarDecompressor.decompress(fileInputStream, worldFile, GzipCompressorInputStream.class);
|
||||
break;
|
||||
|
||||
case BZIP2:
|
||||
GenericTarDecompressor.decompress(fileInputStream, worldFile, BZip2CompressorInputStream.class);
|
||||
break;
|
||||
|
||||
case LZMA:
|
||||
GenericTarDecompressor.decompress(fileInputStream, worldFile, XZCompressorInputStream.class);
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Statics.LOGGER.error("Exception occurred!", e);
|
||||
if(Utilities.getFileExtension(backupFile).orElseThrow(() -> new NoSuchElementException("Couldn't get file extension!")) == ConfigHandler.ArchiveFormat.ZIP) {
|
||||
ZipDecompressor.decompress(backupFile, worldFile);
|
||||
} else {
|
||||
GenericTarDecompressor.decompress(backupFile, worldFile);
|
||||
}
|
||||
|
||||
Statics.LOGGER.info("Done.");
|
||||
Statics.LOGGER.info("Done!");
|
||||
}
|
||||
|
||||
private void awaitServerShutdown() {
|
||||
|
@ -22,21 +22,23 @@ import net.szum123321.textile_backup.Statics;
|
||||
import net.szum123321.textile_backup.core.Utilities;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||
import org.apache.commons.compress.compressors.CompressorException;
|
||||
import org.apache.commons.compress.compressors.CompressorInputStream;
|
||||
import org.apache.commons.compress.compressors.CompressorStreamFactory;
|
||||
import org.apache.commons.compress.utils.IOUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.file.Files;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
public class GenericTarDecompressor {
|
||||
public static void decompress(FileInputStream fileInputStream, File target, Class<? extends CompressorInputStream> DecompressorStream) {
|
||||
public static void decompress(File input, File target) {
|
||||
Instant start = Instant.now();
|
||||
|
||||
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
||||
CompressorInputStream compressorInputStream = DecompressorStream.getDeclaredConstructor(InputStream.class).newInstance(bufferedInputStream);
|
||||
try (FileInputStream fileInputStream = new FileInputStream(input);
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
||||
CompressorInputStream compressorInputStream = new CompressorStreamFactory().createCompressorInputStream(bufferedInputStream);
|
||||
TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(compressorInputStream)) {
|
||||
TarArchiveEntry entry;
|
||||
|
||||
@ -53,18 +55,19 @@ public class GenericTarDecompressor {
|
||||
} else {
|
||||
File parent = file.getParentFile();
|
||||
|
||||
if (!parent.isDirectory() && !parent.mkdirs())
|
||||
throw new IOException("Failed to create directory " + parent);
|
||||
|
||||
try (OutputStream outputStream = Files.newOutputStream(file.toPath());
|
||||
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
|
||||
IOUtils.copy(archiveInputStream, bufferedOutputStream);
|
||||
} catch (IOException e) {
|
||||
Statics.LOGGER.error("An exception occurred while trying to decompress file: {}", file.getName(), e);
|
||||
if (!parent.isDirectory() && !parent.mkdirs()) {
|
||||
Statics.LOGGER.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) {
|
||||
Statics.LOGGER.error("An exception occurred while trying to decompress file: {}", file.getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
|
||||
} catch (IOException | CompressorException e) {
|
||||
Statics.LOGGER.error("An exception occurred! ", e);
|
||||
}
|
||||
|
||||
|
@ -30,10 +30,11 @@ import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
public class ZipDecompressor {
|
||||
public static void decompress(FileInputStream fileInputStream, File target) {
|
||||
public static void decompress(File inputFile, File target) {
|
||||
Instant start = Instant.now();
|
||||
|
||||
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
||||
try (FileInputStream fileInputStream = new FileInputStream(inputFile);
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
||||
ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream((bufferedInputStream))) {
|
||||
ZipArchiveEntry entry;
|
||||
|
||||
@ -65,6 +66,6 @@ public class ZipDecompressor {
|
||||
Statics.LOGGER.error("An exception occurred! ", e);
|
||||
}
|
||||
|
||||
Statics.LOGGER.info("Compression took: {} seconds.", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
||||
Statics.LOGGER.info("Decompression took: {} seconds.", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user