50 lines
1.5 KiB
Java
50 lines
1.5 KiB
Java
package it.cavallium.warppi;
|
|
|
|
import java.io.File;
|
|
|
|
import net.lingala.zip4j.core.ZipFile;
|
|
import net.lingala.zip4j.model.ZipParameters;
|
|
import net.lingala.zip4j.util.Zip4jConstants;
|
|
|
|
public class ZipUtils {
|
|
public static void zip(String targetPath, String destinationFilePath, String password) {
|
|
try {
|
|
final ZipParameters parameters = new ZipParameters();
|
|
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
|
if (password.length() > 0) {
|
|
parameters.setEncryptFiles(true);
|
|
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
|
|
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
|
|
parameters.setPassword(password);
|
|
}
|
|
|
|
final ZipFile zipFile = new ZipFile(destinationFilePath);
|
|
|
|
final File targetFile = new File(targetPath);
|
|
if (targetFile.isFile()) {
|
|
zipFile.addFile(targetFile, parameters);
|
|
} else if (targetFile.isDirectory()) {
|
|
zipFile.addFolder(targetFile, parameters);
|
|
}
|
|
|
|
} catch (final Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void unzip(String targetZipFilePath, String destinationFolderPath, String password) {
|
|
try {
|
|
final ZipFile zipFile = new ZipFile(targetZipFilePath);
|
|
if (zipFile.isEncrypted()) {
|
|
zipFile.setPassword(password);
|
|
}
|
|
zipFile.extractAll(destinationFolderPath);
|
|
|
|
} catch (final Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|