簡體   English   中英

使用 Apache Commons compress 解壓 Tar 文件時發生異常

[英]Exception happened when untarring the Tar file using 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;
    }

您可能會遇到 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 不匹配,因此您可能沒有使用您認為的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM