2

我正在尝试将 tar 文件解压缩为map使用 Apache commons compress in Java。我能够解压缩大多数 tar 文件,但很少有因以下异常而失败。我不确定是什么导致了这个问题。tar 文件是否损坏?我可以在 Windows 中使用 7zip 解压文件,但是当以编程方式解压文件时,相同的文件会失败。我正在使用 Appache commons-compress 1.18

java.io.IOException: Error detected parsing the header
at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:285)
at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextEntry(TarArchiveInputStream.java:552)

Caused by: java.lang.IllegalArgumentException: At offset 124, 12 byte binary number exceeds maximum signed long value
at org.apache.commons.compress.archivers.tar.TarUtils.parseBinaryBigInteger(TarUtils.java:213)
at org.apache.commons.compress.archivers.tar.TarUtils.parseOctalOrBinary(TarUtils.java:177)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:1283)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:1266)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.<init>(TarArchiveEntry.java:404)
at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:283)
... 25 more

下面是我的代码

public static Map<String, byte[]> unTarToMap(byte[] b) throws IOException, ArchiveException {
        final Map<String, byte[]> untaredFiles = new HashMap<>();
        ByteArrayInputStream is = new ByteArrayInputStream(b);
        final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final ByteArrayOutputStream outputFileStream = new ByteArrayOutputStream();
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
            untaredFiles.put(entry.getName(), outputFileStream.toByteArray());
        }
        debInputStream.close();
        return untaredFiles;
    }
4

1 回答 1

1

您可能遇到了 Commons Compress 的限制。在其标头的偏移量 124 处,一个 tar 条目存储其大小。Commons Compress 尝试将大小表示为 Java long,它的最大值非常大 (2^63-1),但理论上 tar 条目可能更大。

要么你有一个包含这么大条目的 tar 存档(7z 应该能够告诉你它认为条目有多大),要么你遇到了一个错误。tar 有许多不同的方言,Commons Compress 很可能认为您的存档使用的是特定方言,而实际上并非如此。在这种情况下,最好在https://issues.apache.org/jira/projects/COMPRESS/上使用 Apache Commons Compress 打开错误报告,并 - 如果可能 - 提供导致异常的存档。

顺便说一句,堆栈跟踪中的行号与 Compress 1.18 不匹配,因此您可能没有使用您认为的版本。

于 2019-01-13T06:22:34.477 回答