-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepairZipFile.java
More file actions
49 lines (39 loc) · 1.37 KB
/
RepairZipFile.java
File metadata and controls
49 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class RepairZipFile {
public static void main(String[] args) {
for (int arg = 0; arg < args.length; arg++) {
File file = new File(args[arg]);
File repairFile = file.rename("*-repair.*");
//Define used standard coddings
Charset Ascii = Charset.forName("ISO-8859-1");
Charset UTF_8 = Charset.forName("UTF8");
try (
//set Acscii as default standard codding
ZipInputStream zis = new ZipInputStream(file, Ascii);
//set UTF_8 as default standard codding
ZipOutputStream zos = new ZipOutputStream(repairFile, UTF_8)) {
ZipEntry inputEntry;
while ((inputEntry = zis.getNextEntry()) != null) {
String fixedEntryName = new String(inputEntry.getName().getBytes(Ascii), UTF_8);
ZipEntry outputEntry = new ZipEntry(fixedEntryName);
zos.putNextEntry(outputEntry);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = zis.read(buffer)) != -1) {
zos.write(buffer, 0, bytesRead);
}
zos.closeEntry();
zis.closeEntry();
}
} catch (IOException e) {
System.err.println("An error occurred while repairing the ZIP file: " + e.getMessage());
}
}
}
}