java.io.IOException : Error detected parsing the header , 在 Java 中解压 .tgz 文件

标签 java apache tar apache-commons-compress

我正在使用 apache commons 解压 .tgz 文件。我从压缩库中收到错误消息。我已经尝试压缩版本 1.9 和 1.8.1,但我仍然遇到相同的错误。

这只发生在某些文件上,但更重要的是当我手动下载文件并验证它时,没有任何问题。

$ gunzip -c foo.tgz | tar t > /dev/null
$ 

但是我看到这个堆栈跟踪来自公共(public)库。

Severe:   java.io.IOException: Error detected parsing the header
at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:257)    
...
Caused by: java.lang.IllegalArgumentException: Invalid byte 0 at offset 5 in '05412{NUL}11' len=8
at org.apache.commons.compress.archivers.tar.TarUtils.parseOctal(TarUtils.java:138)

问题出在线路上

entry = tarIn.getNextTarEntry();

代码如下:

try {
        TarArchiveInputStream tarIn = new TarArchiveInputStream(
                new GZIPInputStream(
                        new BufferedInputStream(
                                new FileInputStream(
                                        tempDirPath + fileName))));

        TarArchiveEntry entry = tarIn.getNextTarEntry();

        while (entry != null) {
            File path = new File(tempDirPath, entry.getName());
            if (entry.isDirectory()) {
                path.mkdirs();
            } else {          
                path.createNewFile();
                byte[] read = new byte[1024];
                BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(path));
                int len;
                while ((len = tarIn.read(read)) != -1) {
                    bout.write(read, 0, len);
                }
                bout.close();
                read = null;
            }
            entry = tarIn.getNextTarEntry();
        }
        tarIn.close();
    } catch (Throwable t) {
        t.printStackTrace();
    }

最佳答案

尝试使用 GzipCompressorInputStream 装饰您的 inputStream。

TarArchiveInputStream tarIn = new TarArchiveInputStream( 新 GzipCompressorInputStream(新 FileInputStream(文件名)))

关于java.io.IOException : Error detected parsing the header , 在 Java 中解压 .tgz 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28487076/

相关文章:

java - 需要统一的浏览器引擎

Java-Calculator 多计算

php - 为规范关闭全局 XSS 过滤。 Controller ?

zip - 解压具有法语名称文件的文件

java - 从 Java 进程生成 tar 文件时 tar 命令卡住了

java - 如何刷新表格中的单行?

regex - 301 将非 www 和 www 重定向到新域,但排除主页/root

Apache 重定向到一个干净的 URL

ubuntu - 如何为 tar 命令选择目标目录

java - 如何在 JUnit XML 报告上配置名称和类名属性