【问题标题】:Apache Commons Compress: Opening .tar.gzApache Commons Compress:打开 .tar.gz
【发布时间】:2015-03-25 08:08:42
【问题描述】:

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

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

这是使用 lib 的类:

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;
    }
}

【问题讨论】:

  • 要获得任何有意义的答案,您必须提供使用 commons-compress 编写的代码。
  • 我敢打赌,错误出在您的代码中,而不是在 Apache Commons 中
  • 您使用什么版本的 Apache commons 压缩库 - 是 1.3 吗?

标签: java apache-commons-compress


【解决方案1】:

您读取的文件不是 TAR,而是 GZ 流。使用TarArchiveInputStream之前需要先解压。

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

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 2013-01-05
    • 2013-09-04
    相关资源
    最近更新 更多