TarFilepublic 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. |
Fields Summary |
---|
protected boolean | readTrue after we've done the expensive read. | protected Vector | listThe list of entries found in the archive | public static final int | RECORDSIZESize of header block on tape. | protected int | blocking | protected int | blocksize | protected String | fileNameFile containing archive | protected RandomAccessFile | isThe main datastream. |
Constructors Summary |
---|
public TarFile(String name)Construct (open) a Tar file by name
fileName = name;
list = new Vector();
read = false;
| public TarFile(File name)Construct (open) a Tar file by File
this(name.getCanonicalPath());
|
Methods Summary |
---|
public void | close()
try {
is.close();
} catch (IOException e) {
// nothing to do
}
| public java.util.Enumeration | entries()
if (!read) {
readFile();
}
return list.elements();
| public TarEntry | getEntry(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.InputStream | getInputStream(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.String | getName()Returns the path name of the Tar file.
return null;
| protected void | readFile()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 -= diff; nbytes += RECORDSIZE;
}
// 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 int | size()Returns the number of entries in the Tar archive.
May cause the entire file to be read.
return 0;
|
|