简体   繁体   中英

Unzip password protected zip file using Java throws NullPointerException

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");
                }
            }

        }
    }

The answer is a line above your File instantiation

//Folder where zip file is present
final File file = new File("C:/Users/zxc/Desktop/ziptest/ziptest2.zip");

You need to define the folder where your zip file is present, not the zip file itself

final File file = new File("C:/Users/zxc/Desktop/ziptest");

You don't need to iterate the File handler. Just pass your file object while creating ZipFile object.

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(file);
            if (extensionFilter.accept(file)) {
                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");
        }
    //}

}

Simple, lets have a look at the javadoc :

public File[] listFiles()

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null.

You gave a filename, and ask for its paths . Files dont have paths. So null is returned, and for (whatever : null) results in a NPE.

So the real point here: read the documentation, instead of blindly putting down some code without understanding what that code is supposed to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM