This Java tutorial covers creating password-protected zip files using a very useful library zip4j
. Java, by default, does not provide any support for password protection for files; though it has very good API support for creating/extracting zip files.
There are some other useful libraries out there, which are equally good and sometimes better than zip4j, but they use some native code as well, which makes their usage platform dependent to some extent. Zip4j uses completely Java code without any support of native code, and that’s what makes it a better fit for me.
1. Zip4j Library
1.1. Features
Zip4j provides the following features:
- Create, Add, Extract, Update, Remove files from a Zip file
- Read/Write password protected Zip files
- Supports AES 128/256 Encryption
- Supports Standard Zip Encryption
- Supports Zip64 format
- Supports Store (No Compression) and Deflate compression method
- Create or extract files from Split Zip files (Ex: z01, z02,…zip)
- Supports Unicode file names
- Progress Monitor
1.2. Dependency
Download the latest maven dependency for zip4j in the project.
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.10.0</version>
</dependency>
2. Creating Password Protected Zip Files
The given below is a very simple example of creating a password-protected zip file using the library. See its Git page for extended options and more examples.
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 CreatePasswordProtectedZipExample
{
public static void main(String[] args)
{
try {
//This is name and path of zip file to be created
ZipFile zipFile = new ZipFile("C:/temp/test.zip");
//Add files to be archived into zip file
ArrayList<File> filesToAdd = new ArrayList<File>();
filesToAdd.add(new File("C:/temp/test1.txt"));
filesToAdd.add(new File("C:/temp/test2.txt"));
//Initiate Zip Parameters which define various properties
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
//DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression
//DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression
//DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed
//DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed
//DEFLATE_LEVEL_ULTRA - Highest compression level but low speed
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//Set the encryption flag to true
parameters.setEncryptFiles(true);
//Set the encryption method to AES Zip Encryption
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
//AES_STRENGTH_128 - For both encryption and decryption
//AES_STRENGTH_192 - For decryption only
//AES_STRENGTH_256 - For both encryption and decryption
//Key strength 192 cannot be used for encryption. But if a zip file already has a
//file encrypted with key strength of 192, then Zip4j can decrypt this file
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
//Set password
parameters.setPassword("howtodoinjava");
//Now add files to the zip file
zipFile.addFiles(filesToAdd, parameters);
}
catch (ZipException e)
{
e.printStackTrace();
}
}
}
Happy Learning !!