tar.gz解压缩失败使用apache commons-compress

时间:2014-10-31 12:05:57

标签: java tar compression

嗨大家我正在使用apache Commons-compress 1.9来压缩和解压缩tar.gz文件。现在解压缩文件时我面临的问题。在解压缩时,它说压缩文件夹中的所有文件都没有找到。以下是解压缩代码

public static boolean uncompressTarGz(String tarFileName, String outputDir, Logger log) throws IOException {

    try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarFileName)))) {
        TarArchiveEntry tarEntry = (TarArchiveEntry) tarArchiveInputStream.getNextEntry();
        log.info("OutputDir : " + outputDir);
        log.info("Tar File Name : " + tarFileName);
        Files.createDirectories(Paths.get(outputDir));
        while (tarEntry != null) {
            log.info("Tarentry : " + tarEntry.getName());
            File destPath = new File(outputDir, tarEntry.getName());
            log.info("1");
            if (!tarEntry.isDirectory()) {
                destPath.createNewFile();
                log.info("2");
                try (FileOutputStream fout = new FileOutputStream(destPath);) {

                    IOUtils.copy(tarArchiveInputStream, fout);
                } catch (Throwable e) {
                    log.info("Error : " + e + " reason : " + e.getMessage());
                }

                log.info("3");

                // byte[] buffer = new byte[8192];
                // int n = 0;
                // while (-1 != (n = tarArchiveInputStream.read(buffer))) {
                // fout.write(buffer, 0, n);
                // }
            } else {
                destPath.mkdir();
            }
            tarEntry = (TarArchiveEntry) tarArchiveInputStream.getNextEntry();
        }

        return true;
    } catch (Throwable e) {
        log.info("Exception while untar : " + e.getMessage());
        return false;
    }
}

我收到错误 / tmp / customTAR / customfiles / filesSucceeded(没有这样的文件或目录)

任何人都可以帮我弄清楚我可能做错了什么。

提前致谢。

0 个答案:

没有答案