|
11 | 11 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
12 | 12 | import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
13 | 13 | 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; |
17 | 14 | import org.apache.commons.io.IOUtils;
|
18 | 15 | import com.github.junrar.Junrar;
|
19 | 16 | import com.github.junrar.exception.RarException;
|
|
23 | 20 | import java.nio.file.Path;
|
24 | 21 | import java.nio.file.Paths;
|
25 | 22 | import java.util.ArrayList;
|
| 23 | +import java.util.Objects; |
26 | 24 | import java.util.jar.JarEntry;
|
27 | 25 | import java.util.jar.JarInputStream;
|
28 | 26 | import java.util.jar.JarOutputStream;
|
@@ -166,41 +164,48 @@ public static Boolean decompress(String file, String path, GXBaseCollection<SdtM
|
166 | 164 | }
|
167 | 165 | }
|
168 | 166 |
|
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); |
181 | 172 | }
|
| 173 | + zos.close(); |
| 174 | + fos.close(); |
182 | 175 | }
|
183 | 176 |
|
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(); |
186 | 183 | 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(); |
187 | 191 | File[] children = file.listFiles();
|
188 | 192 | if (children != null) {
|
189 |
| - for (File child : children) { |
190 |
| - addFileToZip(zos, child, entryName + "/"); |
| 193 | + for (File nestedFile : children) { |
| 194 | + addFileToZip(entryName, nestedFile, zos); |
191 | 195 | }
|
192 | 196 | }
|
193 | 197 | } 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); |
202 | 206 | }
|
203 |
| - zos.closeArchiveEntry(); |
| 207 | + zos.closeEntry(); |
| 208 | + fis.close(); |
204 | 209 | }
|
205 | 210 | }
|
206 | 211 |
|
@@ -277,27 +282,56 @@ private static void addFileToTar(TarArchiveOutputStream taos, File file, String
|
277 | 282 | }
|
278 | 283 | }
|
279 | 284 |
|
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); |
283 | 292 | }
|
| 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 | + } |
284 | 311 |
|
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 | + } |
297 | 323 | }
|
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(); |
301 | 335 | }
|
302 | 336 | }
|
303 | 337 |
|
@@ -420,19 +454,45 @@ private static void decompressGzip(File inputFile, String outputPath) throws IOE
|
420 | 454 | }
|
421 | 455 |
|
422 | 456 | 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); |
425 | 459 | 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 { |
426 | 484 | 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); |
431 | 492 | }
|
432 |
| - fis.close(); |
433 | 493 | jos.closeEntry();
|
| 494 | + fis.close(); |
434 | 495 | }
|
435 |
| - jos.close(); |
436 | 496 | }
|
437 | 497 |
|
438 | 498 | public static void decompressJar(File jarFile, String outputPath) throws IOException {
|
|
0 commit comments