From 300fe18b10c9163bbc2e7411bf786022d689511b Mon Sep 17 00:00:00 2001 From: Szum123321 Date: Sun, 27 Nov 2022 23:11:41 +0100 Subject: [PATCH] Added support of virtual file (in this case textile_status.data). --- .../core/create/FileInputStreamSupplier.java | 8 +++++-- .../core/create/InputSupplier.java | 8 ++++++- .../compressors/AbstractCompressor.java | 5 ++++- .../compressors/ParallelZipCompressor.java | 22 +++++++++++++------ .../create/compressors/ZipCompressor.java | 18 ++++++++++----- .../compressors/tar/AbstractTarArchiver.java | 12 +++++++--- 6 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/szum123321/textile_backup/core/create/FileInputStreamSupplier.java b/src/main/java/net/szum123321/textile_backup/core/create/FileInputStreamSupplier.java index 2e2ac40..30c2556 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/FileInputStreamSupplier.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/FileInputStreamSupplier.java @@ -28,6 +28,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Optional; public record FileInputStreamSupplier(Path path, String name, FileTreeHashBuilder hashTreeBuilder, BrokenFileHandler brokenFileHandler) implements InputSupplier { private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME); @@ -43,8 +44,11 @@ public record FileInputStreamSupplier(Path path, String name, FileTreeHashBuilde } @Override - public Path getPath() { - return path; + public Optional getPath() { return Optional.of(path); } + + @Override + public long size() throws IOException { + return Files.size(path); } @Override diff --git a/src/main/java/net/szum123321/textile_backup/core/create/InputSupplier.java b/src/main/java/net/szum123321/textile_backup/core/create/InputSupplier.java index 74960b2..6a5ec6a 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/InputSupplier.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/InputSupplier.java @@ -23,8 +23,14 @@ import org.apache.commons.compress.parallel.InputStreamSupplier; import java.io.IOException; import java.io.InputStream; import java.nio.file.Path; +import java.util.Optional; + + public interface InputSupplier extends InputStreamSupplier { InputStream getInputStream() throws IOException; - Path getPath(); + //If an entry is virtual (a.k.a there is no actual file to open, only input stream) + Optional getPath(); String getName(); + + long size() throws IOException; } diff --git a/src/main/java/net/szum123321/textile_backup/core/create/compressors/AbstractCompressor.java b/src/main/java/net/szum123321/textile_backup/core/create/compressors/AbstractCompressor.java index 38c742a..596ec48 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/compressors/AbstractCompressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/compressors/AbstractCompressor.java @@ -33,6 +33,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.time.Instant; +import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.stream.Stream; @@ -128,10 +129,12 @@ public abstract class AbstractCompressor { private record StatusFileInputSupplier(byte[] data) implements InputSupplier { public InputStream getInputStream() { return new ByteArrayInputStream(data); } - public Path getPath() { return Path.of(CompressionStatus.DATA_FILENAME); } + public Optional getPath() { return Optional.empty(); } public String getName() { return CompressionStatus.DATA_FILENAME; } + public long size() { return data.length; } + public InputStream get() { return getInputStream(); } } } diff --git a/src/main/java/net/szum123321/textile_backup/core/create/compressors/ParallelZipCompressor.java b/src/main/java/net/szum123321/textile_backup/core/create/compressors/ParallelZipCompressor.java index 8acf42f..3632179 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/compressors/ParallelZipCompressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/compressors/ParallelZipCompressor.java @@ -27,6 +27,7 @@ import org.apache.commons.compress.archivers.zip.*; import java.io.*; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Objects; import java.util.concurrent.*; import java.util.zip.ZipEntry; @@ -67,14 +68,21 @@ public class ParallelZipCompressor extends ZipCompressor { @Override protected void addEntry(InputSupplier input, OutputStream arc) throws IOException { - ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(input.getPath(), input.getName()); - - if(ZipCompressor.isDotDat(input.getPath().getFileName().toString())) { + ZipArchiveEntry entry; + if(input.getPath().isEmpty()) { + entry = new ZipArchiveEntry(input.getName()); entry.setMethod(ZipEntry.STORED); - entry.setSize(Files.size(input.getPath())); - entry.setCompressedSize(Files.size(input.getPath())); - entry.setCrc(getCRC(input.getPath())); - } else entry.setMethod(ZipEntry.DEFLATED); + entry.setSize(input.size()); + } else { + Path file = input.getPath().get(); + entry = (ZipArchiveEntry) ((ZipArchiveOutputStream) arc).createArchiveEntry(file, input.getName()); + if (ZipCompressor.isDotDat(file.toString())) { + entry.setMethod(ZipEntry.STORED); + entry.setSize(Files.size(file)); + entry.setCompressedSize(Files.size(file)); + entry.setCrc(getCRC(file)); + } else entry.setMethod(ZipEntry.DEFLATED); + } entry.setTime(System.currentTimeMillis()); diff --git a/src/main/java/net/szum123321/textile_backup/core/create/compressors/ZipCompressor.java b/src/main/java/net/szum123321/textile_backup/core/create/compressors/ZipCompressor.java index 5a87a97..5a39bab 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/compressors/ZipCompressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/compressors/ZipCompressor.java @@ -57,13 +57,21 @@ public class ZipCompressor extends AbstractCompressor { @Override protected void addEntry(InputSupplier input, OutputStream arc) throws IOException { try (InputStream fileInputStream = input.getInputStream()) { - ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(input.getPath(), input.getName()); + ZipArchiveEntry entry; - if(isDotDat(input.getPath().getFileName().toString())) { + if(input.getPath().isEmpty()) { + entry = new ZipArchiveEntry(input.getName()); entry.setMethod(ZipEntry.STORED); - entry.setSize(Files.size(input.getPath())); - entry.setCompressedSize(Files.size(input.getPath())); - entry.setCrc(getCRC(input.getPath())); + entry.setSize(input.size()); + } else { + Path file = input.getPath().get(); + entry = (ZipArchiveEntry) ((ZipArchiveOutputStream) arc).createArchiveEntry(file, input.getName()); + if (isDotDat(file.toString())) { + entry.setMethod(ZipEntry.STORED); + entry.setSize(Files.size(file)); + entry.setCompressedSize(Files.size(file)); + entry.setCrc(getCRC(file)); + } else entry.setMethod(ZipEntry.DEFLATED); } ((ZipArchiveOutputStream)arc).putArchiveEntry(entry); diff --git a/src/main/java/net/szum123321/textile_backup/core/create/compressors/tar/AbstractTarArchiver.java b/src/main/java/net/szum123321/textile_backup/core/create/compressors/tar/AbstractTarArchiver.java index 5aa8a3c..94d0ad2 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/compressors/tar/AbstractTarArchiver.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/compressors/tar/AbstractTarArchiver.java @@ -42,9 +42,15 @@ public class AbstractTarArchiver extends AbstractCompressor { } @Override - protected void addEntry(InputSupplier in, OutputStream arc) throws IOException { - try (InputStream fileInputStream = in.getInputStream()) { - TarArchiveEntry entry = (TarArchiveEntry)((TarArchiveOutputStream) arc).createArchiveEntry(in.getPath(), in.getName()); + protected void addEntry(InputSupplier input, OutputStream arc) throws IOException { + try (InputStream fileInputStream = input.getInputStream()) { + TarArchiveEntry entry; + if(input.getPath().isEmpty()) {//Virtual entry + entry = new TarArchiveEntry(input.getName()); + entry.setSize(input.size()); + } else + entry = (TarArchiveEntry)((TarArchiveOutputStream) arc).createArchiveEntry(input.getPath().get(), input.getName()); + ((TarArchiveOutputStream)arc).putArchiveEntry(entry); IOUtils.copy(fileInputStream, arc);