2018-10-25 13:49:17 +03:00
|
|
|
package ru.gravit.utils.helper;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-12-02 17:16:23 +03:00
|
|
|
import java.io.InputStream;
|
2018-11-01 16:03:14 +03:00
|
|
|
import java.net.URL;
|
2018-10-25 13:49:17 +03:00
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
public class UnpackHelper {
|
2018-11-01 16:03:14 +03:00
|
|
|
public static boolean unpack(URL resource, Path target) throws IOException {
|
2018-11-08 15:30:16 +03:00
|
|
|
if (IOHelper.exists(target)) {
|
2018-12-02 17:16:23 +03:00
|
|
|
if (matches(target, resource)) return false;
|
2018-10-25 13:49:17 +03:00
|
|
|
}
|
|
|
|
if (!IOHelper.exists(target))
|
|
|
|
target.toFile().createNewFile();
|
2018-12-02 17:16:23 +03:00
|
|
|
try (InputStream in = IOHelper.newInput(resource)) {
|
|
|
|
IOHelper.transfer(in, target, false);
|
|
|
|
}
|
2018-10-25 13:49:17 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-12-02 17:16:23 +03:00
|
|
|
private static boolean matches(Path target, URL in) {
|
2018-10-25 13:49:17 +03:00
|
|
|
try {
|
|
|
|
return Arrays.equals(SecurityHelper.digest(SecurityHelper.DigestAlgorithm.SHA256, in),
|
|
|
|
SecurityHelper.digest(SecurityHelper.DigestAlgorithm.SHA256, target));
|
|
|
|
} catch (IOException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|