|
6 | 6 | import java.nio.file.Path;
|
7 | 7 | import java.util.*;
|
8 | 8 | import java.util.zip.ZipEntry;
|
| 9 | +import java.util.zip.ZipException; |
9 | 10 | import java.util.zip.ZipFile;
|
10 | 11 | import java.util.zip.ZipInputStream;
|
11 | 12 |
|
@@ -34,21 +35,49 @@ protected Map<String, byte[]> loadClasses() throws IOException {
|
34 | 35 | ByteArrayOutputStream out = new ByteArrayOutputStream();
|
35 | 36 | byte[] buffer = new byte[8192];
|
36 | 37 | EntryLoader loader = getEntryLoader();
|
37 |
| - ZipInputStream zis = new ZipInputStream(new FileInputStream(getPath().toFile())); |
38 |
| - ZipEntry entry; |
39 |
| - while ((entry = zis.getNextEntry()) != null) { |
40 |
| - // verify entries are classes and valid files |
41 |
| - // - skip intentional garbage / zip file abnormalities |
42 |
| - if (shouldSkip(entry.getName())) |
43 |
| - continue; |
44 |
| - if (loader.isValidClassEntry(entry)) { |
| 38 | + |
| 39 | + try { |
| 40 | + ZipInputStream zis = new ZipInputStream(new FileInputStream(getPath().toFile())); |
| 41 | + ZipEntry entry; |
| 42 | + |
| 43 | + while ((entry = zis.getNextEntry()) != null) { |
| 44 | + // verify entries are classes and valid files |
| 45 | + // - skip intentional garbage / zip file abnormalities |
| 46 | + if (shouldSkip(entry.getName())) |
| 47 | + continue; |
| 48 | + if (!loader.isValidClassEntry(entry)) |
| 49 | + continue; |
| 50 | + |
45 | 51 | out.reset();
|
46 | 52 | byte[] in = IOUtil.toByteArray(zis, out, buffer);
|
| 53 | + |
47 | 54 | // There is no possible way a "class" under 30 bytes is valid
|
48 | 55 | if (in.length < 30)
|
49 | 56 | continue;
|
| 57 | + |
50 | 58 | loader.onClass(entry.getName(), in);
|
51 | 59 | }
|
| 60 | + } catch (ZipException e) { |
| 61 | + if (e.getMessage().contains("invalid entry CRC")) { |
| 62 | + // "ZipFile"/"JarFile" reads the entire ZIP file structure before letting us do any entry parsing. |
| 63 | + // This may not always be ideal, but this way has one major bonus. It totally ignores CRC validity. |
| 64 | + // It also ignores a few other zip entry values. |
| 65 | + // Since somebody can intentionally write bogus data there to crash "ZipInputStream" this way works. |
| 66 | + ZipFile zf = new ZipFile(getPath().toString()); |
| 67 | + Enumeration<? extends ZipEntry> entries = zf.entries(); |
| 68 | + while (entries.hasMoreElements()) { |
| 69 | + ZipEntry entry = entries.nextElement(); |
| 70 | + |
| 71 | + if (shouldSkip(entry.getName())) |
| 72 | + continue; |
| 73 | + if (!loader.isValidClassEntry(entry)) |
| 74 | + continue; |
| 75 | + |
| 76 | + InputStream zis = zf.getInputStream(entry); |
| 77 | + byte[] in = IOUtil.toByteArray(zis); |
| 78 | + loader.onClass(entry.getName(), in); |
| 79 | + } |
| 80 | + } |
52 | 81 | }
|
53 | 82 | loader.finishClasses();
|
54 | 83 | return loader.getClasses();
|
|
0 commit comments