2 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
也許您可以從控制臺(tái)讀取密碼。例如:
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貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
您還可以檢查錯(cuò)誤代碼。
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?
}
}