3

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

这仅发生在某些文件上,但关键是当我手动下载文件并验证它时,没有问题。

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

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

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();
    }
4

1 回答 1

5

尝试使用GzipCompressorInputStream装饰您的 inputStream 。

TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(fileName)))

于 2018-09-07T09:26:03.103 回答