Java取消压缩5G 7z文件

时间:2019-09-07 10:07:49

标签: java 7zip apache-commons-compress

我正在尝试使用Apache commons compress 1.9版解压缩5G文本文件...

public String unZipFile(String pathFileName, String saveFilePath)  throws IOException {

    SevenZFile sevenZFile = new SevenZFile(new File(pathFileName));
    SevenZArchiveEntry entry = sevenZFile.getNextEntry();

    try {

        while (entry != null) {

            if ("deudores.txt".equals(entry.getName())) {

                Instant now = Instant.now();
                LOG.info("\tunzipping: {} -> {}/{}", pathFileName, saveFilePath, entry.getName());

                byte[] buffer = new byte[1024];
                File newFile = newFile(new File(saveFilePath), entry.getName());
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = sevenZFile.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                LOG.info("\tunzipping done, took {} secs", now.until(Instant.now(), ChronoUnit.SECONDS));
                return saveFilePath+"/"+entry.getName();
            }
            entry = sevenZFile.getNextEntry();
        }
    } finally {
        sevenZFile.close();
    }
    return null;
}


 public File newFile(File destinationDir, String name) throws IOException {
    File destFile = new File(destinationDir, name);

    String destDirPath = destinationDir.getCanonicalPath();
    String destFilePath = destFile.getCanonicalPath();

    if (!destFilePath.startsWith(destDirPath + File.separator)) {
        throw new IOException("Entry is outside of the target dir: " + name);
    }

    return destFile;
}

我仅获得前1 GB

任何建议/替代方案?交换到另一个库不是问题。

已解决...在缓冲区上进行迭代是错误的。

0 个答案:

没有答案
相关问题