由于我是 Java 新手,我创建了一种解压缩受密码保护的 zip 文件的方法,我使用 zip4j 库来解压缩 zip 文件,密码正确时代码工作正常,但密码错误时如何处理ZipException(net.lingala.zip4j.exception.ZipException:net.lingala.zip4j.exception.ZipException:net.lingala.zip4j.exception.ZipException:文件密码错误:Demo.zip)并显示适当的消息(密码错误! )。请帮助,这是我的代码。
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 UnunzipDemo{
public void unzipFilesWithPassword(String sourceZipFilePath,String extractedZipFilePath,String password){
try {
ZipFile zipFile = new ZipFile(sourceZipFilePath);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
zipFile.extractAll(extractedZipFilePath);
System.out.println("Done");
}
catch (ZipException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String sourceZipFilePath="E:/MyFiles/Files/Zip/Demo.zip";
String extractedZipFilePath="E:/MyFiles/Files/Unzip/";
String password="JOEL"; //Correct Password
UnunzipDemo unzipDemo=new UnunzipDemo();
unzipDemo.unzipFilesWithPassword(sourceZipFilePath,extractedZipFilePath,password);
}
}
暮色呼如
FFIVE
相关分类