Skip to content

Commit 35211b7

Browse files
Consider folder within files to compress for jar and zip formats
(cherry picked from commit b64ca63)
1 parent 308ad6f commit 35211b7

File tree

1 file changed

+114
-54
lines changed

1 file changed

+114
-54
lines changed

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

Lines changed: 114 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
1212
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
1313
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
14-
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
15-
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
16-
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
1714
import org.apache.commons.io.IOUtils;
1815
import com.github.junrar.Junrar;
1916
import com.github.junrar.exception.RarException;
@@ -23,6 +20,7 @@
2320
import java.nio.file.Path;
2421
import java.nio.file.Paths;
2522
import java.util.ArrayList;
23+
import java.util.Objects;
2624
import java.util.jar.JarEntry;
2725
import java.util.jar.JarInputStream;
2826
import java.util.jar.JarOutputStream;
@@ -166,41 +164,48 @@ public static Boolean decompress(String file, String path, GXBaseCollection<SdtM
166164
}
167165
}
168166

169-
private static void compressToZip(File[] files, String outputPath) {
170-
try (OutputStream fos = Files.newOutputStream(Paths.get(outputPath));
171-
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos)) {
172-
zos.setMethod(ZipArchiveOutputStream.DEFLATED);
173-
for (File file : files) {
174-
if (file.exists()) {
175-
addFileToZip(zos, file, "");
176-
}
177-
}
178-
} catch (IOException e) {
179-
log.error("Error while compressing to zip", e);
180-
throw new RuntimeException("Failed to compress files", e);
167+
private static void compressToZip(File[] files, String outputPath) throws IOException {
168+
FileOutputStream fos = new FileOutputStream(outputPath);
169+
ZipOutputStream zos = new ZipOutputStream(fos);
170+
for (File file : files) {
171+
addFileToZip("", file, zos);
181172
}
173+
zos.close();
174+
fos.close();
182175
}
183176

184-
private static void addFileToZip(ZipArchiveOutputStream zos, File file, String base) throws IOException {
185-
String entryName = base + file.getName();
177+
private static void addFileToZip(String parent, File file, ZipOutputStream zos) throws IOException {
178+
if (file.isHidden()) {
179+
log.error("{} is a hidden file and cannot be compressed", file.getAbsolutePath());
180+
return;
181+
}
182+
String entryName = parent + file.getName();
186183
if (file.isDirectory()) {
184+
if (!entryName.endsWith("/")) {
185+
entryName += "/";
186+
}
187+
ZipEntry entry = new ZipEntry(entryName);
188+
entry.setTime(file.lastModified());
189+
zos.putNextEntry(entry);
190+
zos.closeEntry();
187191
File[] children = file.listFiles();
188192
if (children != null) {
189-
for (File child : children) {
190-
addFileToZip(zos, child, entryName + "/");
193+
for (File nestedFile : children) {
194+
addFileToZip(entryName, nestedFile, zos);
191195
}
192196
}
193197
} else {
194-
ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, entryName);
195-
zos.putArchiveEntry(zipEntry);
196-
try (FileInputStream fis = new FileInputStream(file)) {
197-
byte[] buffer = new byte[1024];
198-
int length;
199-
while ((length = fis.read(buffer)) >= 0) {
200-
zos.write(buffer, 0, length);
201-
}
198+
FileInputStream fis = new FileInputStream(file);
199+
ZipEntry entry = new ZipEntry(entryName);
200+
entry.setTime(file.lastModified());
201+
zos.putNextEntry(entry);
202+
byte[] buffer = new byte[1024];
203+
int count;
204+
while ((count = fis.read(buffer)) != -1) {
205+
zos.write(buffer, 0, count);
202206
}
203-
zos.closeArchiveEntry();
207+
zos.closeEntry();
208+
fis.close();
204209
}
205210
}
206211

@@ -277,27 +282,56 @@ private static void addFileToTar(TarArchiveOutputStream taos, File file, String
277282
}
278283
}
279284

280-
private static void compressToGzip(File[] files, String outputPath) throws RuntimeException {
281-
if (files.length > 1) {
282-
throw new IllegalArgumentException("GZIP does not support multiple files. Consider archiving the files first.");
285+
private static void compressToGzip(File[] files, String outputPath) throws IOException {
286+
File tarFile = new File(outputPath.replace(".gz", ".tar"));
287+
FileOutputStream fosTar = new FileOutputStream(tarFile);
288+
BufferedOutputStream bosTar = new BufferedOutputStream(fosTar);
289+
TarArchiveOutputStream taos = new TarArchiveOutputStream(bosTar);
290+
for (File file : files) {
291+
addFilesToTar(file, "", taos);
283292
}
293+
taos.finish();
294+
taos.close();
295+
bosTar.close();
296+
fosTar.close();
297+
FileInputStream fis = new FileInputStream(tarFile);
298+
FileOutputStream fosGzip = new FileOutputStream(outputPath);
299+
GZIPOutputStream gzos = new GZIPOutputStream(fosGzip);
300+
byte[] buffer = new byte[1024];
301+
int length;
302+
while ((length = fis.read(buffer)) > 0) {
303+
gzos.write(buffer, 0, length);
304+
}
305+
gzos.finish();
306+
gzos.close();
307+
fosGzip.close();
308+
fis.close();
309+
tarFile.delete();
310+
}
284311

285-
File inputFile = files[0];
286-
File outputFile = new File(outputPath);
287-
288-
try (InputStream in = Files.newInputStream(inputFile.toPath());
289-
FileOutputStream fout = new FileOutputStream(outputFile);
290-
BufferedOutputStream out = new BufferedOutputStream(fout);
291-
GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out)) {
292-
293-
byte[] buffer = new byte[4096];
294-
int n;
295-
while (-1 != (n = in.read(buffer))) {
296-
gzOut.write(buffer, 0, n);
312+
private static void addFilesToTar(File file, String parent, TarArchiveOutputStream taos) throws IOException {
313+
String entryName = parent + file.getName();
314+
if (file.isDirectory()) {
315+
File[] children = file.listFiles();
316+
if (children != null) {
317+
if (!entryName.endsWith("/")) {
318+
entryName += "/";
319+
}
320+
for (File child : children) {
321+
addFilesToTar(child, entryName, taos);
322+
}
297323
}
298-
} catch (IOException e) {
299-
log.error("Error while compressing to gzip", e);
300-
throw new RuntimeException("Error compressing file with GZIP", e);
324+
} else {
325+
TarArchiveEntry entry = new TarArchiveEntry(file, entryName);
326+
taos.putArchiveEntry(entry);
327+
BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(file.toPath()));
328+
byte[] buffer = new byte[1024];
329+
int count;
330+
while ((count = bis.read(buffer)) != -1) {
331+
taos.write(buffer, 0, count);
332+
}
333+
bis.close();
334+
taos.closeArchiveEntry();
301335
}
302336
}
303337

@@ -420,19 +454,45 @@ private static void decompressGzip(File inputFile, String outputPath) throws IOE
420454
}
421455

422456
private static void compressToJar(File[] files, String outputPath) throws IOException {
423-
JarOutputStream jos = new JarOutputStream(Files.newOutputStream(Paths.get(outputPath)));
424-
byte[] buffer = new byte[1024];
457+
FileOutputStream fos = new FileOutputStream(outputPath);
458+
JarOutputStream jos = new JarOutputStream(fos);
425459
for (File file : files) {
460+
addFileToJar("", file, jos);
461+
}
462+
jos.close();
463+
fos.close();
464+
}
465+
466+
private static void addFileToJar(String parent, File file, JarOutputStream jos) throws IOException {
467+
if (file.isHidden()) {
468+
log.error("{} is a hidden file and cannot be compressed", file.getAbsolutePath());
469+
return;
470+
}
471+
String entryName = parent + file.getName();
472+
if (file.isDirectory()) {
473+
if (!entryName.endsWith("/")) {
474+
entryName += "/";
475+
}
476+
JarEntry entry = new JarEntry(entryName);
477+
entry.setTime(file.lastModified());
478+
jos.putNextEntry(entry);
479+
jos.closeEntry();
480+
for (File nestedFile : Objects.requireNonNull(file.listFiles())) {
481+
addFileToJar(entryName, nestedFile, jos);
482+
}
483+
} else {
426484
FileInputStream fis = new FileInputStream(file);
427-
jos.putNextEntry(new JarEntry(file.getName()));
428-
int length;
429-
while ((length = fis.read(buffer)) > 0) {
430-
jos.write(buffer, 0, length);
485+
JarEntry entry = new JarEntry(entryName);
486+
entry.setTime(file.lastModified());
487+
jos.putNextEntry(entry);
488+
byte[] buffer = new byte[1024];
489+
int count;
490+
while ((count = fis.read(buffer)) != -1) {
491+
jos.write(buffer, 0, count);
431492
}
432-
fis.close();
433493
jos.closeEntry();
494+
fis.close();
434495
}
435-
jos.close();
436496
}
437497

438498
public static void decompressJar(File jarFile, String outputPath) throws IOException {

0 commit comments

Comments
 (0)