Decompressors now take File not FileInputStream
GenericTarDecompressor now guesses correct decompressor2.x-1.16
parent
7b6531185d
commit
67fe75667b
|
@ -19,6 +19,7 @@
|
||||||
package net.szum123321.textile_backup.core.restore;
|
package net.szum123321.textile_backup.core.restore;
|
||||||
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.szum123321.textile_backup.ConfigHandler;
|
||||||
import net.szum123321.textile_backup.core.LivingServer;
|
import net.szum123321.textile_backup.core.LivingServer;
|
||||||
import net.szum123321.textile_backup.Statics;
|
import net.szum123321.textile_backup.Statics;
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
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.create.BackupHelper;
|
||||||
import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecompressor;
|
import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecompressor;
|
||||||
import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
|
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.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
public class RestoreBackupRunnable implements Runnable {
|
public class RestoreBackupRunnable implements Runnable {
|
||||||
|
@ -70,31 +66,15 @@ public class RestoreBackupRunnable implements Runnable {
|
||||||
|
|
||||||
worldFile.mkdirs();
|
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!"))) {
|
if(Utilities.getFileExtension(backupFile).orElseThrow(() -> new NoSuchElementException("Couldn't get file extension!")) == ConfigHandler.ArchiveFormat.ZIP) {
|
||||||
case ZIP:
|
ZipDecompressor.decompress(backupFile, worldFile);
|
||||||
ZipDecompressor.decompress(fileInputStream, worldFile);
|
} else {
|
||||||
break;
|
GenericTarDecompressor.decompress(backupFile, worldFile);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Statics.LOGGER.info("Done.");
|
Statics.LOGGER.info("Done!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void awaitServerShutdown() {
|
private void awaitServerShutdown() {
|
||||||
|
|
|
@ -22,21 +22,23 @@ import net.szum123321.textile_backup.Statics;
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
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.CompressorInputStream;
|
||||||
|
import org.apache.commons.compress.compressors.CompressorStreamFactory;
|
||||||
import org.apache.commons.compress.utils.IOUtils;
|
import org.apache.commons.compress.utils.IOUtils;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public class GenericTarDecompressor {
|
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();
|
Instant start = Instant.now();
|
||||||
|
|
||||||
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
try (FileInputStream fileInputStream = new FileInputStream(input);
|
||||||
CompressorInputStream compressorInputStream = DecompressorStream.getDeclaredConstructor(InputStream.class).newInstance(bufferedInputStream);
|
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
||||||
|
CompressorInputStream compressorInputStream = new CompressorStreamFactory().createCompressorInputStream(bufferedInputStream);
|
||||||
TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(compressorInputStream)) {
|
TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(compressorInputStream)) {
|
||||||
TarArchiveEntry entry;
|
TarArchiveEntry entry;
|
||||||
|
|
||||||
|
@ -53,9 +55,9 @@ public class GenericTarDecompressor {
|
||||||
} else {
|
} else {
|
||||||
File parent = file.getParentFile();
|
File parent = file.getParentFile();
|
||||||
|
|
||||||
if (!parent.isDirectory() && !parent.mkdirs())
|
if (!parent.isDirectory() && !parent.mkdirs()) {
|
||||||
throw new IOException("Failed to create directory " + parent);
|
Statics.LOGGER.error("Failed to create {}", parent);
|
||||||
|
} else {
|
||||||
try (OutputStream outputStream = Files.newOutputStream(file.toPath());
|
try (OutputStream outputStream = Files.newOutputStream(file.toPath());
|
||||||
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
|
||||||
IOUtils.copy(archiveInputStream, bufferedOutputStream);
|
IOUtils.copy(archiveInputStream, bufferedOutputStream);
|
||||||
|
@ -64,7 +66,8 @@ public class GenericTarDecompressor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
|
}
|
||||||
|
} catch (IOException | CompressorException e) {
|
||||||
Statics.LOGGER.error("An exception occurred! ", e);
|
Statics.LOGGER.error("An exception occurred! ", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,10 +30,11 @@ import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public class ZipDecompressor {
|
public class ZipDecompressor {
|
||||||
public static void decompress(FileInputStream fileInputStream, File target) {
|
public static void decompress(File inputFile, File target) {
|
||||||
Instant start = Instant.now();
|
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))) {
|
ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream((bufferedInputStream))) {
|
||||||
ZipArchiveEntry entry;
|
ZipArchiveEntry entry;
|
||||||
|
|
||||||
|
@ -65,6 +66,6 @@ public class ZipDecompressor {
|
||||||
Statics.LOGGER.error("An exception occurred! ", e);
|
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