使用Apache Commons压缩解压缩Tar文件时发生异常

时间:2019-01-11 08:28:20

标签: java apache-commons-compress

我正在尝试使用Java中的Apache commons压缩将tar文件解压缩到map。我能够解压缩大多数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;
    }

1 个答案:

答案 0 :(得分:1)

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

要么您的tar存档中的条目很大(7z应该可以告诉您认为条目的大小),要么您遇到了bug。 tar有许多不同的方言,Common Compress很有可能会认为您的存档使用的不是特定的方言。在这种情况下,最好使用https://issues.apache.org/jira/projects/COMPRESS/上的Apache Commons Compress打开错误报告,并在可能的情况下,提供引起异常的档案。

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