O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Program: TarList (File Converter)

This program provides easy access to tar -format files using an interface similar to that used for zip archives in Section 9.19. Unix users will be familiar with the tar program, an archiver first written back in the mid-1970s. And JDK users might find the tar program syntax somewhat familiar, as it was the basis for the command-line Java Archiver (jar) program in the JDK, written 20 years later. If you’re not a Unix user, don’t dismay: just think of this as an example of a whole category of programs, those that need to repetitively read and write files in a special-purpose, predefined format. MS-Windows is full of special-purpose file formats, as are many other operating systems. Unlike jar, tar is just an archiver, not a combined archiver and compressor, so its format is somewhat simpler. In this section we’ll develop a program that reads a tar archive and lists the contents. The TarList program combines several reading methods with several formatting methods. So the commands:

tar -xvf demo.tar
java TarList demo.tar

should produce the same output. And indeed they do, at least for some files and some versions of tar, when run on a small tar archive:

$ java TarList demo.tar -rwxr-xr-x ian/wheel 734 1999-10-05 19:10 TarDemo.class -rwxr-xr-x ian/wheel 431 1999-10-05 19:10 TarList.java -rw-r--r-- ian/wheel 0 1999-10-05 19:10 a -rw-r--r-- ian/wheel 0 1999-10-05 19:10 b link to a lrwxr-xr-x ian/wheel 0 1999-10-05 19:10 c -> a $ tar -tvf demo.tar -rwxr-xr-x ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required