How to create password protected zip files | Zip and unzip directories | zip and unzip specific files in java | Unzip JAR file

In this post we’ll use library called Zip4j to
  • Create password protected zip files,
  • Zip and unzip directories
  • Zip and unzip specific files in java.


In this post we’ll create following programs >
  • Program 1 to create password protected zip files, Zip directory >
  • Program 2 to unzip password protected zip file (We will unzip zip file created in program 1) >
  • Program 3 to create password protected zip files, Zip specific files only >
  • Program 4 to unzip JAR file (We’ll use approach used in program 2)



Program 1 to create password protected zip files, Zip directory >
Suppose you have to zip "c:/myDirectory" and your directory structure is like this

  • All the files and sub-directories of directories will be zipped.
  • You will need to provide password for files only, not the directory or sub-directories.
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
public class CreatePasswordProtectedZipFile {
   public static void main(String[] args) {
          try {
                
                 // --------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:/myDirectory.zip";
                 // Create ZIP file
                 ZipFile zipFile = new ZipFile(destinationZipFilePath);
                 // Directory to be Zipped
                 String directoryToBeZipped = "C:/myDirectory";
                 // 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);
          } catch (ZipException e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
Password protected Zip file of Directory C:/myDirectory have been created at C:/myDirectory.zip
*/


After execution of above program C:/myDirectory.zip will be created and as I mentioned above that you will need to provide password for files only, not the directory or sub-directories.
Below Snapshot shows you need to provide password for opening c:\myDirectory.zip\myDirectory\myFile1.txt present in zip file, or any other file, but you may open c:\myDirectory.zip\myDirectory\sub-directory without any password.



Program 2 to unzip password protected zip file (We will unzip zip file created in program 1) >

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
public class UnzipPasswordProtectedZipFile {
   public static void main(String[] args) {
          try {
                 String sourceZipFilePath = "C:/myDirectory.zip";
                 String extractedZipFilePath = "C:/myDirectoryExtracted";
                 ZipFile zipFile = new ZipFile(sourceZipFilePath);
                 // check if file was encrypted, if encrypted
//then decrypt it using using password
                 if (zipFile.isEncrypted()) {
                       zipFile.setPassword("password");
                 }
                 //Now Extract ZIP file
                 zipFile.extractAll(extractedZipFilePath);
                 System.out.println(sourceZipFilePath + " have been extracted at "
                              + extractedZipFilePath);
          } catch (ZipException e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
C:/myDirectory.zip have been extracted at C:/myDirectoryExtracted
*/






At times you need to zip only specific files in that case
  • Create list of files to be added to ZIP file
  • Add SPECIFIC files to list by (adding path of files)

Now let’s create program for better understanding.


Program 3 to create password protected zip files, Zip specific files only >

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;
public class CreatePasswordProtectedZipFile {
   public static void main(String[] args) {
          try {
                 // --------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 file name
                 String destinationZipFilePath = "C:/myZipFile.zip";
                
                 // Create ZIP file
                 ZipFile zipFile = new ZipFile(destinationZipFilePath);
                 // Create list of files to be added to ZIP file
                 ArrayList<File> list = new ArrayList<File>();
                 //Add SPECIFIC  files to list
                 list.add(new File("C:/myFile1.txt"));
                 list.add(new File("C:/myFile2.txt"));
                 // pass (list of files to be added to ZIP file) and ZIP parameters
                 //for Zip file to be created
                 zipFile.addFiles(list, zipParameters);
                 System.out.println("Password protected Zip file of specific files "
                              + "have been created at "  + destinationZipFilePath);
          } catch (ZipException e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
Password protected Zip file of specific files have been created at C:/myZipFile.zip
*/

You may unzip file using the Program 2.



Program 4 to unzip JAR file (We’ll use approach used in program 2) >

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
public class UnzipJarFile {
   public static void main(String[] args) {
          try {
                 String sourceJarFilePath = "E:/java jars/zip4j_1.3.2.jar";
                 String extractedJarFilePath = "E:/java jars/zip4j_1.3.2";
                 ZipFile zipFile = new ZipFile(sourceJarFilePath);
                 //Now Extract JAR file
                 zipFile.extractAll(extractedJarFilePath);
                 System.out.println(sourceJarFilePath + " have been extracted at "
                              + extractedJarFilePath);
          } catch (ZipException e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
E:/java jars/zip4j_1.3.2.jar have been extracted at E:/java jars/zip4j_1.3.2
*/

If in case jar you are trying unzip is password protected then you must add set password of zip file [zipFile.setPassword("password")] like we did in Program 2

Labels: Core Java
eEdit
Must read for you :