Unpack hashing fixed.

This commit is contained in:
zaxar163 2018-12-02 17:16:23 +03:00
parent d6d002c2f6
commit d21558e316
No known key found for this signature in database
GPG key ID: E3B309DD3852DE06

View file

@ -1,24 +1,25 @@
package ru.gravit.utils.helper;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.util.Arrays;
public class UnpackHelper {
@SuppressWarnings("ResultOfMethodCallIgnored")
public static boolean unpack(URL resource, Path target) throws IOException {
byte[] orig = IOHelper.read(resource);
if (IOHelper.exists(target)) {
if (matches(target, orig)) return false;
if (matches(target, resource)) return false;
}
if (!IOHelper.exists(target))
target.toFile().createNewFile();
IOHelper.transfer(orig, target, false);
try (InputStream in = IOHelper.newInput(resource)) {
IOHelper.transfer(in, target, false);
}
return true;
}
private static boolean matches(Path target, byte[] in) {
private static boolean matches(Path target, URL in) {
try {
return Arrays.equals(SecurityHelper.digest(SecurityHelper.DigestAlgorithm.SHA256, in),
SecurityHelper.digest(SecurityHelper.DigestAlgorithm.SHA256, target));