用Java创建受密码保护的Zip文件
时间:2020-01-09 10:35:25 来源:igfitidea点击:
在文章如何用Java压缩文件以及如何用Java压缩文件夹中,我们已经看到了如何用Java压缩文件和目录。在本文中,我们将介绍如何在Java中创建受密码保护的zip文件以及如何解压缩受密码保护的压缩文件。
Java中受密码保护的zip文件支持
在java.util.zip软件包中,该软件包包括用于压缩和解压缩文件的类,不支持创建受密码保护的zip文件,因此使用Zip4j库来实现此目的。
可以从以下位置下载创建受密码保护的zip文件zip4j_1.3.2.jar所需的jar – http://www.lingala.net/zip4j/download.php。
尽管Zip4j库有点老,并且还有其他可用的选项,但我认为该库仍然是创建受密码保护的zip文件的最佳选择。
受密码保护的zip文件Java示例
此处给出的两个示例涵盖了方案:
- 如果我们有单独的文件,可以将其添加到文件夹,然后对其进行压缩和密码保护。参见示例。
- 当我们要压缩目录和密码时,请对其进行保护。参见示例。
在这些示例中,我们还将看到如何解压缩受密码保护的文件。
将文件添加到文件夹并压缩
如果要压缩单独的文件,则将它们添加到ArrayList并将该列表以及用于压缩和加密的参数一起传递,以获取受密码保护的压缩文件。在示例中,unZipPasswordProtectedFiles()方法将受密码保护的压缩文件解压缩。
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
public class PwdZipFiles {
public static void main(String[] args) {
PwdZipFiles pwdZipFiles = new PwdZipFiles();
String zipFilePath = "F:\ZipTest\Final.zip";
pwdZipFiles.compressFilesWithPwd(zipFilePath);
pwdZipFiles.unZipPasswordProtectedFiles(zipFilePath);
}
public void compressFilesWithPwd(String zipFilePath) {
// Zipped folder name
try {
ZipFile zipFile = new ZipFile(zipFilePath);
ArrayList<File> filesToAdd = new ArrayList<>();
// Add files which are to be compressed to the array list
filesToAdd.add(new File("F:\ZipTest\Shop Implementation.docx"));
filesToAdd.add(new File("F:\ZipTest\Test.txt"));
filesToAdd.add(new File("F:\Test\sample.txt"));
// Initiate Zip Parameters
ZipParameters parameters = new ZipParameters();
// set compression method to deflate compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
// Setting password
parameters.setPassword("password");
zipFile.addFiles(filesToAdd, parameters);
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void unZipPasswordProtectedFiles(String zipFilePath){
// Get unzip file path by removing .zip from the zipped file name
String unZipFilePath = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));;
try {
ZipFile zipFile = new ZipFile(zipFilePath);
// provide password if encrypted
if(zipFile.isEncrypted()){
zipFile.setPassword("password");
}
// unzip
zipFile.extractAll(unZipFilePath);
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
压缩整个目录并提供密码保护
如果必须递归压缩目录结构,则可以按照此处给出的方法进行压缩。示例中压缩的目录结构如下所示。
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
public class PwdZipFiles {
public static void main(String[] args) {
PwdZipFiles pwdZipFiles = new PwdZipFiles();
// Directory to be zipped
String dirPath = "F:\ZipTest";
String zippedDirPath = "F:\ZipTest\ZippedDir.zip";
pwdZipFiles.compressDirWithPwd(dirPath, zippedDirPath);
pwdZipFiles.unZipPasswordProtectedFiles(zippedDirPath);
}
public void compressDirWithPwd(String dirPath, String zippedDirPath) {
try {
ZipFile zipFile = new ZipFile(zippedDirPath);
// Initiate Zip Parameters
ZipParameters parameters = new ZipParameters();
// set compression method to deflate compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
// Setting password
parameters.setPassword("password");
zipFile.addFolder(dirPath, parameters);
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void unZipPasswordProtectedFiles(String zipFilePath){
// Get unzip file path by removing .zip from the zipped file name
String unZipFilePath = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));;
try {
ZipFile zipFile = new ZipFile(zipFilePath);
// provide password if encrypted
if(zipFile.isEncrypted()){
zipFile.setPassword("password");
}
// unzip
zipFile.extractAll(unZipFilePath);
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我们可以看到解压缩压缩目录需要密码。

