2012-01-10 47 views
5

Tôi có câu hỏi về mật khẩu bảo vệ tệp Excel.Làm thế nào để bảo vệ mật khẩu một tệp Excel đã nén trong Java?

Tình huống là, tôi có tệp zip, có tệp Excel trong đó. Tôi cần viết một chương trình Java, để bảo vệ mật khẩu tệp Excel. Do đó, người dùng có thể giải nén tệp (tệp zip không cần phải được bảo vệ bằng mật khẩu). Tuy nhiên, Excel cần phải được bảo vệ bằng mật khẩu. Khi người dùng cố gắng giải nén tệp, anh ấy có thể làm như vậy. Và khi anh ta cố mở tệp Excel (nằm trong thư mục đã giải nén), nó phải yêu cầu mật khẩu. Câu hỏi tương tự như Protect excel file with java, với độ phức tạp thêm vào đó, tệp Excel được nén.

Tôi có mã, mật khẩu đó chỉ bảo vệ tệp zip, nhưng đây không phải là những gì tôi muốn.

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

Có thể có cách thêm bảo vệ mật khẩu vào tệp nén mà không giải nén , nhưng cách duy nhất tôi biết để xử lý nó là giải nén nó, sau đó recompress nó với mật khẩu bảo vệ. Bạn có thể phải thâm nhập vào thông số zip để tìm hiểu cách bảo vệ mật khẩu được xử lý nếu nó cần tuân thủ tiêu chuẩn zip. –

+0

Tại sao bạn nén tệp .xlsx? .xlsx đã là một tệp nén, bạn sẽ không đạt được nhiều thứ! – AxFab

+0

Tôi không biết trường hợp sử dụng của bạn, nhưng chỉ xem xét rằng mật khẩu bảo vệ một zip là an toàn, vì nó sẽ không chỉ thêm một "mật khẩu" vào nó, nhưng mã hóa toàn bộ điều. Cũng nên xem xét rằng một số phiên bản của excel thực sự không an toàn: http://www.excelforum.com/excel-programming/503874-how-safe-is-the-excel-password-functionality.html. Nếu bảo mật là một vấn đề cho bạn, bạn nên ở lại với mã hóa zip. –

Trả lời

14

Nếu không giải nén, Không thể bảo vệ mật khẩu excel nằm trong tệp zip.

Đây là những gì bạn có thể làm

4
  • Sử dụng java.util.zip hoặc zip4j để giải nén tập tin vào một số direcotry temp hoặc vào bộ nhớ, nếu bạn biết nó nhỏ.
  • Sau đó, sử dụng HSSFWorkbook.writeProtectWorkbook từ thư viện Apache POI
  • Nén sổ làm việc Excel một lần nữa.
+0

Bạn có chắc chắn có phương pháp HSSFWorkbook.writeProtectPassword trong thư viện POI không? Bởi vì tôi không thể có được nơi ở của nó. Đã thấy 'writeProtect' mặc dù. – eeerahul

+0

Xin lỗi - writeProtectWorkbook [API] (http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html) –

1

Tôi nghĩ bạn nên kiểm tra phần mềm thực (Truezip website). Nó cung cấp truy cập đọc/ghi vào ZIP, JAR, EAR, WAR vv và hỗ trợ thêm vào các tệp ZIP hiện có.

Tôi khuyên bạn nên tạo tệp zip của mình mà không có tệp excel trong đó, tạo tệp excel được mật khẩu của bạn theo hướng dẫn trong liên kết bạn cung cấp và sau đó sử dụng tệp truezip để ghi tệp excel này vào lưu trữ. Hy vọng điều này sẽ giúp

Các vấn đề liên quan