FileDocCategorySizeDatePackage
TarList.javaAPI DocExample3156Sat Nov 25 12:56:26 GMT 2000None

TarList

public class TarList extends Object
Demonstrate the Tar archive lister.
author
Ian F. Darwin, ian@darwinsys.com
version
$Id: TarList.java,v 1.4 2000/11/25 17:56:26 ian Exp $

Fields Summary
TarFile
tf
The TarFile we are reading
protected StringBuffer
sb
protected static int[]
shft
Shift used in formatting permissions
protected static String[]
rwx
Format strings used in permissions
NumberFormat
sizeForm
NumberFormat used in formatting List form string
Date
date
Date used in printing mtime
SimpleDateFormat
dateForm
Constructors Summary
public TarList(String fileName)
Constructor

		tf = new TarFile(fileName);
	
Methods Summary
public voidlist()
Generate and print the listing

		Enumeration list = tf.entries();
		while (list.hasMoreElements()) {
			TarEntry e = (TarEntry)list.nextElement();
			System.out.println(toListFormat(e));
		}
	
public static voidmain(java.lang.String[] argv)

		if (argv.length == 0) {
			System.err.println("Usage: TarList archive");
			System.exit(1);
		}
		new TarList(argv[0]).list();
	
public java.lang.StringtoListFormat(TarEntry e)
Format a TarEntry the same way that UNIX tar does


	           
	    
		sb = new StringBuffer();
		switch(e.type) {
			case TarEntry.LF_OLDNORMAL:
			case TarEntry.LF_NORMAL:
			case TarEntry.LF_CONTIG:
			case TarEntry.LF_LINK:		// hard link: same as file
				sb.append('-");	// 'f' would be sensible
				break;
			case TarEntry.LF_DIR:
				sb.append('d");
				break;
			case TarEntry.LF_SYMLINK:
				sb.append('l");
				break;
			case TarEntry.LF_CHR:		// UNIX device file
				sb.append('c");
				break;
			case TarEntry.LF_BLK:		// UNIX device file
				sb.append('b");
				break;
			case TarEntry.LF_FIFO:		// UNIX named pipe
				sb.append('p");
				break;
			default:			// Can't happen?
				sb.append('?");
				break;
		}

		// Convert e.g., 754 to rwxrw-r--
		int mode = e.getMode();
		for (int i=0; i<3; i++) {
			sb.append(rwx[mode >> shft[i] & 007]);
		}
		sb.append(' ");

		// owner and group
		sb.append(e.getUname()).append('/").append(e.getGname()).append(' ");

		// size
		// DecimalFormat can't do "%-9d", so we do part of it ourselves
		sb.append(' ");
		String t = sizeForm.format(e.getSize());
		boolean digit = false;
		char c;
		for (int i=0; i<8; i++) {
			c = t.charAt(i);
			if (!digit && i<(8-1) && c == '0")
				sb.append(' ");		// leading space
			else {
				digit = true;
				sb.append(c);
			}
		}
		sb.append(' ");

		// mtime
		// copy file's mtime into Data object (after scaling
		// from "sec since 1970" to "msec since 1970"), and format it.
		date.setTime(1000*e.getTime());
		sb.append(dateForm.format(date)).append(' ");

		sb.append(e.getName());
		if (e.isLink())
			sb.append(" link to " ).append(e.getLinkName());
		if (e.isSymLink())
			sb.append(" -> " ).append(e.getLinkName());

		return sb.toString();