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:
parent
5b38419aed
commit
1f3fb8f3eb
1 changed files with 38 additions and 22 deletions
|
@ -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,32 +97,39 @@ 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
|
||||||
//
|
//
|
||||||
ZipFile zipFile = new ZipFile(localZipFile.toFile());
|
boolean success = false;
|
||||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
int retries = 0;
|
||||||
File outputDirectory = new File(DISTRIBUTIONS_DIR);
|
while (! success && retries < 2) {
|
||||||
outputDirectory.mkdirs();
|
try {
|
||||||
while (entries.hasMoreElements()) {
|
ZipFile zipFile = new ZipFile(localZipFile.toFile());
|
||||||
ZipEntry entry = entries.nextElement();
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||||
File entryFile = new File(entry.getName());
|
File outputDirectory = new File(DISTRIBUTIONS_DIR);
|
||||||
if (entry.isDirectory()) {
|
outputDirectory.mkdirs();
|
||||||
entryFile.mkdirs();
|
while (entries.hasMoreElements()) {
|
||||||
} else {
|
ZipEntry entry = entries.nextElement();
|
||||||
Path dest = Paths.get(zipOutputDir, entryFile.getPath());
|
File entryFile = new File(entry.getName());
|
||||||
log(2, " Writing " + entry.getName() + " to " + dest);
|
if (entry.isDirectory()) {
|
||||||
Files.createDirectories(dest.getParent());
|
entryFile.mkdirs();
|
||||||
Files.copy(zipFile.getInputStream(entry), dest, StandardCopyOption.REPLACE_EXISTING);
|
} else {
|
||||||
|
Path dest = Paths.get(zipOutputDir, entryFile.getPath());
|
||||||
|
log(2, " Writing " + entry.getName() + " to " + dest);
|
||||||
|
Files.createDirectories(dest.getParent());
|
||||||
|
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 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue