Create Password Protected Zip File in Java
Primarily, java does not have any function or package that will create a password-protected zip file without using some native codes. Java has some useful library which is good but they use some native codes to perform their task that makes their usage platform dependent to some extent. Whereas in the zip4j library we are just using java code without any support of native code, that is the reason that people are using jip4j over other built-in libraries.
There is a library called ‘zip4j’ which was written by “Srikanth Reddy Lingala” back in 2008/2009. It is the most comprehensive Java library for zip files.
Some important features of the zip4j library are as follows:
- It will create, add, extract, update, remove files from a zip file.
- It has support for streams like ZipInputStream and ZipOuputStream.
- We can use this library to create any password-protected zip file in java.
- We can zip/unzip any existing file or directory by just specifying the file path.
- It also provides supports for both AES 128/256 Encryption and standard Zip Encryption.
Note: ‘zip4j’ is written on JDK 8 and if we try to run this library on JDK 7, then not all features will be supported.
Methods:
- Naive approach using java.util.zip package
- Efficient approach
Method 1: Naive approach
We don’t have any standard java in-built library to create a password-protected zip file. We can only make a simple zip file with the help of the java.util.zip package.
Example
Java
import java.io.*;
import java.nio.file.*;
import java.util.zip.*;
public class ZipFile {
private static void zipFile(String filePath) {
try {
File file = new File(filePath);
String zipFileName = file.getName().concat( ".zip" );
FileOutputStream fos = new FileOutputStream(zipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry( new ZipEntry(file.getName()));
byte [] bytes = Files.readAllBytes(Paths.get(filePath));
zos.write(bytes, 0 , bytes.length);
zos.closeEntry();
zos.close();
}
catch (FileNotFoundException ex) {
System.err.format( "The file does not exist" , filePath);
}
catch (IOException ex) {
System.err.println( "ERROR: " + ex);
}
}
public static void main(String[] args) {
String filePath = args[ 0 ];
zipFile(filePath);
}
}
|
Method 2: Efficient approach
Procedure:
- First, create a list of files to be added to the ZIP file and
- Specifying the path you can add it to the list.
Example
Java
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 GFG {
public static void main(String[] args) {
try {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipParameters.setEncryptFiles( true );
zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
zipParameters.setPassword( "password" );
String destinationZipFilePath = "D:/myZipFile.zip" ;
ZipFile zipFile = new ZipFile(destinationZipFilePath);
ArrayList<File> list = new ArrayList<File>();
list.add( new File( "D:/myFile1.txt" ));
list.add( new File( "D:/myFile2.txt" ));
zipFile.addFiles(list, zipParameters);
System.out.println( "Password protected Zip file"
+ "have been created at " + destinationZipFilePath);
} catch (ZipException e) {
e.printStackTrace();
}
}
}
|
Output: After running this code password-protected Zip file of specific files has been created at D:/myZipFile.zip.
Output explanation:
Before execution in the above image, we can see 1 directory and 2 files named GeeksForGeeks.txt and krishana_97
Now after execution, we can see the new zip file created. This zip file is the password protested zip file. You have to give the correct password to access the file inside this zip.
Last Updated :
23 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...