I need to unzip a password protected zip file.
so far I got this with the help of internet and im trying to understand the code.
but it throws nullpointerexception. and I'm really confused. is it the "child" or something else. please kindly explain.
Exception in thread "main" java.lang.NullPointerException at unzip2.Unzip2.main(Unzip2.java:27) C:\Users\zxc\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)
it says that it's on line 27.
for (final File child : file. listFiles()) {
as far as i know, nullpointerexception means that my variable is null but i cant get where is the null here and how to fix it
here's my complete code. please enlighten me.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package unzip2;
/**
*
* @author zxc
*/
import java.io.File;
import java.util.List;
import javax.swing.filechooser.FileNameExtensionFilter;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
public class Unzip2 {
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/zxc/Desktop/ziptest/ziptest2.zip");
for (final File child : file.listFiles()) {
try {
ZipFile zipFile;
zipFile = new ZipFile(child);
if (extensionFilter.accept(child)) {
if (zipFile.isEncrypted()) {
//Your ZIP password
zipFile.setPassword("password");
}
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/zxc/Desktop/zipfile");
System.out.println("Extracted");
}
}
} catch (ZipException e) {
System.out.println("Please Try Again");
}
}
}
}