|
8 | 8 |
|
9 | 9 | import java.io.*; |
10 | 10 | import java.net.HttpURLConnection; |
11 | | -import java.net.MalformedURLException; |
12 | 11 | import java.net.URL; |
| 12 | +import java.nio.charset.Charset; |
13 | 13 | import java.nio.charset.StandardCharsets; |
14 | | -import java.util.Arrays; |
15 | 14 | import java.util.List; |
16 | 15 | import java.util.Map; |
17 | | -import java.util.stream.Collectors; |
18 | 16 | import java.util.zip.ZipEntry; |
19 | 17 | import java.util.zip.ZipInputStream; |
20 | 18 | import java.util.zip.ZipOutputStream; |
21 | 19 |
|
22 | 20 | public class Utils { |
23 | 21 | private static final int BUFFER_SIZE = 4096; |
24 | 22 | 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 }; |
25 | 24 |
|
26 | 25 | public static void invalid() { |
27 | 26 | System.err.println("Invalid usage. Use \"curseforge help\" for command list,"); |
@@ -100,40 +99,44 @@ public static <E> E getLast(List<E> list) { |
100 | 99 | } |
101 | 100 |
|
102 | 101 | 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(); |
127 | 129 | } |
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()); |
129 | 136 | } |
130 | | - zis.closeEntry(); |
131 | | - zis.close(); |
132 | | - return true; |
133 | | - } catch (IOException e) { |
134 | | - e.printStackTrace(); |
135 | | - return false; |
136 | 137 | } |
| 138 | + System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("All charsets failed. Installation stopped.").reset()); |
| 139 | + return false; |
137 | 140 | } |
138 | 141 |
|
139 | 142 | public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException { |
|
0 commit comments