我正在尝试将 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;
}