2012-01-10 89 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文件存檔。希望這有助於

相關問題