FileDocCategorySizeDatePackage
TarFile.javaAPI DocExample3355Sun Mar 07 11:53:16 GMT 2004None

TarFile

public class TarFile extends Object
Tape Archive Lister, patterned loosely after java.util.ZipFile. Since, unlike Zip files, there is no central directory, you have to read the entire file either to be sure of having a particular file's entry, or to know how many entries there are in the archive.
author
Ian Darwin, http://www.darwinsys.com/
version
$Id: TarFile.java,v 1.12 2004/03/07 17:53:15 ian Exp $

Fields Summary
protected boolean
read
True after we've done the expensive read.
protected Vector
list
The list of entries found in the archive
public static final int
RECORDSIZE
Size of header block.
protected int
blocking
protected int
blocksize
protected String
fileName
File containing archive
protected RandomAccessFile
is
The main datastream.
Constructors Summary
public TarFile(String name)
Construct (open) a Tar file by name


	        
	   
		fileName = name;
		list = new Vector();
	
public TarFile(File name)
Construct (open) a Tar file by File

		this(name.getCanonicalPath());
	
Methods Summary
public voidclose()

		try {
			is.close();
		} catch (IOException e) {
			// nothing to do
		}
	
public java.util.Enumerationentries()

		if (!read) {
			readFile();
		}
		return list.elements();
	
public TarEntrygetEntry(java.lang.String name)
Returns the Tar entry for the specified name, or null if not found.

		for (int i=0; i<list.size(); i++) {
			TarEntry e = (TarEntry)list.elementAt(i);
			if (name.equals(e.getName()))
				return e;
		}
		return null;
	
public java.io.InputStreamgetInputStream(TarEntry entry)
Returns an InputStream for reading the contents of the specified entry from the archive. May cause the entire file to be read.

		return null;
	
public java.lang.StringgetName()
Returns the path name of the Tar file.

		return fileName;
	
protected voidreadFile()
Read the Tar archive in its entirety. This is semi-lazy evaluation, in that we don't read the file until we need to. A future revision may use even lazier evaluation: in getEntry, scan the list and, if not found, continue reading! For now, just read the whole file.

	 	is = new RandomAccessFile(fileName, "r");
		TarEntry hdr;
		try {
			do {
				hdr = new TarEntry(is);
				if (hdr.getSize() < 0) {
					System.out.println("Size < 0");
					break;
				}
				// System.out.println(hdr.toString());
				list.addElement(hdr);
				// Get the size of the entry
				int nbytes = hdr.getSize(), diff;
				// Round it up to blocksize.
				if ((diff = (nbytes % RECORDSIZE)) != 0) {
					nbytes += RECORDSIZE - diff;
				}
				// And skip over the data portion.
				// System.out.println("Skipping " + nbytes + " bytes");
				is.skipBytes(nbytes);
			} while (true);
		} catch (EOFException e) {
			// OK, just stop reading.
		}
		// All done, say we've read the contents.
		read = true;
	
public intsize()
Returns the number of entries in the Tar archive. May cause the entire file to be read. XXX Obviously not written yet, sorry.

		return 0;