@@ -36,20 +36,27 @@ protected Map<String, byte[]> loadClasses() throws IOException {
36
36
byte [] buffer = new byte [8192 ];
37
37
EntryLoader loader = getEntryLoader ();
38
38
39
- try {
40
- ZipInputStream zis = new ZipInputStream (new FileInputStream (getPath ().toFile ()));
39
+ try (ZipInputStream zis = new ZipInputStream (new FileInputStream (getPath ().toFile ()))) {
41
40
ZipEntry entry ;
42
41
43
42
while ((entry = zis .getNextEntry ()) != null ) {
44
43
// verify entries are classes and valid files
45
44
// - skip intentional garbage / zip file abnormalities
46
45
if (shouldSkip (entry .getName ()))
47
46
continue ;
48
- if (!loader .isValidClassEntry (entry ))
49
- continue ;
50
47
51
48
out .reset ();
52
- byte [] in = IOUtil .toByteArray (zis , out , buffer );
49
+ byte [] in ;
50
+ if (!loader .isValidClassEntry (entry )) {
51
+ // The class file might not end with .class or .class/
52
+ // so we also check it's header.
53
+ in = IOUtil .toByteArray (zis , out , buffer , 4 );
54
+ if (!loader .isValidClassFile (new ByteArrayInputStream (in ))) {
55
+ continue ;
56
+ }
57
+ }
58
+
59
+ in = IOUtil .toByteArray (zis , out , buffer );
53
60
54
61
// There is no possible way a "class" under 30 bytes is valid
55
62
if (in .length < 30 )
@@ -70,11 +77,27 @@ protected Map<String, byte[]> loadClasses() throws IOException {
70
77
71
78
if (shouldSkip (entry .getName ()))
72
79
continue ;
73
- if (!loader .isValidClassEntry (entry ))
74
- continue ;
75
80
76
- InputStream zis = zf .getInputStream (entry );
77
- byte [] in = IOUtil .toByteArray (zis );
81
+ out .reset ();
82
+ byte [] in ;
83
+
84
+ if (!loader .isValidClassEntry (entry )) {
85
+ // The class file might not end with .class or .class/
86
+ // so we also check it's header.
87
+ out .reset ();
88
+ try (InputStream zis = zf .getInputStream (entry )) {
89
+ in = IOUtil .toByteArray (zis , out , buffer , 4 );
90
+ }
91
+ if (!loader .isValidClassFile (new ByteArrayInputStream (in ))) {
92
+ continue ;
93
+ }
94
+ }
95
+
96
+ out .reset ();
97
+ try (InputStream zis = zf .getInputStream (entry )) {
98
+ in = IOUtil .toByteArray (zis , out , buffer );
99
+ }
100
+
78
101
loader .onClass (entry .getName (), in );
79
102
}
80
103
}
0 commit comments