2012-01-10 131 views
5

我有一个关于密码保护Excel文件的问题。如何在Java中密码保护压缩的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(); 
    } 
} 
+2

有可能是密码保护添加到压缩文件解压缩没有它的方式,但我知道处理它的唯一方法就是解压缩它,然后用密码保护重新压缩它。如果需要遵循压缩标准,则可能需要深入查看zip规范以了解如何处理密码保护。 – 2012-01-10 08:55:40

+0

为什么要压缩.xlsx文件? .xlsx已经是一个ziped文件,你不会获得很多! – AxFab 2012-01-18 21:57:56

+0

我不知道你的用例,但考虑到保护zip的密码是安全的,因为它不会仅仅为它添加“密码”,而是加密整个事物。另外考虑一些版本的excel是非常不安全的:http://www.excelforum.com/excel-programming/503874-how-safe-is-the-excel-password-functionality.html。如果安全性对您来说是个问题,您最好还是使用zip加密。 – 2012-01-19 08:07:52

回答

4
  • 使用java.util.zip或zip4j到文件解压缩到一些短期direcotry或内存,如果你知道它的小。
  • 然后使用Apache POI库中的HSSFWorkbook.writeProtectWorkbook
  • 再次压缩Excel工作簿。
+0

您确定POI库中有HSSFWorkbook.writeProtectPassword方法吗?因为我找不到它的下落。虽然看过'writeProtect'。 – eeerahul 2012-01-13 09:53:14

+0

对不起 - writeProtectWorkbook [API](http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html) – 2012-01-13 11:20:38

1

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

我建议你创建你的zip文件没有它的excel文件,创建你需要密码excel文件中的指示,你所提供的链接,然后用truezip写这个Excel文件存档。希望这有助于

相关问题