From 66fb7ea8771057499dd84413f439630894b341f8 Mon Sep 17 00:00:00 2001 From: Harry9656 Date: Tue, 22 Nov 2022 08:44:18 +0100 Subject: [PATCH] JAVA-5735: Appending a new File to a ZIP file (#13044) --- .../java/com/baeldung/zip/ZipAppendFile.java | 29 +++++++++++++++++++ .../com/baeldung/zip/ZipMultipleFiles.java | 8 +++-- .../src/main/resources/zipTest/file3.txt | 1 + 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 core-java-modules/core-java-io/src/main/java/com/baeldung/zip/ZipAppendFile.java create mode 100644 core-java-modules/core-java-io/src/main/resources/zipTest/file3.txt diff --git a/core-java-modules/core-java-io/src/main/java/com/baeldung/zip/ZipAppendFile.java b/core-java-modules/core-java-io/src/main/java/com/baeldung/zip/ZipAppendFile.java new file mode 100644 index 0000000000..3ac9beb542 --- /dev/null +++ b/core-java-modules/core-java-io/src/main/java/com/baeldung/zip/ZipAppendFile.java @@ -0,0 +1,29 @@ +package com.baeldung.zip; + +import java.io.*; +import java.net.URI; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import static java.util.stream.Collectors.toList; + +public class ZipAppendFile { + public static void main(final String[] args) throws IOException { + ZipMultipleFiles.main(args); + String file3 = ZipAppendFile.class.getClassLoader().getResource("zipTest/file3.txt").getPath(); + Map env = new HashMap<>(); + env.put("create", "true"); + Path path = Paths.get(Paths.get(file3).getParent() + "/compressed.zip"); + URI uri = URI.create("jar:" + path.toUri()); + try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { + Path nf = fs.getPath("newFile3.txt"); + Files.write(nf, Files.readAllBytes(Paths.get(file3)), StandardOpenOption.CREATE); + } + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-io/src/main/java/com/baeldung/zip/ZipMultipleFiles.java b/core-java-modules/core-java-io/src/main/java/com/baeldung/zip/ZipMultipleFiles.java index fc86147e43..cae7cd97d4 100644 --- a/core-java-modules/core-java-io/src/main/java/com/baeldung/zip/ZipMultipleFiles.java +++ b/core-java-modules/core-java-io/src/main/java/com/baeldung/zip/ZipMultipleFiles.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; import java.util.zip.ZipEntry; @@ -11,8 +12,11 @@ import java.util.zip.ZipOutputStream; public class ZipMultipleFiles { public static void main(final String[] args) throws IOException { - final List srcFiles = Arrays.asList("src/main/resources/zipTest/test1.txt", "src/main/resources/zipTest/test2.txt"); - final FileOutputStream fos = new FileOutputStream("src/main/resources/multiCompressed.zip"); + String file1 = ZipAppendFile.class.getClassLoader().getResource("zipTest/test1.txt").getPath(); + String file2 = ZipAppendFile.class.getClassLoader().getResource("zipTest/test2.txt").getPath(); + final List srcFiles = Arrays.asList(file1, file2); + final FileOutputStream fos = + new FileOutputStream(Paths.get(file1).getParent().toAbsolutePath() + "/compressed.zip"); final ZipOutputStream zipOut = new ZipOutputStream(fos); for (final String srcFile : srcFiles) { final File fileToZip = new File(srcFile); diff --git a/core-java-modules/core-java-io/src/main/resources/zipTest/file3.txt b/core-java-modules/core-java-io/src/main/resources/zipTest/file3.txt new file mode 100644 index 0000000000..5328e69047 --- /dev/null +++ b/core-java-modules/core-java-io/src/main/resources/zipTest/file3.txt @@ -0,0 +1 @@ +Test3.txt \ No newline at end of file