Skip to content

Commit bab7cce

Browse files
Add jar support
1 parent 2d1783e commit bab7cce

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.genexus.compression;
22

33
public enum CompressionFormat {
4-
GZIP,TAR, ZIP, SEVENZ
4+
GZIP,TAR, ZIP, SEVENZ, JAR
55
}

gxcompress/src/main/java/com/genexus/compression/GXCompressor.java

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import java.nio.file.Path;
1919
import java.nio.file.Paths;
2020
import java.util.Vector;
21+
import java.util.jar.JarEntry;
22+
import java.util.jar.JarInputStream;
23+
import java.util.jar.JarOutputStream;
2124
import java.util.zip.*;
2225

2326
import static org.apache.commons.io.FilenameUtils.getExtension;
@@ -51,6 +54,9 @@ public static int compressFiles(Vector<String> files, String path, String format
5154
case GZIP:
5255
compressToGzip(toCompress, path);
5356
return 0;
57+
case JAR:
58+
compressToJar(toCompress, path);
59+
return 0;
5460
}
5561
} catch (IllegalArgumentException iae) {
5662
log.error("Unsupported compression format for compression: {}", format);
@@ -103,6 +109,9 @@ public static int decompress(String file, String path) {
103109
case "gz":
104110
decompressGzip(toCompress, path);
105111
return 0;
112+
case "jar":
113+
decompressJar(toCompress, path);
114+
return 0;
106115
default:
107116
log.error("Unsupported compression format for decompression: {}", extension);
108117
return -3;
@@ -329,12 +338,8 @@ private static void decompressTar(File file, String outputPath) throws IOExcepti
329338
if (!outputFile.toPath().startsWith(targetDir)) {
330339
throw new IOException("Entry is outside of the target directory: " + entry.getName());
331340
}
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);
338343
} else {
339344
File parent = outputFile.getParentFile();
340345
if (!parent.exists() && !parent.mkdirs()) {
@@ -370,6 +375,52 @@ private static void decompressGzip(File inputFile, String outputPath) throws IOE
370375
}
371376
}
372377

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+
373424
private static CompressionFormat getCompressionFormat(String format) {
374425
try {
375426
return CompressionFormat.valueOf(format.toUpperCase());

gxcompress/src/test/java/com/genexus/compression/TestCompression.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public void testCompressToGzip() {
7474
assertTrue(new File(outputPath).exists());
7575
}
7676

77+
@Test
78+
public void testCompressToJar() {
79+
String outputPath = new File(testDirectory, "output.jar").getAbsolutePath();
80+
int result = GXCompressor.compressFiles(files, outputPath, "JAR");
81+
assertEquals(0, result);
82+
assertTrue(new File(outputPath).exists());
83+
}
84+
7785
@Test
7886
public void testUnsupportedFormat() {
7987
String outputPath = new File(testDirectory, "output.unknown").getAbsolutePath();

0 commit comments

Comments
 (0)