如何密码保护Java中的压缩Excel文件? [英] How to password protect a zipped Excel file in Java?

查看:190
本文介绍了如何密码保护Java中的压缩Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于密码保护Excel文件的问题。

I have a question about password protecting an Excel file.

情况是,我有一个zip文件,其中有一个Excel文件。我需要编写一个Java程序,密码保护Excel文件。因此,用户应该可以解压缩文件(zip文件不需要密码保护)。但是,Excel需要受密码保护。当用户尝试解压文件时,他应该可以这样做。
当他尝试打开Excel文件(这是在解压缩的文件夹内)时,它必须要求一个密码。这个问题类似于使用java保护excel文件,增加了复杂性, Excel文件被压缩。

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.

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

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


推荐答案

uncompressing,它不可能的密码保护在zip文件内的excel。

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

这是你可以做的

  • Unzip the content using tips in What is the best way to extract a zip file using java and Compressing and Decompressing Data Using Java APIs
  • Password protect extracted excel file using tips in Password Protected Excel File
  • Zip the password protected excel file using tips in Java Compress Large File

这篇关于如何密码保护Java中的压缩Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆