|
22 | 22 | import org.apache.commons.io.IOUtils;
|
23 | 23 | import org.apache.commons.io.filefilter.NameFileFilter;
|
24 | 24 | import org.apache.commons.lang3.mutable.MutableBoolean;
|
| 25 | +import org.apache.log4j.Logger; |
25 | 26 | import org.jetbrains.annotations.Contract;
|
26 | 27 |
|
27 | 28 | import javax.annotation.Nonnull;
|
|
30 | 31 | import java.nio.charset.Charset;
|
31 | 32 | import java.nio.charset.StandardCharsets;
|
32 | 33 | import java.nio.file.Files;
|
| 34 | +import java.nio.file.Path; |
33 | 35 | import java.util.ArrayList;
|
34 | 36 | import java.util.Comparator;
|
35 | 37 | import java.util.List;
|
|
40 | 42 | */
|
41 | 43 | @SuppressWarnings({"WeakerAccess", "unused"})
|
42 | 44 | public final class ZipUtil {
|
| 45 | + private static final Logger logger = Logger.getLogger(ZipUtil.class); |
| 46 | + |
43 | 47 | @SuppressWarnings("unused")
|
44 | 48 | public static final int MINIMAL_COMPRESSION_LEVEL = 0;
|
45 | 49 | public static final int DEFAULT_COMPRESSION_LEVEL = 5;
|
@@ -261,8 +265,107 @@ public static void unzip(File zipArchive, File destinationDirectory) throws IOEx
|
261 | 265 | unzip(zipArchive, destinationDirectory, null);
|
262 | 266 | }
|
263 | 267 |
|
| 268 | + /** |
| 269 | + * Unzips a ZIP-archive to the specified directory. |
| 270 | + * |
| 271 | + * @param zipArchive ZIP-archive to unzip |
| 272 | + * @param destinationDirectory directory to unzip to |
| 273 | + * @throws IOException if any I/O-exception occurred |
| 274 | + */ |
264 | 275 | public static void unzip(File zipArchive, File destinationDirectory, @Nullable FileFilter skipFilter)
|
265 | 276 | throws IOException {
|
| 277 | + long startTimeMillis = System.currentTimeMillis(); |
| 278 | + long compressedSize = zipArchive.length(); |
| 279 | + long totalUncompressedSize = 0L; |
| 280 | + |
| 281 | + FileUtil.ensureDirectoryExists(destinationDirectory); |
| 282 | + Path destPath = destinationDirectory.toPath().toRealPath(); |
| 283 | + |
| 284 | + int count = 0; |
| 285 | + |
| 286 | + try (ZipInputStream zis = new ZipInputStream( |
| 287 | + new BufferedInputStream(Files.newInputStream(zipArchive.toPath())))) { |
| 288 | + ZipEntry entry; |
| 289 | + |
| 290 | + while ((entry = zis.getNextEntry()) != null && count < MAX_ZIP_ENTRY_COUNT) { |
| 291 | + try { |
| 292 | + String entryName = entry.getName().replace('\\', '/'); |
| 293 | + |
| 294 | + File targetFile = new File(destinationDirectory, entryName).getCanonicalFile(); |
| 295 | + if (!targetFile.getAbsolutePath().startsWith(destPath.toString())) { |
| 296 | + throw new IOException("ZIP entry tries to escape destination directory: " + entryName); |
| 297 | + } |
| 298 | + |
| 299 | + if (skipFilter != null && skipFilter.accept(targetFile)) { |
| 300 | + continue; // Entry will be closed in finally block |
| 301 | + } |
| 302 | + |
| 303 | + if (entry.isDirectory()) { |
| 304 | + FileUtil.ensureDirectoryExists(targetFile); |
| 305 | + } else { |
| 306 | + // Check size if known upfront |
| 307 | + long size = entry.getSize(); |
| 308 | + if (size > MAX_ZIP_ENTRY_SIZE) { |
| 309 | + throw new IOException(String.format("Entry '%s' (%s) is larger than %s.", |
| 310 | + entryName, FileUtil.formatSize(size), |
| 311 | + FileUtil.formatSize(MAX_ZIP_ENTRY_SIZE))); |
| 312 | + } |
| 313 | + |
| 314 | + // Ensure parent dirs exist |
| 315 | + File parent = targetFile.getParentFile(); |
| 316 | + if (!parent.exists() && !parent.mkdirs()) { |
| 317 | + throw new IOException("Failed to create parent directory: " + parent); |
| 318 | + } |
| 319 | + |
| 320 | + Files.deleteIfExists(targetFile.toPath()); |
| 321 | + Path targetPath = targetFile.toPath(); |
| 322 | + |
| 323 | + try (OutputStream out = new BufferedOutputStream( |
| 324 | + Files.newOutputStream(targetPath))) { |
| 325 | + byte[] buffer = new byte[65536]; // 64 KiB buffer |
| 326 | + int read; |
| 327 | + long totalRead = 0; |
| 328 | + |
| 329 | + while ((read = zis.read(buffer)) != -1) { |
| 330 | + totalRead += read; |
| 331 | + if (totalRead > MAX_ZIP_ENTRY_SIZE) { |
| 332 | + throw new IOException("Extracted data exceeds allowed size for: " + entryName); |
| 333 | + } |
| 334 | + out.write(buffer, 0, read); |
| 335 | + } |
| 336 | + |
| 337 | + totalUncompressedSize += totalRead; |
| 338 | + } catch (IOException e) { |
| 339 | + // Clean up partially created file on error |
| 340 | + Files.deleteIfExists(targetPath); |
| 341 | + throw e; |
| 342 | + } |
| 343 | + } |
| 344 | + |
| 345 | + ++count; |
| 346 | + } finally { |
| 347 | + // Always close the current entry, even on exceptions |
| 348 | + try { |
| 349 | + zis.closeEntry(); |
| 350 | + } catch (IOException e) { |
| 351 | + // Log warning but don't mask original exception |
| 352 | + // logger.warn("Failed to close ZIP entry", e); |
| 353 | + } |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + |
| 358 | + String message = String.format( |
| 359 | + "Unzipped %d entries from '%s' to '%s' in %d ms. Compressed size: %s, Uncompressed size: %s.", |
| 360 | + count, zipArchive.getAbsolutePath(), destinationDirectory.getAbsolutePath(), |
| 361 | + System.currentTimeMillis() - startTimeMillis, |
| 362 | + FileUtil.formatSize(compressedSize), FileUtil.formatSize(totalUncompressedSize) |
| 363 | + ); |
| 364 | + logger.info(message); |
| 365 | + } |
| 366 | + |
| 367 | + public static void unzip2(File zipArchive, File destinationDirectory, @Nullable FileFilter skipFilter) |
| 368 | + throws IOException { |
266 | 369 | try (net.lingala.zip4j.ZipFile zipFile = new net.lingala.zip4j.ZipFile(zipArchive)) {
|
267 | 370 | FileUtil.ensureDirectoryExists(destinationDirectory);
|
268 | 371 |
|
|
0 commit comments