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);
}