This 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 file; 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 of native code as well, which make 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.
Zip4j provides 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) Download and Include Zip4j library into Project
To download the Zip4j library, go to its zip4j download page and download it’s latest version (zip4j_1.3.2.jar till today).
Add zip4j.jar file into your project’s classpath.
2) Example of creating password protected zip file
Now, you can use below code to create a password protected archive.
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(); } } }
There are other useful usecases, you can look into it’s source distribution [zip4j_examples_eclipse_1.3.2.zip] as well. Checkout this wonderfully useful library.
Happy Learning !!
Reference : http://www.lingala.net/zip4j/
Hi Lokesh,
Thanks for sharing useful example.
i have one requirement in my project like i need to send a mail with attached zipped password protected file.
i am using JavaMailSenderImpl & MimeMessageHelper to send a TXT, CSV & XML file in mail but now client requested to send in password protected zipped file.
i am trying to use your code but problem is MimeMessageHelper dose not allow to attach zipped file.
did lot of search but not able to come to the Solution.
could you please guide here if it possible ASAP.
Note :- i already have generated file in my application(File type) just need to zipped with password and send to mail.
Is there a program to decode, thank you very much
I guess… no.
Hi,
Whether this can be supported in java1.4? Are we able to do this password protected in Java 1.4 version. Kindly share if any example is there
I have not tried with 1.4 – I have no plan to find and install 1.4 as of now. Please try and share with us.
Hi there,
Very good example with proper comments.
Can you please help me making a “Exiting Zip file” password protected?
Thanks in advance!!
Hi Lokesh,
Again very clean and simple example. zip4j has a overloaded setpassword() with char[] to avoid immutable property of string.
Regards,
Himansu
Good catch. Thanks for sharing.