簡體   English   中英

如何使用 zip4j 提取帶有密碼保護的 zip 文件

[英]How to use zip4j to extract an zip file with password protection

我正在嘗試解壓縮帶有密碼保護的 zip 文件。 我知道有一個名為“zip4j”的 Java 庫可以幫助我。 但是我無法打開 zip4j 網站查看教程。

我用另一個鏡像下載了 zip4j 庫,但我不知道如何使用它。 有沒有人可以粘貼示例代碼以使用 zip4j 解壓縮密碼保護 zip 文件?

zip4j 網站

非常感謝!

我正在嘗試使用密碼保護解壓縮zip文件。 我知道有一個名為“ zip4j”的Java庫可以為我提供幫助。 但是我無法打開zip4j網站來查看該教程。

我已經用另一個鏡像下載了zip4j庫,但是我不知道如何使用它。 有沒有人可以粘貼使用zip4j解壓縮密碼保護zip文件的示例代碼?

zip4j網站

非常感謝!

我正在嘗試使用密碼保護解壓縮zip文件。 我知道有一個名為“ zip4j”的Java庫可以為我提供幫助。 但是我無法打開zip4j網站來查看該教程。

我已經用另一個鏡像下載了zip4j庫,但是我不知道如何使用它。 有沒有人可以粘貼使用zip4j解壓縮密碼保護zip文件的示例代碼?

zip4j網站

非常感謝!

使用zip4j壓縮/解壓縮文件夾/文件的完整實現


將此依賴項添加到您的構建管理器。 或者,從這里下載最新的 JAR 文件並將其添加到您的項目構建路徑中。 下面的class可以壓縮和提取任何有或沒有密碼保護的文件或文件夾-

import java.io.File;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import net.lingala.zip4j.core.ZipFile;  

public class Compressor {
    public static void zip (String targetPath, String destinationFilePath, String password) {
        try {
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

            if (password.length() > 0) {
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
                parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                parameters.setPassword(password);
            }
                
            ZipFile zipFile = new ZipFile(destinationFilePath);
                
            File targetFile = new File(targetPath);
            if (targetFile.isFile()) {
                zipFile.addFile(targetFile, parameters);
            } else if (targetFile.isDirectory()) {
                zipFile.addFolder(targetFile, parameters);
            } else {
                //neither file nor directory; can be symlink, shortcut, socket, etc.
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        
    public static void unzip(String targetZipFilePath, String destinationFolderPath, String password) {
        try {
            ZipFile zipFile = new ZipFile(targetZipFilePath);
            if (zipFile.isEncrypted()) {
                zipFile.setPassword(password);
            }
            zipFile.extractAll(destinationFolderPath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**/ /// for test
    public static void main(String[] args) {
        
        String targetPath = "target\\file\\or\\folder\\path";
        String zipFilePath = "zip\\file\\Path"; 
        String unzippedFolderPath = "destination\\folder\\path";
        String password = "your_password"; // keep it EMPTY<""> for applying no password protection
            
        Compressor.zip(targetPath, zipFilePath, password);
        Compressor.unzip(zipFilePath, unzippedFolderPath, password);
    }/**/
}

更詳細的用法,請看這里

如果您使用的是 android,請確保您已在清單文件中添加了存儲權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM