|
18 | 18 | import java.nio.file.Path;
|
19 | 19 | import java.nio.file.Paths;
|
20 | 20 | import java.util.Vector;
|
| 21 | +import java.util.jar.JarEntry; |
| 22 | +import java.util.jar.JarInputStream; |
| 23 | +import java.util.jar.JarOutputStream; |
21 | 24 | import java.util.zip.*;
|
22 | 25 |
|
23 | 26 | import static org.apache.commons.io.FilenameUtils.getExtension;
|
@@ -51,6 +54,9 @@ public static int compressFiles(Vector<String> files, String path, String format
|
51 | 54 | case GZIP:
|
52 | 55 | compressToGzip(toCompress, path);
|
53 | 56 | return 0;
|
| 57 | + case JAR: |
| 58 | + compressToJar(toCompress, path); |
| 59 | + return 0; |
54 | 60 | }
|
55 | 61 | } catch (IllegalArgumentException iae) {
|
56 | 62 | log.error("Unsupported compression format for compression: {}", format);
|
@@ -103,6 +109,9 @@ public static int decompress(String file, String path) {
|
103 | 109 | case "gz":
|
104 | 110 | decompressGzip(toCompress, path);
|
105 | 111 | return 0;
|
| 112 | + case "jar": |
| 113 | + decompressJar(toCompress, path); |
| 114 | + return 0; |
106 | 115 | default:
|
107 | 116 | log.error("Unsupported compression format for decompression: {}", extension);
|
108 | 117 | return -3;
|
@@ -329,12 +338,8 @@ private static void decompressTar(File file, String outputPath) throws IOExcepti
|
329 | 338 | if (!outputFile.toPath().startsWith(targetDir)) {
|
330 | 339 | throw new IOException("Entry is outside of the target directory: " + entry.getName());
|
331 | 340 | }
|
332 |
| - if (entry.isDirectory()) { |
333 |
| - if (!outputFile.exists()) { |
334 |
| - if (!outputFile.mkdirs()) { |
335 |
| - throw new IOException("Failed to create directory: " + outputFile); |
336 |
| - } |
337 |
| - } |
| 341 | + if (entry.isDirectory() && !outputFile.exists() && !outputFile.mkdirs()) { |
| 342 | + throw new IOException("Failed to create directory: " + outputFile); |
338 | 343 | } else {
|
339 | 344 | File parent = outputFile.getParentFile();
|
340 | 345 | if (!parent.exists() && !parent.mkdirs()) {
|
@@ -370,6 +375,52 @@ private static void decompressGzip(File inputFile, String outputPath) throws IOE
|
370 | 375 | }
|
371 | 376 | }
|
372 | 377 |
|
| 378 | + private static void compressToJar(File[] files, String outputPath) throws IOException { |
| 379 | + JarOutputStream jos = new JarOutputStream(Files.newOutputStream(Paths.get(outputPath))); |
| 380 | + byte[] buffer = new byte[1024]; |
| 381 | + for (File file : files) { |
| 382 | + FileInputStream fis = new FileInputStream(file); |
| 383 | + jos.putNextEntry(new JarEntry(file.getName())); |
| 384 | + int length; |
| 385 | + while ((length = fis.read(buffer)) > 0) { |
| 386 | + jos.write(buffer, 0, length); |
| 387 | + } |
| 388 | + fis.close(); |
| 389 | + jos.closeEntry(); |
| 390 | + } |
| 391 | + jos.close(); |
| 392 | + } |
| 393 | + |
| 394 | + public static void decompressJar(File jarFile, String outputPath) throws IOException { |
| 395 | + if (!jarFile.exists()) { |
| 396 | + throw new IOException("The jar file does not exist."); |
| 397 | + } |
| 398 | + File outputDir = new File(outputPath); |
| 399 | + if (!outputDir.exists() && !outputDir.mkdirs()) { |
| 400 | + throw new IOException("Failed to create output directory."); |
| 401 | + } |
| 402 | + try (JarInputStream jis = new JarInputStream(Files.newInputStream(jarFile.toPath()))) { |
| 403 | + JarEntry entry; |
| 404 | + byte[] buffer = new byte[1024]; |
| 405 | + while ((entry = jis.getNextJarEntry()) != null) { |
| 406 | + File outputFile = new File(outputDir, entry.getName()); |
| 407 | + if (entry.isDirectory()) { |
| 408 | + if (!outputFile.exists() && !outputFile.mkdirs()) { |
| 409 | + throw new IOException("Failed to create directory " + outputFile.getAbsolutePath()); |
| 410 | + } |
| 411 | + } else { |
| 412 | + try (FileOutputStream fos = new FileOutputStream(outputFile)) { |
| 413 | + int len; |
| 414 | + while ((len = jis.read(buffer)) > 0) { |
| 415 | + fos.write(buffer, 0, len); |
| 416 | + } |
| 417 | + } |
| 418 | + } |
| 419 | + jis.closeEntry(); |
| 420 | + } |
| 421 | + } |
| 422 | + } |
| 423 | + |
373 | 424 | private static CompressionFormat getCompressionFormat(String format) {
|
374 | 425 | try {
|
375 | 426 | return CompressionFormat.valueOf(format.toUpperCase());
|
|
0 commit comments