Added support of virtual file (in this case textile_status.data).

This commit is contained in:
Szum123321 2022-11-27 23:11:41 +01:00
parent d871fc14cb
commit 300fe18b10
6 changed files with 54 additions and 19 deletions

View File

@ -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<Path> getPath() { return Optional.of(path); }
@Override
public long size() throws IOException {
return Files.size(path);
}
@Override

View File

@ -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<Path> getPath();
String getName();
long size() throws IOException;
}

View File

@ -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<Path> getPath() { return Optional.empty(); }
public String getName() { return CompressionStatus.DATA_FILENAME; }
public long size() { return data.length; }
public InputStream get() { return getInputStream(); }
}
}

View File

@ -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()));
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());

View File

@ -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);

View File

@ -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);