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.
- 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.
Method 2: Efficient approach
- First, create a list of files to be added to the ZIP file and
- Specifying the path you can add it to the list.
Output: After running this code password-protected Zip file of specific files has been created at D:/myZipFile.zip.
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.