This commit is contained in:
huanghongxun
2020-04-14 15:02:52 +08:00
parent d8b84a2574
commit 5adbac3cc9
2 changed files with 41 additions and 16 deletions

View File

@@ -26,6 +26,8 @@ import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.IOUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils;
@@ -34,13 +36,11 @@ import org.tukaani.xz.XZInputStream;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.jar.*;
import java.util.logging.Level;
import static org.jackhuang.hmcl.util.DigestUtils.digest;
@@ -142,6 +142,13 @@ public class LibraryDownloadTask extends Task<Void> {
library.getDownload().getSha1() != null ? new IntegrityCheck("SHA-1", library.getDownload().getSha1()) : null)
.setCacheRepository(cacheRepository)
.setCaching(true);
task.addIntegrityCheckHandler((file, dest) -> {
String ext = FileUtils.getExtension(dest).toLowerCase();
if (ext.equals("jar")) {
JarFile jarFile = new JarFile(file.toFile());
jarFile.getManifest();
}
});
xz = false;
}
} catch (IOException e) {

View File

@@ -91,6 +91,7 @@ public class FileDownloadTask extends Task<Void> {
private CacheRepository repository = CacheRepository.getInstance();
private RandomAccessFile rFile;
private InputStream stream;
private final ArrayList<IntegrityCheckHandler> integrityCheckHandlers = new ArrayList<>();
/**
* @param url the URL of remote file.
@@ -196,6 +197,10 @@ public class FileDownloadTask extends Task<Void> {
return this;
}
public void addIntegrityCheckHandler(IntegrityCheckHandler handler) {
integrityCheckHandlers.add(Objects.requireNonNull(handler));
}
@Override
public void execute() throws Exception {
boolean checkETag;
@@ -296,11 +301,19 @@ public class FileDownloadTask extends Task<Void> {
closeFiles();
if (downloaded != contentLength)
throw new IOException("Unexpected file size: " + downloaded + ", expected: " + contentLength);
// Restore temp file to original name.
if (isCancelled()) {
temp.toFile().delete();
break;
} else {
}
for (IntegrityCheckHandler handler : integrityCheckHandlers) {
handler.checkIntegrity(temp, file.toPath());
}
Files.deleteIfExists(file.toPath());
if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile()))
throw new IOException("Unable to make parent directory " + file);
@@ -309,10 +322,6 @@ public class FileDownloadTask extends Task<Void> {
} catch (Exception e) {
throw new IOException("Unable to move temp file from " + temp + " to " + file, e);
}
}
if (downloaded != contentLength)
throw new IOException("Unexpected file size: " + downloaded + ", expected: " + contentLength);
// Integrity check
if (integrityCheck != null) {
@@ -375,7 +384,7 @@ public class FileDownloadTask extends Task<Void> {
/**
* Download speed in byte/sec.
* @return
* @return download speed
*/
public int getSpeed() {
return speed;
@@ -387,4 +396,13 @@ public class FileDownloadTask extends Task<Void> {
}
}
public interface IntegrityCheckHandler {
/**
* Check whether the file is corrupted or not.
* @param filePath the file locates in (maybe in temp directory)
* @param destinationPath for real file name
* @throws IOException if the file is corrupted
*/
void checkIntegrity(Path filePath, Path destinationPath) throws IOException;
}
}