亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何處理 ZipException(文件密碼錯誤:Demo.zip)并顯示適當的消息

如何處理 ZipException(文件密碼錯誤:Demo.zip)并顯示適當的消息

慕勒3428872 2022-05-25 09:43:39
    由于我是 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);    }}
查看完整描述

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");

        }

    }


查看完整回答
反對 回復 2022-05-25
?
FFIVE

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?

        }

    }


查看完整回答
反對 回復 2022-05-25
  • 2 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號