Java: checking a TAR file

Is it possible to check a tar file in Java so that I can verify that other tar utilities (like the UNIX tar command) can extract the tar? I would like to do this in code and not have to resort to execution tar -tf

from my application and report based on the return code.

I am using Apache compression to read through the gags writes (do not fetch them). I manually modified the tar part using a text editor to present the "corrupted" tar file. Commons compress does not always read the entries in which because the UNIX or 7zip tar file command failed sequentially no matter what I change. Obviously something else needs to be done to ensure that the tar file is not corrupted, but what is it?

Note: I have not actually tried to extract tar via commons compress as a verification tool. I don't think this is really an option; the tar file sizes I have to work with are large (3 to 4 gigs) and I would rather not see this process taking longer than it already does.

+3


source to share


1 answer


Since you don't want to execute the command tar

, you can use org.apache.tools.tar.TarInputStream

the Apache ANT implementation .

Personnel, I have not tried and I am not an expert on the TAR file format , but I would try to iterate over the TAR file using the method getNextEntry

. Assuming that after you're done with all without error / exception, you've (somewhat) proven that the TAR file is "valid".

UPDATE . The above Wikipedia page lists these Java implementations:



The Jtar includes an example of declaring a TAR file, which I suppose can only be changed to "check" the archive.

   String tarFile = "c:/test/test.tar";

   // Create a TarInputStream
   TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
   TarEntry entry;
   while((entry = tis.getNextEntry()) != null) {

      System.out.println(entry.getName());

      // The following might not be required, but arguably does 
      // additionally check the content of the archive.
      int count;
      byte data[] = new byte[2048];

      while((count = tis.read(data)) != -1) {
      }
   }

   tis.close();

      

0


source







All Articles