使用Java将文件解压缩到目录

时间:2018-09-12 22:35:03

标签: java nullpointerexception zip bukkit

因此,基本上来说,总结是,我将从一个文件夹中提取一个.zip文件到另一个文件夹中。我为此使用Java示例代码,因为我以前从未使用过java.util.zip。看起来很简单,但是我的代码仍然遇到一些奇怪的问题

好的,

package com.supercas240.RaidEvents.Main;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipUtility {
    /**
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;
    /**
     * Extracts a zip file specified by the zipFilePath to a directory specified by
     * destDirectory (will be created if does not exists)
     * @param zipFilePath
     * @param destDirectory
     * @throws IOException
     */
    public void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }

        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();

        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }
    /**
     * Extracts a zip entry (file entry)
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }
}

是我提取文件的类设置。当前正在测试中,在主类中,我通过以下方式调用该类:

public class Main extends JavaPlugin implements Listener {


public String zipFilePath = this.getServer().getWorldContainer().getAbsolutePath() + "\\plugins\\RaidEvents\\world_RaidEvents.zip";
public String destDirectory = this.getServer().getWorldContainer().getAbsolutePath();

UnzipUtility unzip = new UnzipUtility();

public void onEnable() {



    try {
        unzip.unzip(zipFilePath, destDirectory);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

在我解释我的错误不适之前,请先显示它:

error

所以代码基本上要做的是:它获取目标目录,如果不存在,它将成为目标目录:

File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();
    }

然后,它开始遍历.zip中的所有文件,如果它是目录,则创建它;如果是它的文件,它将复制它:

ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();

    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }

所以这似乎行得通吗?仍然,如果我们查看错误,似乎是在遍历目录时并没有创建目录,并且当目录到达villagers.dat时,找不到目录来放置它?我现在有点迷路了。 zip文件目录正确,称为“ world_RaidEvents.zip”。

0 个答案:

没有答案