|
21 | 21 | import java.io.IOException; |
22 | 22 | import java.io.InputStream; |
23 | 23 | import java.net.URL; |
| 24 | +import java.util.ArrayList; |
24 | 25 | import java.util.Enumeration; |
| 26 | +import java.util.List; |
25 | 27 | import java.util.zip.ZipEntry; |
26 | 28 | import java.util.zip.ZipFile; |
27 | 29 |
|
@@ -275,34 +277,54 @@ private File unpackFromFileURL(URL url, String fileName, ClassLoader cl) { |
275 | 277 | return cacheFile; |
276 | 278 | } |
277 | 279 |
|
| 280 | + /** |
| 281 | + * Parse the list of entries of a URL assuming the URL is a jar URL. |
| 282 | + * |
| 283 | + * <ul> |
| 284 | + * <li>when the URL is a nested file within the archive, the list is the jar entry</li> |
| 285 | + * <li>when the URL is a nested file within a nested file within the archive, the list is jar entry followed by the jar entry of the nested archive</li> |
| 286 | + * <li>and so on.</li> |
| 287 | + * </ul> |
| 288 | + * |
| 289 | + * @param url the URL |
| 290 | + * @return the list of entries |
| 291 | + */ |
| 292 | + private List<String> listOfEntries(URL url) { |
| 293 | + String path = url.getPath(); |
| 294 | + List<String> list = new ArrayList<>(); |
| 295 | + int last = path.length(); |
| 296 | + for (int i = path.length() - 1; i > 4;) { |
| 297 | + if (path.charAt(i) == '!' && (path.startsWith(".jar", i - 4) || path.startsWith(".zip", i - 4) || path.startsWith(".war", i - 4))) { |
| 298 | + list.add(path.substring(2 + i, last)); |
| 299 | + last = i; |
| 300 | + i -= 4; |
| 301 | + } else { |
| 302 | + i--; |
| 303 | + } |
| 304 | + } |
| 305 | + return list; |
| 306 | + } |
| 307 | + |
| 308 | + |
278 | 309 | private File unpackFromJarURL(URL url, String fileName) { |
279 | 310 | ZipFile zip = null; |
280 | 311 | try { |
281 | | - String path = url.getPath(); |
282 | | - int idx1 = -1, idx2 = -1; |
283 | | - for (int i = path.length() - 1; i > 4; ) { |
284 | | - if (path.charAt(i) == '!' && (path.startsWith(".jar", i - 4) || path.startsWith(".zip", i - 4) || path.startsWith(".war", i - 4))) { |
285 | | - if (idx1 == -1) { |
286 | | - idx1 = i; |
287 | | - i -= 4; |
288 | | - continue; |
289 | | - } else { |
290 | | - idx2 = i; |
291 | | - break; |
292 | | - } |
293 | | - } |
294 | | - i--; |
295 | | - } |
296 | | - if (idx2 == -1) { |
297 | | - File file = new File(decodeURIComponent(path.substring(5, idx1), false)); |
298 | | - zip = new ZipFile(file); |
299 | | - } else { |
300 | | - String s = path.substring(idx2 + 2, idx1); |
301 | | - File file = resolveFile(s); |
302 | | - zip = new ZipFile(file); |
| 312 | + List<String> listOfEntries = listOfEntries(url); |
| 313 | + switch (listOfEntries.size()) { |
| 314 | + case 1: |
| 315 | + String path = url.getPath(); |
| 316 | + int to = path.length() - listOfEntries.get(0).length() - 2; |
| 317 | + String sub = path.substring(5, to); |
| 318 | + File file = new File(decodeURIComponent(sub, false)); |
| 319 | + zip = new ZipFile(file); |
| 320 | + break; |
| 321 | + case 2: |
| 322 | + zip = new ZipFile(resolveFile(listOfEntries.get(1))); |
| 323 | + break; |
| 324 | + default: |
| 325 | + throw new UnsupportedOperationException("Not yet implemented"); |
303 | 326 | } |
304 | | - |
305 | | - String inJarPath = path.substring(idx1 + 2); |
| 327 | + String inJarPath = listOfEntries.get(0); |
306 | 328 | StringBuilder prefixBuilder = new StringBuilder(); |
307 | 329 | int first = 0; |
308 | 330 | int second; |
|
0 commit comments