2 回答
TA贡献1853条经验 获得超9个赞
也许您可以从控制台读取密码。例如:
private static String password = "123";
public static void main(String[] args) {
// read the input password from console
// if you have UI, maybe you can read it from some way.
Scanner sc = new Scanner(System.in);
String inputPassword = sc.next();
while (true) {
//do something...
try {
unzip(inputPassword);
break;
} catch (Exception e) {
inputPassword = sc.next();
}
}
}
private static void unzip(String inputPassword) {
if (inputPassword.equals(password)) {
//unzip
} else {
// just demo. In your case, this is ZipException
throw new IllegalArgumentException("wrong password");
}
}
TA贡献1797条经验 获得超6个赞
您还可以检查错误代码。
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) {
if (e.getCode == ZipExceptionConstants.WRONG_PASSWORD) {
// Handle wrong password scenario
System.out.println("Wrong password");
} else {
//Handle other exception scenario - printing out error messages?
}
}
添加回答
举报