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

This commit is contained in:
Szum123321 2022-06-17 21:33:13 +02:00
parent 6897b94afc
commit a85db99d82
5 changed files with 49 additions and 44 deletions

View File

@ -30,9 +30,10 @@ import net.szum123321.textile_backup.core.create.compressors.tar.ParallelBZip2Co
import net.szum123321.textile_backup.core.create.compressors.tar.ParallelGzipCompressor; import net.szum123321.textile_backup.core.create.compressors.tar.ParallelGzipCompressor;
import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream; import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
public class MakeBackupRunnable implements Runnable { public class MakeBackupRunnable implements Runnable {
@ -55,22 +56,22 @@ public class MakeBackupRunnable implements Runnable {
log.sendInfoAL(context, "Starting backup"); log.sendInfoAL(context, "Starting backup");
File world = Utilities.getWorldFolder(context.getServer()); Path world = Utilities.getWorldFolder(context.getServer()).toPath();
log.trace("Minecraft world is: {}", world); log.trace("Minecraft world is: {}", world);
File outFile = Utilities Path outFile = Utilities
.getBackupRootPath(Utilities.getLevelName(context.getServer())) .getBackupRootPath(Utilities.getLevelName(context.getServer()))
.toPath() .toPath()
.resolve(getFileName()) .resolve(getFileName());
.toFile();
log.trace("Outfile is: {}", outFile); log.trace("Outfile is: {}", outFile);
outFile.getParentFile().mkdirs(); // outFile.getParentFile().mkdirs();
try { try {
outFile.createNewFile(); //outFile.createNewFile();
Files.createDirectories(outFile.getParent());
Files.createFile(outFile);
} catch (IOException e) { } catch (IOException e) {
log.error("An exception occurred when trying to create new backup file!", e); log.error("An exception occurred when trying to create new backup file!", e);

View File

@ -31,27 +31,27 @@ import java.nio.file.Path;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;
public abstract class AbstractCompressor { public abstract class AbstractCompressor {
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME); private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
public void createArchive(File inputFile, File outputFile, BackupContext ctx, int coreLimit) { public void createArchive(Path inputFile, Path outputFile, BackupContext ctx, int coreLimit) {
Instant start = Instant.now(); Instant start = Instant.now();
try (FileOutputStream outStream = new FileOutputStream(outputFile); try (OutputStream outStream = Files.newOutputStream(outputFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outStream);
OutputStream arc = createArchiveOutputStream(bufferedOutputStream, ctx, coreLimit)) { OutputStream arc = createArchiveOutputStream(bufferedOutputStream, ctx, coreLimit);
Stream<Path> fileStream = Files.walk(inputFile)) {
Files.walk(inputFile.toPath()) fileStream
.filter(path -> !Utilities.isBlacklisted(inputFile.toPath().relativize(path))) .filter(path -> !Utilities.isBlacklisted(inputFile.relativize(path)))
.map(Path::toFile) .filter(Files::isRegularFile).forEach(file -> {
.filter(File::isFile)
.forEach(file -> {
try { try {
//hopefully one broken file won't spoil the whole archive //hopefully one broken file won't spoil the whole archive
addEntry(file, inputFile.toPath().relativize(file.toPath()).toString(), arc); addEntry(file, inputFile.relativize(file).toString(), arc);
} catch (IOException e) { } catch (IOException e) {
log.error("An exception occurred while trying to compress: {}", inputFile.toPath().relativize(file.toPath()).toString(), e); log.error("An exception occurred while trying to compress: {}", inputFile.relativize(file).toString(), e);
if (ctx.getInitiator() == ActionInitiator.Player) if (ctx.getInitiator() == ActionInitiator.Player)
log.sendError(ctx, "Something went wrong while compressing files!"); log.sendError(ctx, "Something went wrong while compressing files!");
@ -86,13 +86,13 @@ public abstract class AbstractCompressor {
} }
protected abstract OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException; protected abstract OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException;
protected abstract void addEntry(File file, String entryName, OutputStream arc) throws IOException; protected abstract void addEntry(Path file, String entryName, OutputStream arc) throws IOException;
protected void finish(OutputStream arc) throws InterruptedException, ExecutionException, IOException { protected void finish(OutputStream arc) throws InterruptedException, ExecutionException, IOException {
;//Basically this function is only needed for the ParallelZipCompressor to write out ParallelScatterZipCreator //Basically this function is only needed for the ParallelZipCompressor to write out ParallelScatterZipCreator
} }
protected void close() { protected void close() {
;//Same as above, just for ParallelGzipCompressor to shutdown ExecutorService //Same as above, just for ParallelGzipCompressor to shut down ExecutorService
} }
} }

View File

@ -26,6 +26,8 @@ import org.apache.commons.compress.archivers.zip.*;
import org.apache.commons.compress.parallel.InputStreamSupplier; import org.apache.commons.compress.parallel.InputStreamSupplier;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
@ -65,13 +67,13 @@ public class ParallelZipCompressor extends ZipCompressor {
} }
@Override @Override
protected void addEntry(File file, String entryName, OutputStream arc) throws IOException { protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException {
ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(file, entryName); ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(file, entryName);
if(ZipCompressor.isDotDat(file.getName())) { if(ZipCompressor.isDotDat(file.getFileName().toString())) {
entry.setMethod(ZipArchiveOutputStream.STORED); entry.setMethod(ZipEntry.STORED);
entry.setSize(file.length()); entry.setSize(Files.size(file));
entry.setCompressedSize(file.length()); entry.setCompressedSize(Files.size(file));
entry.setCrc(getCRC(file)); entry.setCrc(getCRC(file));
} else entry.setMethod(ZipEntry.DEFLATED); } else entry.setMethod(ZipEntry.DEFLATED);
@ -126,12 +128,12 @@ public class ParallelZipCompressor extends ZipCompressor {
} }
} }
record FileInputStreamSupplier(File sourceFile) implements InputStreamSupplier { record FileInputStreamSupplier(Path sourceFile) implements InputStreamSupplier {
public InputStream get() { public InputStream get() {
try { try {
return new FileInputStream(sourceFile); return Files.newInputStream(sourceFile);
} catch (IOException e) { } catch (IOException e) {
log.error("An exception occurred while trying to create an input stream from file: {}!", sourceFile.getName(), e); log.error("An exception occurred while trying to create an input stream from file: {}!", sourceFile.toString(), e);
} }
return null; return null;

View File

@ -27,9 +27,12 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
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.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.zip.CRC32; import java.util.zip.CRC32;
import java.util.zip.Checksum; import java.util.zip.Checksum;
import java.util.zip.ZipEntry;
public class ZipCompressor extends AbstractCompressor { public class ZipCompressor extends AbstractCompressor {
private final static ConfigHelper config = ConfigHelper.INSTANCE; private final static ConfigHelper config = ConfigHelper.INSTANCE;
@ -51,14 +54,14 @@ public class ZipCompressor extends AbstractCompressor {
} }
@Override @Override
protected void addEntry(File file, String entryName, OutputStream arc) throws IOException { protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(file)){ try (InputStream fileInputStream = Files.newInputStream(file)){
ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(file, entryName); ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(file, entryName);
if(isDotDat(file.getName())) { if(isDotDat(file.getFileName().toString())) {
entry.setMethod(ZipArchiveOutputStream.STORED); entry.setMethod(ZipEntry.STORED);
entry.setSize(file.length()); entry.setSize(Files.size(file));
entry.setCompressedSize(file.length()); entry.setCompressedSize(Files.size(file));
entry.setCrc(getCRC(file)); entry.setCrc(getCRC(file));
} }
@ -76,15 +79,15 @@ public class ZipCompressor extends AbstractCompressor {
return arr[arr.length - 1].contains("dat"); //includes dat_old return arr[arr.length - 1].contains("dat"); //includes dat_old
} }
protected static long getCRC(File file) throws IOException { protected static long getCRC(Path file) throws IOException {
Checksum sum = new CRC32(); Checksum sum = new CRC32();
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
int len; int len;
try (InputStream stream = new FileInputStream(file)) { try (InputStream stream = Files.newInputStream(file)) {
while ((len = stream.read(buffer)) != -1) sum.update(buffer, 0, len); while ((len = stream.read(buffer)) != -1) sum.update(buffer, 0, len);
} catch (IOException e) { } catch (IOException e) {
throw new IOException("Error while calculating CRC of: " + file.getAbsolutePath(), e); throw new IOException("Error while calculating CRC of: " + file.toAbsolutePath(), e);
} }
return sum.getValue(); return sum.getValue();

View File

@ -24,10 +24,9 @@ import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.IOUtils;
import java.io.File; import java.io.*;
import java.io.FileInputStream; import java.nio.file.Files;
import java.io.IOException; import java.nio.file.Path;
import java.io.OutputStream;
public class AbstractTarArchiver extends AbstractCompressor { public class AbstractTarArchiver extends AbstractCompressor {
protected OutputStream getCompressorOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException { protected OutputStream getCompressorOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException {
@ -44,8 +43,8 @@ public class AbstractTarArchiver extends AbstractCompressor {
} }
@Override @Override
protected void addEntry(File file, String entryName, OutputStream arc) throws IOException { protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(file)){ try (InputStream fileInputStream = Files.newInputStream(file)){
TarArchiveEntry entry = (TarArchiveEntry)((TarArchiveOutputStream) arc).createArchiveEntry(file, entryName); TarArchiveEntry entry = (TarArchiveEntry)((TarArchiveOutputStream) arc).createArchiveEntry(file, entryName);
((TarArchiveOutputStream)arc).putArchiveEntry(entry); ((TarArchiveOutputStream)arc).putArchiveEntry(entry);