From 8c7827bebd4d977f2fd6f5cae3bf76513058f432 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 16 Sep 2021 15:41:15 +0300 Subject: [PATCH] BAEL-5033 close resources, create file --- .../java/io/zip4j/CreateSplitZipFile.java | 7 ++++--- .../baeldung/java/io/zip4j/ExtractAllFile.java | 6 ++++-- .../java/io/zip4j/ExtractSingleFile.java | 6 ++++-- .../com/baeldung/java/io/zip4j/ZipFolder.java | 5 +++-- .../com/baeldung/java/io/zip4j/ZipMultiFile.java | 16 ++++++++++------ .../baeldung/java/io/zip4j/ZipSingleFile.java | 5 +++-- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/CreateSplitZipFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/CreateSplitZipFile.java index cc39bc9dd2..a7ecd7946e 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/CreateSplitZipFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/CreateSplitZipFile.java @@ -1,22 +1,23 @@ package com.baeldung.java.io.zip4j; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; +import java.io.IOException; import java.util.Arrays; public class CreateSplitZipFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); - int splitLength = 1024 * 1024 * 10; //10MB + int splitLength = 1024 * 1024 * 10; // 10MB zipFile.createSplitZipFile(Arrays.asList(new File("aFile.txt")), zipParameters, true, splitLength); zipFile.createSplitZipFileFromFolder(new File("/users/folder_to_add"), zipParameters, true, splitLength); + zipFile.close(); } } diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractAllFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractAllFile.java index 10e7ddd339..924d80a921 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractAllFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractAllFile.java @@ -1,12 +1,14 @@ package com.baeldung.java.io.zip4j; +import java.io.IOException; + import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; public class ExtractAllFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.extractAll("/destination_directory"); + zipFile.close(); } } diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractSingleFile.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractSingleFile.java index 4cf466d02b..ad79557a37 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractSingleFile.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ExtractSingleFile.java @@ -1,12 +1,14 @@ package com.baeldung.java.io.zip4j; +import java.io.IOException; + import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; public class ExtractSingleFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.extractFile("aFile.txt", "/destination_directory"); + zipFile.close(); } } diff --git a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipFolder.java b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipFolder.java index 4d89e8665f..ed25494083 100644 --- a/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipFolder.java +++ b/libraries-io/src/main/java/com/baeldung/java/io/zip4j/ZipFolder.java @@ -1,19 +1,20 @@ package com.baeldung.java.io.zip4j; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; +import java.io.IOException; public class ZipFolder { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.addFolder(new File("/users/folder_to_add"), zipParameters); + zipFile.close(); } } 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 dcb860ef92..b34eccd7db 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 @@ -1,27 +1,31 @@ package com.baeldung.java.io.zip4j; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.List; public class ZipMultiFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); - List filesToAdd = Arrays.asList( - new File("aFile.txt"), - new File("bFile.txt") - ); + File firstFile = new File("aFile.txt"); + File secondFile = new File("bFile.txt"); + + firstFile.createNewFile(); + secondFile.createNewFile(); + + List filesToAdd = Arrays.asList(firstFile, secondFile); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.addFiles(filesToAdd, zipParameters); + zipFile.close(); } } 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 a5f600df47..4d72c71c8d 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 @@ -1,21 +1,22 @@ package com.baeldung.java.io.zip4j; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.CompressionLevel; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; +import java.io.IOException; public class ZipSingleFile { - public static void main(String[] args) throws ZipException { + public static void main(String[] args) throws IOException { ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setCompressionLevel(CompressionLevel.HIGHER); zipParameters.setEncryptionMethod(EncryptionMethod.AES); ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray()); zipFile.addFile(new File("aFile.txt")); + zipFile.close(); } }