Password Protected Zip File in Java

Password protected zip file in java

Standard Java API does not support password protected zip files. Fortunately good guys have already implemented such ability for us. Please take a look on this article that explains how to create password protected zip.

(The link was dead, latest archived version: https://web.archive.org/web/20161029174700/http://java.sys-con.com/node/1258827)

How to create password protected zip/7zip file using apache.compress

Apache compress library does not support creating archives(zip/7zip files)

Create password protected zip file in Java without creating it on disk

I think we need to create file on disk.

How to unzip all the password protected zip files in a directory using Java

Thanks guys although none answered my question.
I found the answer I am posting this as there might be someone else who might be looking for the similar answer.

import java.io.File;
import java.util.List;

import javax.swing.filechooser.FileNameExtensionFilter;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.FileHeader;

public class SamExtraction {

public static void main(String[] args) {

final FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("N/A", "zip");
//Folder where zip file is present
final File file = new File("C:/Users/Desktop/ZipFile");
for (final File child : file.listFiles()) {
try {
ZipFile zipFile = new ZipFile(child);
if (extensionFilter.accept(child)) {
if (zipFile.isEncrypted()) {
//Your ZIP password
zipFile.setPassword("MYPASS!");
}
List fileHeaderList = zipFile.getFileHeaders();

for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
//Path where you want to Extract
zipFile.extractFile(fileHeader, "C:/Users/Desktop/ZipFile");
System.out.println("Extracted");
}
}
} catch (Exception e) {
System.out.println("Please Try Again");
}
}

}
}

How to create a password-protected zip folder in Java?

As stated here by WinZip, the Zip format encrypts the files inside the archive only and not the zip itself. http://kb.winzip.com/kb/entry/147/

However, as the article suggests, double zipping the archive will hide files inside the inner zip file. As long as you encrypt the outer archive.



Related Topics



Leave a reply



Submit