Apache Commons Compress: Opening .tar.gz
Asked Answered
B

1

6

I'm developing a software that will get information from tar.gz files and I'm using the Apache commons-compress lib. But I'm getting the following error:

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

The sample tar.gz file used is eclipse-jee-luna-SR1-linux-gtk-x86_64.tar.gz

Here is the class that uses the 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;
    }
}
Beecher answered 26/1, 2015 at 20:26 Comment(3)
To get any kind of meaningful answer, you'll have to provide the code you wrote that is using commons-compress.Aegeus
I bet the bug is in your code and not in Apache CommonsPuzzle
What version of Apache commons compress lib do you use - is it 1.3 ?Catholicity
L
5

You read the file, which is not a TAR but a GZ stream. You need to first uncompress it before you can use the TarArchiveInputStream.

tarArchiveInputStream = new TarArchiveInputStream(
                          new GzipCompressorInputStream(
                            new BufferedInputStream(
                              new FileInputStream(fileName))));
Lennox answered 26/1, 2015 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.