You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Peter Lee (Jira)" <ji...@apache.org> on 2020/04/24 03:48:00 UTC
[jira] [Comment Edited] (COMPRESS-511) Decompress tar failed.
java.io.IOException: Error detected parsing the header
[ https://issues.apache.org/jira/browse/COMPRESS-511?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17091142#comment-17091142 ]
Peter Lee edited comment on COMPRESS-511 at 4/24/20, 3:47 AM:
--------------------------------------------------------------
I checked the magic of the layer1.tar and find the magic code is 0x1F8B0800, which is the magic code of gzip.
This means that the layer1.tar is not a tar archive, but a tar.gz file. So you should read the layer1.tar as a tar.gz archive instead of tar archive.
I can successfully read the archive like this:
{code:java}
@Test
public void testReadTar() throws Exception {
//Send the TAR file as an argument
/******** This is a tar.gz archive instead of a tar archive ********/
String tarFileName = "/root/temp/layer1.tar";
/* Read TAR File into TarArchiveInputStream */
/******** This is a tar.gz archive instead of a tar archive ********/
TarArchiveInputStream myTarFile = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarFileName)));
/* To read individual TAR file */
TarArchiveEntry entry = null;
String individualFiles;
int offset;
FileOutputStream outputFile = null;
/* Create a loop to read every single entry in TAR file */
while ((entry = myTarFile.getNextTarEntry()) != null) { /* Get the name of the file */
individualFiles = entry.getName(); /* Get Size of the file and create a byte array for the size */
byte[] content = new byte[(int) entry.getSize()];
offset = 0; /* Some SOP statements to check progress */
System.out.println("File Name in TAR File is: " + individualFiles);
System.out.println("Size of the File is: " + entry.getSize());
System.out.println("Byte Array length: " + content.length); /* Read file from the archive into byte array */
myTarFile.read(content, offset, content.length - offset); /* Define OutputStream for writing the file */
}
}
{code}
was (Author: peterlee):
I checked the magic of the layer1.tar and find the magic code is 0x1F8B0800, which is the magic code of gzip.
This means that the layer1.tar is not a tar archive, but a tar.gz file. The tar.gz archive is supported by tar command, but unfortunely it's not supported by Commons Compress.
I used the command
{code:java}
gunzip layer1.tar
{code}
to get the actual tar file, then I can successfully read the tar file using Commons Compress like this:
{code:java}
@Test
public void testReadTar() throws Exception {
//Send the TAR file as an argument
String tarFileName = "/root/temp/layer1";
/* Read TAR File into TarArchiveInputStream */
TarArchiveInputStream myTarFile = new TarArchiveInputStream(new FileInputStream(new File(tarFileName)));
/* To read individual TAR file */
TarArchiveEntry entry = null;
String individualFiles;
int offset;
FileOutputStream outputFile = null;
/* Create a loop to read every single entry in TAR file */
while ((entry = myTarFile.getNextTarEntry()) != null) { /* Get the name of the file */
individualFiles = entry.getName(); /* Get Size of the file and create a byte array for the size */
byte[] content = new byte[(int) entry.getSize()];
offset = 0; /* Some SOP statements to check progress */
System.out.println("File Name in TAR File is: " + individualFiles);
System.out.println("Size of the File is: " + entry.getSize());
System.out.println("Byte Array length: " + content.length); /* Read file from the archive into byte array */
myTarFile.read(content, offset, content.length - offset); /* Define OutputStream for writing the file */
}
}{code}
> Decompress tar failed. java.io.IOException: Error detected parsing the header
> -----------------------------------------------------------------------------
>
> Key: COMPRESS-511
> URL: https://issues.apache.org/jira/browse/COMPRESS-511
> Project: Commons Compress
> Issue Type: Bug
> Components: Archivers
> Affects Versions: 1.20
> Reporter: Akhila Mangipudi
> Priority: Major
> Attachments: layer1.tar
>
>
> public static void main(String args[]) throws Exception {
> //Send the TAR file as an argument
> String tarFileName = args[0];
> /* Read TAR File into TarArchiveInputStream */
> TarArchiveInputStream myTarFile=new TarArchiveInputStream(new FileInputStream(new File(tarFileName)));
> /* To read individual TAR file */
> TarArchiveEntry entry = null;
> String individualFiles;
> int offset;
> FileOutputStream outputFile=null;
> /* Create a loop to read every single entry in TAR file */
> while ((entry = myTarFile.getNextTarEntry()) != null) {
> /* Get the name of the file */
> individualFiles = entry.getName();
> /* Get Size of the file and create a byte array for the size */
> byte[] content = new byte[(int) entry.getSize()];
> offset=0;
> /* Some SOP statements to check progress */
> System.out.println("File Name in TAR File is: " + individualFiles);
> System.out.println("Size of the File is: " + entry.getSize());
> System.out.println("Byte Array length: " + content.length);
> /* Read file from the archive into byte array */
> myTarFile.read(content, offset, content.length - offset);
> /* Define OutputStream for writing the file */
> outputFile=new FileOutputStream(new File(individualFiles));
> }
>
> I was able to decompress the tar file in the Linux environment using the tar command. Following is the error when I run the code:
>
> [ec2-user@ip-172-31-16-85 src]$ java TarToByteArray.java layer1.tar
> Error: Could not find or load main class TarToByteArray.java
> [ec2-user@ip-172-31-16-85 src]$ java TarToByteArray layer1.tar
> Exception in thread "main" java.io.IOException: Error detected parsing the header
> at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:371)
> at TarToByteArray.main(TarToByteArray.java:19)
> 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:215)
> at org.apache.commons.compress.archivers.tar.TarUtils.parseOctalOrBinary(TarUtils.java:179)
> at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:1350)
> at org.apache.commons.compress.archivers.tar.TarArchiveEntry.<init>(TarArchiveEntry.java:438)
> at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:369)
> ... 1 more
>
>
> Also attaching the tar file that I am using!
--
This message was sent by Atlassian Jira
(v8.3.4#803005)