如何为 java 中的 Zip 文件夹提供密码保护?

how to give the password protection to Zip folder in java?

我需要通过 java 为 Zip 文件夹设置密码保护,而不是为 zip 文件夹文件设置密码保护。没有密码我应该无法打开 Zip 文件夹。

这是我从 google 中找到的代码。

 public static void encrypt(String key, InputStream is, OutputStream os)        
 throws Throwable {encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);                             
}

据我所知,唯一能做到这一点的免费图书馆是 winzipaes。它有 Apache 许可证。

Google 代码项目页面 => https://code.google.com/p/winzipaes/

Maven 回购 Link => http://mvnrepository.com/artifact/de.idyl/winzipaes

使用 winzipaes1.0 完成。1.jar...

示例代码...

import java.io.File;
import java.io.IOException;
import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;

public class Practice1Main {

public static void main(String[]args) throws IOException{


    File aNewZipFile = new File("src.zip");
    File existingUnzippedFile = new File("src.txt");

    AESEncrypterBC encrypter = new AESEncrypterBC();
    encrypter.init("password", 0);  // The 0 is keySize, it is ignored for AESEncrypterBC

    AesZipFileEncrypter zipEncrypter = new AesZipFileEncrypter(aNewZipFile, encrypter);

    zipEncrypter.add(existingUnzippedFile, "src.txt", "password");
    zipEncrypter.close();
   }
}