如何用Java密码保护压缩的Excel文件?

时间:2012-01-10 08:49:24

标签: java excel zip password-protection

我对密码保护Excel文件有疑问。

情况是,我有一个zip文件,里面有一个Excel文件。我需要编写一个Java程序,以密码保护Excel文件。因此,用户应该能够解压缩文件(zip文件不需要受密码保护)。但是,Excel需要受密码保护。当用户尝试解压缩文件时,他应该能够这样做。 当他试图打开Excel文件(在解压缩文件夹中)时,它必须要求输入密码。该问题与Protect excel file with java类似,增加的复杂性是Excel文件已压缩。

我有代码,密码只保护zip文件,但这不是我想要的。

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

/**
* Demonstrates adding files to zip file with standard Zip Encryption
*/

public class AddFilesWithStandardZipEncryption
{
    public AddFilesWithStandardZipEncryption()
    {
    try {
            // Initiate ZipFile object with the path/name of the zip file.
            //ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesWithStandardZipEncryption.zip");
            ZipFile zipFile = new ZipFile("C:\\homepage\\workspace\\PasswordProtectedFiles\\new.zip");

            // Build the list of files to be added in the array list
            // Objects of type File have to be added to the ArrayList
            ArrayList filesToAdd = new ArrayList();
            //filesToAdd.add(new File("C:\\homepage\\workspace\\passwordprotectedzipfile\\profile\\profile.txt"));
            filesToAdd.add(new File("C:\\homepage\\workspace\\PasswordProtectedFiles\\new.xlsx"));
            //filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi"));
            //filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3"));

            // Initiate Zip Parameters which define various properties such
            // as compression method, etc.
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to store compression

            // Set the compression level
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 

            // Set the encryption flag to true
            // If this is set to false, then the rest of encryption properties are ignored
            parameters.setEncryptFiles(true);

            // Set the encryption method to Standard Zip Encryption
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);

            // Set password
            parameters.setPassword("test123!");

            // Now add files to the zip file
            // Note: To add a single file, the method addFile can be used
            // Note: If the zip file already exists and if this zip file is a split file
            // then this method throws an exception as Zip Format Specification does not 
            // allow updating split zip files
            zipFile.addFiles(filesToAdd, parameters);
        }
        catch (ZipException e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        new AddFilesWithStandardZipEncryption();
    }
}

3 个答案:

答案 0 :(得分:14)

如果没有解压缩,就无法密码保护zip文件中的excel。

这是你可以做的事情

答案 1 :(得分:4)

  • 如果您知道它很小,请使用java.util.zip或zip4j将文件解压缩到某个临时目录或内存。
  • 然后使用Apache POI库中的HSSFWorkbook.writeProtectWorkbook
  • 再次压缩Excel工作簿。

答案 2 :(得分:1)

我认为你应该查看truezip(Truezip website)。它提供对ZIP,JAR,EAR,WAR等的读/写访问,并支持附加到现有的ZIP文件。

我建议您创建不包含excel文件的zip文件,按照您提供的链接创建passworded excel文件,然后使用truezip将此excel文件写入存档。希望这有帮助