2

I'm developing a project in Eclipse with java 7 and I want compress a directory that it have many directories and files inside it and establish a password protected, i use for them zip4j library and it resolves my problem but not all because it only establish password to files inside of directories and not to root folder, in other words, i want that when we do double clic to the zip file automatically it request me write a password like windows S.O do it. Here is my code using above library:

public static void zipFile(String password) throws NoSuchAlgorithmException, ZipException
{   
    // --------Encryption zipParameters (for password protection)-------
    //Create ZipParameters

    ZipParameters zipParameters = new ZipParameters();

    // Set how you want to encrypt files
    zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

    // Set encryption of files to true
    zipParameters.setEncryptFiles(true);

    // Set encryption method
    zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
    // Set key strength
    zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

    // Set password

    zipParameters.setPassword(password);               


    // --------------------CREATE ZIP file - Zip DIRECTORY------------

    //Zip file name        
    String destinationZipFilePath = "C:/temp/FoldertoCompress.zip";

    // Create ZIP file
    ZipFile zipFile = new ZipFile(destinationZipFilePath);

    // Directory to be Zipped
    String directoryToBeZipped = "C:/FoldertoCompress";

    // pass (Directory to be Zipped) and ZIP parameters
    //for Zip file to be created
    zipFile.addFolder(directoryToBeZipped, zipParameters);

    System.out.println("Password protected Zip file of Directory "
           +directoryToBeZipped+" have been created at "+ destinationZipFilePath);          
}
Michał Grzejszczak
  • 2,599
  • 1
  • 23
  • 28
  • Do you have that behavior with other zip files ? – reos Dec 09 '15 at 17:19
  • No, is only one. It is take a directory with others files inside it, compress with zip and establish it a password for that nobody can see it directories structure without before write a password, do you understand?? – Pavel Rodriguez Acosta Dec 10 '15 at 16:37
  • I asked it because I think it's the normal behavior of a zip file with password. You can try it. Create a zip file with a tool and then try to open it. It'll let you browse the content but in the moment you want to open a file content it'll ask you for a password. – reos Dec 10 '15 at 16:42
  • Right, exactly it is that i don't want that it do, i want that the zip don't show the content when i opened!!!! – Pavel Rodriguez Acosta Dec 11 '15 at 15:02
  • Do you want to prevent user view the file name and other information without password? If yes, ZIP4J does not support this feature. – Beck Yang Feb 17 '16 at 09:24

0 Answers0