BAEL-5033 close resources, create file

This commit is contained in:
Loredana Crusoveanu 2021-09-16 15:41:15 +03:00
parent ef068af675
commit 8c7827bebd
6 changed files with 28 additions and 17 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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<File> 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<File> filesToAdd = Arrays.asList(firstFile, secondFile);
ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray());
zipFile.addFiles(filesToAdd, zipParameters);
zipFile.close();
}
}

View File

@ -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();
}
}