1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Self correct if the zip file is corrupt.

This commit is contained in:
Cedric Beust 2015-10-08 23:24:05 -07:00
parent 5b38419aed
commit 1f3fb8f3eb

View file

@ -12,6 +12,7 @@ import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
public class Main { public class Main {
@ -96,18 +97,16 @@ public class Main {
getWrapperDir().getPath() + "/" + FILE_NAME + "-" + getWrapperVersion() + ".jar"); getWrapperDir().getPath() + "/" + FILE_NAME + "-" + getWrapperVersion() + ".jar");
if (! Files.exists(localZipFile) || ! Files.exists(kobaltJarFile)) { if (! Files.exists(localZipFile) || ! Files.exists(kobaltJarFile)) {
if (!Files.exists(localZipFile)) { if (!Files.exists(localZipFile)) {
String fullUrl = URL + "/" + fileName; download(fileName, localZipFile.toFile());
download(fullUrl, localZipFile.toFile());
if (!Files.exists(localZipFile)) {
log(2, localZipFile + " downloaded, extracting it");
} else {
log(2, localZipFile + " already exists, extracting it");
}
} }
// //
// Extract all the zip files // Extract all the zip files
// //
boolean success = false;
int retries = 0;
while (! success && retries < 2) {
try {
ZipFile zipFile = new ZipFile(localZipFile.toFile()); ZipFile zipFile = new ZipFile(localZipFile.toFile());
Enumeration<? extends ZipEntry> entries = zipFile.entries(); Enumeration<? extends ZipEntry> entries = zipFile.entries();
File outputDirectory = new File(DISTRIBUTIONS_DIR); File outputDirectory = new File(DISTRIBUTIONS_DIR);
@ -124,6 +123,15 @@ public class Main {
Files.copy(zipFile.getInputStream(entry), dest, StandardCopyOption.REPLACE_EXISTING); Files.copy(zipFile.getInputStream(entry), dest, StandardCopyOption.REPLACE_EXISTING);
} }
} }
success = true;
} catch (ZipException e) {
retries++;
error("Couldn't open zip file " + localZipFile + ": " + e.getMessage());
error("The file is probably corrupt, downloading it again");
Files.delete(localZipFile);
download(fileName, localZipFile.toFile());
}
}
} }
// //
@ -153,7 +161,9 @@ public class Main {
private static final String[] FILES = new String[] { KOBALTW, "kobalt/wrapper/" + FILE_NAME + "-wrapper.jar" }; private static final String[] FILES = new String[] { KOBALTW, "kobalt/wrapper/" + FILE_NAME + "-wrapper.jar" };
private void download(String fileUrl, File file) throws IOException { private void download(String fn, File file) throws IOException {
String fileUrl = URL + "/" + fn;
URL url = new URL(fileUrl); URL url = new URL(fileUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode(); int responseCode = httpConn.getResponseCode();
@ -214,6 +224,12 @@ public class Main {
error("No file to download. Server replied HTTP code: " + responseCode); error("No file to download. Server replied HTTP code: " + responseCode);
} }
httpConn.disconnect(); httpConn.disconnect();
if (! file.exists()) {
log(2, file + " downloaded, extracting it");
} else {
log(2, file + " already exists, extracting it");
}
} }
private void saveFile(File file, String text) throws IOException { private void saveFile(File file, String text) throws IOException {