Better CRC32
parent
927638e0b6
commit
c344e01c26
|
@ -56,9 +56,7 @@ public class ParallelZipCompressor extends ZipCompressor {
|
||||||
entry.setMethod(ZipArchiveOutputStream.STORED);
|
entry.setMethod(ZipArchiveOutputStream.STORED);
|
||||||
entry.setSize(file.length());
|
entry.setSize(file.length());
|
||||||
entry.setCompressedSize(file.length());
|
entry.setCompressedSize(file.length());
|
||||||
CRC32 sum = new CRC32();
|
entry.setCrc(getCRC(file));
|
||||||
sum.update(Files.readAllBytes(file.toPath()));
|
|
||||||
entry.setCrc(sum.getValue());
|
|
||||||
} else entry.setMethod(ZipEntry.DEFLATED);
|
} else entry.setMethod(ZipEntry.DEFLATED);
|
||||||
|
|
||||||
entry.setTime(System.currentTimeMillis());
|
entry.setTime(System.currentTimeMillis());
|
||||||
|
|
|
@ -30,6 +30,7 @@ import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.zip.CRC32;
|
import java.util.zip.CRC32;
|
||||||
|
import java.util.zip.Checksum;
|
||||||
|
|
||||||
public class ZipCompressor extends AbstractCompressor {
|
public class ZipCompressor extends AbstractCompressor {
|
||||||
public static ZipCompressor getInstance() {
|
public static ZipCompressor getInstance() {
|
||||||
|
@ -37,7 +38,7 @@ public class ZipCompressor extends AbstractCompressor {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) {
|
protected OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException {
|
||||||
ZipArchiveOutputStream arc = new ZipArchiveOutputStream(stream);
|
ZipArchiveOutputStream arc = new ZipArchiveOutputStream(stream);
|
||||||
|
|
||||||
arc.setMethod(ZipArchiveOutputStream.DEFLATED);
|
arc.setMethod(ZipArchiveOutputStream.DEFLATED);
|
||||||
|
@ -57,9 +58,7 @@ public class ZipCompressor extends AbstractCompressor {
|
||||||
entry.setMethod(ZipArchiveOutputStream.STORED);
|
entry.setMethod(ZipArchiveOutputStream.STORED);
|
||||||
entry.setSize(file.length());
|
entry.setSize(file.length());
|
||||||
entry.setCompressedSize(file.length());
|
entry.setCompressedSize(file.length());
|
||||||
CRC32 sum = new CRC32();
|
entry.setCrc(getCRC(file));
|
||||||
sum.update(Files.readAllBytes(file.toPath()));
|
|
||||||
entry.setCrc(sum.getValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
((ZipArchiveOutputStream)arc).putArchiveEntry(entry);
|
((ZipArchiveOutputStream)arc).putArchiveEntry(entry);
|
||||||
|
@ -75,4 +74,18 @@ public class ZipCompressor extends AbstractCompressor {
|
||||||
String[] arr = filename.split("\\.");
|
String[] arr = filename.split("\\.");
|
||||||
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 {
|
||||||
|
Checksum sum = new CRC32();
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
try (InputStream stream = new FileInputStream(file)) {
|
||||||
|
while ((len = stream.read(buffer)) != -1) sum.update(buffer, 0, len);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IOException("Error while calculating CRC of: " + file.getAbsolutePath(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum.getValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue