文件夹加密不起作用(包括演示)
Folder Encryption Not Working (Demo Included)
这个简单的文件夹 encryption/compression 失败了。该文件夹仅包含子文件夹和文本文件,但加密 returns false。知道为什么吗?
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>
要加密的文件夹:
您忘记将 zipParameters
作为 zipFile.addFolder()
调用中的第二个参数传递。如果这是固定的,它将起作用(s. 也是 zip4j docs):
zipFile.addFolder(new File("C:\Users\me\Desktop\Folder"), zipParameters);
这个简单的文件夹 encryption/compression 失败了。该文件夹仅包含子文件夹和文本文件,但加密 returns false。知道为什么吗?
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>
要加密的文件夹:
您忘记将 zipParameters
作为 zipFile.addFolder()
调用中的第二个参数传递。如果这是固定的,它将起作用(s. 也是 zip4j docs):
zipFile.addFolder(new File("C:\Users\me\Desktop\Folder"), zipParameters);