Skip to content
This repository was archived by the owner on Sep 9, 2023. It is now read-only.

Commit 78ae043

Browse files
fixed zip extraction
1 parent a20e59e commit 78ae043

3 files changed

Lines changed: 42 additions & 35 deletions

File tree

src/ml/northwestwind/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class Constants {
88
public static final String CURSEFORGE_API = "https://northwestwind.ml/api/curseforge/mods/";
9-
public static final String VERSION = "1.3.2";
9+
public static final String VERSION = "1.3.3";
1010
private static final String OS = System.getProperty("os.name").toLowerCase();
1111
public static final boolean IS_WINDOWS = (OS.contains("win"));
1212
public static final boolean IS_MAC = (OS.contains("mac"));

src/ml/northwestwind/Utils.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@
88

99
import java.io.*;
1010
import java.net.HttpURLConnection;
11-
import java.net.MalformedURLException;
1211
import java.net.URL;
12+
import java.nio.charset.Charset;
1313
import java.nio.charset.StandardCharsets;
14-
import java.util.Arrays;
1514
import java.util.List;
1615
import java.util.Map;
17-
import java.util.stream.Collectors;
1816
import java.util.zip.ZipEntry;
1917
import java.util.zip.ZipInputStream;
2018
import java.util.zip.ZipOutputStream;
2119

2220
public class Utils {
2321
private static final int BUFFER_SIZE = 4096;
2422
private static final String UPDATE_URL = "https://raw.githubusercontent.com/North-West-Wind/CurseForge-CLI/main/update.json";
23+
private static final Charset[] CHARSETS = { StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1, StandardCharsets.US_ASCII, StandardCharsets.UTF_16, StandardCharsets.UTF_16BE, StandardCharsets.UTF_16LE };
2524

2625
public static void invalid() {
2726
System.err.println("Invalid usage. Use \"curseforge help\" for command list,");
@@ -100,40 +99,44 @@ public static <E> E getLast(List<E> list) {
10099
}
101100

102101
public static boolean unzip(String file) {
103-
try {
104-
String fileZip = file;
105-
String[] splitted = file.split("/");
106-
File destDir = new File(Arrays.stream(Arrays.copyOf(splitted, splitted.length - 1)).collect(Collectors.joining("/")));
107-
byte[] buffer = new byte[1024];
108-
ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
109-
ZipEntry zipEntry = zis.getNextEntry();
110-
while (zipEntry != null) {
111-
File newFile = newFile(destDir, zipEntry);
112-
if (newFile.exists()) {
113-
if (newFile.isDirectory()) FileUtils.cleanDirectory(newFile);
114-
else newFile.delete();
115-
}
116-
if (zipEntry.isDirectory()) {
117-
if (!newFile.isDirectory() && !newFile.mkdirs())
118-
throw new IOException("Failed to create directory " + newFile);
119-
} else {
120-
File parent = newFile.getParentFile();
121-
if (!parent.isDirectory() && !parent.mkdirs())
122-
throw new IOException("Failed to create directory " + parent);
123-
FileOutputStream fos = new FileOutputStream(newFile);
124-
int len;
125-
while ((len = zis.read(buffer)) > 0) fos.write(buffer, 0, len);
126-
fos.close();
102+
for (Charset charset : CHARSETS) {
103+
try {
104+
File destDir = new File(file).getParentFile();
105+
byte[] buffer = new byte[1024];
106+
107+
FileInputStream fis = new FileInputStream(file);
108+
ZipInputStream zis = new ZipInputStream(fis, charset);
109+
ZipEntry zipEntry = zis.getNextEntry();
110+
while (zipEntry != null) {
111+
File newFile = newFile(destDir, zipEntry);
112+
if (newFile.exists()) {
113+
if (newFile.isDirectory()) FileUtils.cleanDirectory(newFile);
114+
else newFile.delete();
115+
}
116+
if (zipEntry.isDirectory()) {
117+
if (!newFile.isDirectory() && !newFile.mkdirs())
118+
throw new IOException("Failed to create directory " + newFile);
119+
} else {
120+
File parent = newFile.getParentFile();
121+
if (!parent.isDirectory() && !parent.mkdirs())
122+
throw new IOException("Failed to create directory " + parent);
123+
FileOutputStream fos = new FileOutputStream(newFile);
124+
int len;
125+
while ((len = zis.read(buffer)) > 0) fos.write(buffer, 0, len);
126+
fos.close();
127+
}
128+
zipEntry = zis.getNextEntry();
127129
}
128-
zipEntry = zis.getNextEntry();
130+
zis.closeEntry();
131+
zis.close();
132+
fis.close();
133+
return true;
134+
} catch (Exception e) {
135+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Charset " + charset.name() + " failed. Trying the next one...").reset());
129136
}
130-
zis.closeEntry();
131-
zis.close();
132-
return true;
133-
} catch (IOException e) {
134-
e.printStackTrace();
135-
return false;
136137
}
138+
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("All charsets failed. Installation stopped.").reset());
139+
return false;
137140
}
138141

139142
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {

update.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"1.3.3": {
3+
"title": "Added Charset retry",
4+
"description": "This fixes some modpack ZIPs having malformed inputs error."
5+
},
26
"1.3.2": {
37
"title": "Added User-Agent to requests",
48
"description": "This should fix the 403 error some users are receiving."

0 commit comments

Comments
 (0)