This simple folder encryption/compression fails. The folder only contains a child folder and text file, yet encryption returns false. Any idea why?
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.AesKeyStrength;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import java.io.File;
public class Demo_ZipEncryptFolderOnly {
public static void main(String[] args) throws ZipException {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setEncryptFiles(true);
zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
zipParameters.setCompressionLevel(CompressionLevel.NORMAL);
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
ZipFile zipFile = new ZipFile("C:\\Users\\me\\Desktop\\Folder.zip", "password".toCharArray());
zipFile.addFolder(new File("C:\\Users\\me\\Desktop\\Folder"));
System.out.println("Encrypted: "+zipFile.isEncrypted());
}
}
Maven:
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.8.0</version>
</dependency>
Folder to Encrypt:
You forgot to pass zipParameters
as second parameter in the zipFile.addFolder()
call. If this is fixed, it works (s. also the zip4j docs):
zipFile.addFolder(new File("C:\\Users\\me\\Desktop\\Folder"), zipParameters);