压缩java中的文件夹

时间:2015-12-09 16:33:29

标签: java zip zip4j

我正在使用java 7在Eclipse中开发一个项目,我想压缩一个目录,里面有很多目录和文件并建立密码保护,我用它们zip4j库,它解决了我的问题,但没有所有因为它只建立目录内文件的密码而不是root文件夹,换句话说,我想当我们自动对zip文件进行双重clic时它要求我写一个像Windows这样的密码。以下是使用上述库的代码:

public static void zipFile(String password) throws NoSuchAlgorithmException, ZipException
{   
    // --------Encryption zipParameters (for password protection)-------
    //Create ZipParameters

    ZipParameters zipParameters = new ZipParameters();

    // Set how you want to encrypt files
    zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

    // Set encryption of files to true
    zipParameters.setEncryptFiles(true);

    // Set encryption method
    zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
    // Set key strength
    zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

    // Set password

    zipParameters.setPassword(password);               


    // --------------------CREATE ZIP file - Zip DIRECTORY------------

    //Zip file name        
    String destinationZipFilePath = "C:/temp/FoldertoCompress.zip";

    // Create ZIP file
    ZipFile zipFile = new ZipFile(destinationZipFilePath);

    // Directory to be Zipped
    String directoryToBeZipped = "C:/FoldertoCompress";

    // pass (Directory to be Zipped) and ZIP parameters
    //for Zip file to be created
    zipFile.addFolder(directoryToBeZipped, zipParameters);

    System.out.println("Password protected Zip file of Directory "
           +directoryToBeZipped+" have been created at "+ destinationZipFilePath);          
}

0 个答案:

没有答案