在尝试使用资源时丢失ZLIB inputstream失败

时间:2018-10-17 08:32:22

标签: java-8 unzip

我具有用于解压缩具有多个条目的zip的功能。有时会捕获到一个异常,指出“ ZLIB输入流的意外结束”。我认为这是不可能的,因为我正在尝试使用resrsources。

private boolean decompress(final Path blf) {

    final String pathToZip = blf.getParent().toString();

    final byte[] buffer = new byte[8192];

    try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(blf.toFile()))) {

        //We will unzip files in this folder
        if (!Files.exists(Paths.get(pathToZip))) {
            Files.createDirectory(Paths.get(pathToZip));
        }

        ZipEntry entry = zipInputStream.getNextEntry();
        final Path pathToFile = Paths.get(pathToZip + "\\" + entry.getName());

        //Faster procedure if the file already exists
        if (Files.exists((pathToFile))) {
            loggerAnalysis.log(new LogRecord(Level.INFO, "[Analysis]",
                    "File already exists, skipping" + pathToFile));
            return true;
        }
        //Iterate over entries
        while (entry != null) {

            //If directory then create a new directory in uncompressed folder
            if (entry.isDirectory()) {
                loggerAnalysis.log(new LogRecord(Level.INFO, "[Analysis]",
                        "Creating Directory:" + pathToFile));

                Files.createDirectories(pathToFile);
            } else {
                Files.createFile(pathToFile);
                loggerAnalysis.log(new LogRecord(Level.INFO, "[Analysis]",
                        "File unzip: " + pathToFile.getFileName()));

                try (FileOutputStream fileOutputStream = new FileOutputStream(pathToFile.toString())) {
                    int len;

                    while ((len = zipInputStream.read(buffer)) > 0) {
                        fileOutputStream.write(buffer, 0, len);
                    }
                }
                entry = zipInputStream.getNextEntry();
            }
        }
        return true;
    } catch (final IOException e) {
        loggerAnalysis.log(new LogRecord(Level.ERROR, "[Analysis]", e.getMessage()));
        return false;
    }
}

最好的问候

0 个答案:

没有答案