gpt4 book ai didi

java - Apache Commons 压缩 : Opening . tar.gz

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:16 33 4
gpt4 key购买 nike

我正在开发一个将从 tar.gz 文件中获取信息的软件,并且我正在使用 Apache commons-compress 库。但是我收到以下错误:

Caused by: java.lang.IllegalArgumentException: Invalid byte 4 at offset 0 in 'O�!�C' len=8
at org.apache.commons.compress.archivers.tar.TarUtils.parseOctal(TarUtils.java:134)
at org.apache.commons.compress.archivers.tar.TarUtils.parseOctalOrBinary(TarUtils.java:166)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:953)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:940)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.<init>(TarArchiveEntry.java:324)
at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:274)
... 2 more

使用的示例 tar.gz 文件是 eclipse-jee-luna-SR1-linux-gtk-x86_64.tar.gz

这是使用库的类:

public class TarGzBuildAdapter extends BuildAdapter {
public TarGzBuildAdapter(File build) {
super(build);
}

@Override
public List<ArtifactInfo> getArtifactInfos() throws IOException {
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
new FileInputStream(this.build));
TarArchiveEntry tarArchiveEntry;
List<ArtifactInfo> artifactInfos = new LinkedList<ArtifactInfo>();

while ((tarArchiveEntry = tarArchiveInputStream.getNextTarEntry()) != null) {
System.out.println(String.format("Name: %s LinkName: %s Size: %Ld RealSize: %Ld",
tarArchiveEntry.getName(), tarArchiveEntry.getLinkName(),
tarArchiveEntry.getSize(), tarArchiveEntry.getRealSize()));
artifactInfos.add(new ArtifactInfo(tarArchiveEntry.getName(), tarArchiveEntry
.getRealSize()));
}

tarArchiveInputStream.close();

return artifactInfos;
}
}

最佳答案

您读取了文件,它不是 TAR 而是 GZ 流。您需要先解压缩它,然后才能使用 TarArchiveInputStream

tarArchiveInputStream = new TarArchiveInputStream(
new GzipCompressorInputStream(
new BufferedInputStream(
new FileInputStream(fileName))));

关于java - Apache Commons 压缩 : Opening . tar.gz,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28158140/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com