From 656c697f4c766a11200b25e95776c1768e9ce95a Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 16 Sep 2021 15:50:41 +0300 Subject: [PATCH] BAEL-5033 create file if it doesn't exist --- .../java/com/baeldung/java/io/zip4j/ZipMultiFile.java | 9 ++++++--- .../java/com/baeldung/java/io/zip4j/ZipSingleFile.java | 7 ++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java index b34eccd7db..081d207294 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipMultiFile.java @@ -18,9 +18,12 @@ public class ZipMultiFile { File firstFile = new File("aFile.txt"); File secondFile = new File("bFile.txt"); - - firstFile.createNewFile(); - secondFile.createNewFile(); + if (!firstFile.exists()) { + firstFile.createNewFile(); + } + if (!secondFile.exists()) { + secondFile.createNewFile(); + } List filesToAdd = Arrays.asList(firstFile, secondFile); diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java index 4d72c71c8d..d0947afa2e 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipSingleFile.java @@ -16,7 +16,12 @@ public class ZipSingleFile { zipParameters.setCompressionLevel(CompressionLevel.HIGHER); zipParameters.setEncryptionMethod(EncryptionMethod.AES); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); - zipFile.addFile(new File("aFile.txt")); + + File fileToAdd = new File("aFile.txt"); + if (!fileToAdd.exists()) { + fileToAdd.createNewFile(); + } + zipFile.addFile(fileToAdd); zipFile.close(); } }