简体   繁体   English

使用Java解压缩受密码保护的zip文件会引发NullPointerException

[英]Unzip password protected zip file using Java throws NullPointerException

I need to unzip a password protected zip file. 我需要解压缩受密码保护的zip文件。

so far I got this with the help of internet and im trying to understand the code. 到目前为止,我在互联网的帮助下得到了这个,我试图理解代码。

but it throws nullpointerexception. 但它抛出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) unzip2.Unzip2.main(Unzip2.java:27)处的线程“主”中的java.lang.NullPointerException异常C:\\ Users \\ zxc \\ AppData \\ Local \\ NetBeans \\ Cache \\ 8.2 \\ executor-snippets \\ run.xml:53 :Java返回:1 BUILD FAILED(总时间:0秒)

it says that it's on line 27. 它说它在第27行。

for (final File child : file. listFiles()) { 对于(最终文件子级:文件。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 据我所知,nullpointerexception意味着我的变量为null,但我无法获得null在哪里以及如何解决它

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 答案是在File实例化上方的一行

//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 您需要定义zip文件所在的文件夹,而不是zip文件本身

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

You don't need to iterate the File handler. 您不需要迭代File处理程序。 Just pass your file object while creating ZipFile object. 在创建ZipFile对象时只需传递file对象即可。

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 : 简单,让我们看一下javadoc

public File[] listFiles() 公共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. 如果此抽象路径名不表示目录,则此方法返回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. 因此返回null,并且for (whatever : null)导致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. 因此,这里的重点是:阅读文档,而不是盲目地放下一些代码而不了解该代码应该做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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