5

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();
    }
}
Community
  • 1
  • 1
eeerahul
  • 1,629
  • 4
  • 27
  • 38
  • 2
    There may be a way to add password protection to a compressed file without decompressing it, but the only way I know to handle it is to decompress it, then recompress it with password protection. You may have to dig into the zip spec to find out how password protection is handled if it needs to follow the zip standard. – Adam Davis Jan 10 '12 at 08:55
  • Why are you zipped an .xlsx file ? .xlsx is already a ziped file, you will not gain a lot ! – AxFab Jan 18 '12 at 21:57
  • I don't know your use-case, but just consider that password protecting a zip is safe, as it will not just add a "password" to it, but encrypt the whole thing. Also consider that some versions of excel are really unsafe: http://www.excelforum.com/excel-programming/503874-how-safe-is-the-excel-password-functionality.html . If security is a issue for you, you better stay with zip encryptation. – Gabriel Mazetto Jan 19 '12 at 08:07

3 Answers3

14

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

Here is what you can do

Community
  • 1
  • 1
Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82
4
  • 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.
Sorter
  • 9,704
  • 6
  • 64
  • 74
Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
  • Are you sure there is a HSSFWorkbook.writeProtectPassword method in POI library? Because I could not get the whereabouts of it. Have seen `writeProtect` though. – eeerahul Jan 13 '12 at 09:53
  • Sorry - writeProtectWorkbook [API](http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html) – Piotr Gwiazda Jan 13 '12 at 11:20
1

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

Yanki Twizzy
  • 7,771
  • 8
  • 41
  • 68