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

发布于 2024-12-25 18:26:55 字数 3091 浏览 3 评论 0原文

我有一个关于保护 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();
    }
}

I have a question about password protecting an Excel file.

The situation is that, I have a zip file, that has an Excel file in it. I need to write a Java program, to password protect the Excel file. Hence, the user should be able to unzip the file (the zip file need not be password protected). But, the Excel needs to be password-protected. When the user tries to unzip the file, he should be able to do so.
And when he tries to open the Excel file (which is inside the unzipped folder), it must ask for a password. The question is similar to Protect excel file with java, with the added complexity that, the Excel file is zipped.

I have code, that password protects only the zip file, but this is not what I want.

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦屿孤独相伴 2025-01-01 18:26:55

Without uncompressing, Its impossible to password protect excel which is inside a zip file.

Here is what you can do

那支青花 2025-01-01 18:26:55
  • 使用 java.util.zip 或 zip4j 将文件解压缩到某个临时目录或内存(如果您知道它很小)。
  • 然后使用 Apache 中的 HSSFWorkbook.writeProtectWorkbook POI 库
  • 再次压缩 Excel 工作簿。
  • Use java.util.zip or zip4j to decompress file to some temp direcotry or to memory, if you know it's small.
  • Then use HSSFWorkbook.writeProtectWorkbook from Apache POI library
  • Compress Excel workbook again.
七分※倦醒 2025-01-01 18:26:55

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

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

I think you should check out truezip (Truezip website). It provides read/write access to ZIP, JAR, EAR, WAR etc and supports appending to existing ZIP files.

I suggest you create your zip file without the excel file in it, create your passworded excel file as directed in the link you provided and then use truezip to write this excel file to the archive. Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文