//============================================================================== // ZFile.java //============================================================================== package tribble.search.zip; // System imports import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.Exception; import java.lang.IllegalArgumentException; import java.lang.Long; import java.lang.String; import java.util.ConcurrentModificationException; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; // Local imports import tribble.search.DocumentI; /******************************************************************************* * Zipfile data file. * *

* This represents the contents of an entry found within a zipfile. * * * @version $Revision: 1.6 $ $Date: 2001/07/02 22:40:58 $ * @since 2001-05-18 * @author * David R. Tribble * (david@tribble.com). *
* Copyright * ©2001 by David R. Tribble, all rights reserved.
* * @see ZArchive */ public class ZFile implements tribble.search.DocumentI { // Identification /** Revision information. */ static final String REV = "@(#)tribble/search/zip/ZFile.java $Revision: 1.6 $ $Date: 2001/07/02 22:40:58 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constants /** Series number. */ public static final int SERIES = 300; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected constants /** Zipfile entry key attribute names. */ protected static final String[] s_attrNames = { "comment", "compressed.size", "crc", "extra", "method", "name", "size", "time", }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected variables /** Searcher that found and created this zipfile entry. */ protected ZArchive m_searcher; /* Zipfile entry. */ protected ZipEntry m_entry; /** * Serial number (zipfile open count). * This is used to determine if the zipfile has been closed or reopened since * this entry was created, and thus whether this entry is still valid or not. */ protected int m_serialNo; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public methods /*************************************************************************** * Determine if this zipfile entry exists. * * @return * True always. * * @since 1.3, 2001-06-15 */ public boolean exists() //implements tribble.search.DocumentI { // Always true return (true); } /*************************************************************************** * Determine if this zipfile entry specifies a directory entry. * * @return * True if this entry names a directory, otherwise false. * * @since 1.3, 2001-06-15 */ public boolean isDirectory() //implements tribble.search.DocumentI { // Determine if this entry is a directory return (m_entry.isDirectory()); } /*************************************************************************** * Determine if this zipfile entry is readable. * * @return * True always. * * @since 1.3, 2001-06-15 */ public boolean canRead() //implements tribble.search.DocumentI { // Always true return (true); } /*************************************************************************** * Determine if this zipfile entry is writable. * * @return * False always (since zipfile entries cannot be written to). * * @since 1.3, 2001-06-15 */ public boolean canWrite() //implements tribble.search.DocumentI { // Always false return (false); } /*************************************************************************** * Determine the length of this zipfile entry. * * @return * The size of this file, in bytes. * * @since 1.3, 2001-06-15 */ public long length() //implements tribble.search.DocumentI { // Retrieve the zipfile entry size return (m_entry.getSize()); } /*************************************************************************** * Determine the date that this zipfile entry was last modified. * * @return * The date that this file was last modified, or null if the date is unknown. * * @since 1.3, 2001-06-15 */ public Date lastModified() //implements tribble.search.DocumentI { long when; // Retrieve the zipfile entry date when = m_entry.getTime(); if (when > 0) return (new Date(when)); else return (null); } /*************************************************************************** * Retrieve the name of this zipfile entry. * * @return * The name of this file. * * @since 1.3, 2001-06-15 */ public String getName() //implements tribble.search.DocumentI { // Retrieve the zipfile entry name return (m_entry.getName()); } /*************************************************************************** * Retrieve the type of the data file corresponding to this zipfile entry. * * @return * The type of the file (which is the filename extension or suffix, which can * be empty ""). Such a type name is typically suitable for display * by some user interface. * * @since 1.3, 2001-06-15 */ public String getType() //implements tribble.search.DocumentI { String ext; int off; // Retrieve the filename type (extension/suffix) of this zipfile entry ext = m_entry.getName(); off = ext.lastIndexOf('.'); if (off >= 0) ext = ext.substring(off+1); else ext = ""; return (ext); } /*************************************************************************** * Retrieve the attribute names for this zipfile entry. * * @return * An array containing the names of the attributes associated with this * zipfile entry, or null if it has none. * * @throws Exception * Thrown if the information about the entry is unobtainable, or if some * other error occurs. * * @see #getAttribute * * @since 1.5, 2001-06-30 */ public String[] getAttributeNames() //implements tribble.search.DocumentI { return (s_attrNames); } /*************************************************************************** * Retrieve the value of an attribute for this zipfile entry. * * @param key * The name of an attribute associated with this zipfile entry. * The following attribute names are supported: *

    *    "comment"           String
    *    "compressed.size"   Long
    *    "crc"               Long
    *    "extra"             byte[]
    *    "method"            String
    *    "name"              String
    *    "size"              Long
    *    "time"              Date 
* * @return * The value of the specified attribute, or null if this entry has no * such attribute. Note that the value is returned as a generic * Object, meaning that it must be cast into the appropriate type. * * @throws Exception * Thrown if the information about the entry is unobtainable, or if some * other error occurs. * * @see #getAttributeNames * * @since 1.5, 2001-06-30 */ public Object getAttribute(String key) //implements tribble.search.DocumentI { // Retrieve the value of the named attribute if (key == "method") { int meth; meth = m_entry.getMethod(); switch (meth) { case ZipEntry.STORED: return ("stored"); case ZipEntry.DEFLATED: return ("deflated"); default: return (meth + ""); } } else if (key == "comment") return (m_entry.getComment()); // String else if (key == "compressed.size") return (new Long(m_entry.getCompressedSize())); // Long else if (key == "crc") return (new Long(m_entry.getCrc())); // Long else if (key == "extra") return (m_entry.getExtra()); // byte[] else if (key == "name") return (m_entry.getName()); // String else if (key == "size") return (new Long(m_entry.getSize())); // Long else if (key == "time") return (new Date(m_entry.getTime())); // Date else if (key.equalsIgnoreCase("comment")) return (getAttribute("comment")); else if (key.equalsIgnoreCase("comment")) return (getAttribute("comment")); else if (key.equalsIgnoreCase("compressed.size")) return (getAttribute("compressed.size")); else if (key.equalsIgnoreCase("crc")) return (getAttribute("crc")); else if (key.equalsIgnoreCase("extra")) return (getAttribute("extra")); else if (key.equalsIgnoreCase("method")) return (getAttribute("method")); else if (key.equalsIgnoreCase("name")) return (getAttribute("name")); else if (key.equalsIgnoreCase("size")) return (getAttribute("size")); else if (key.equalsIgnoreCase("time")) return (getAttribute("time")); else throw new IllegalArgumentException("Unknown attribute: '" + key + "'"); } /*************************************************************************** * Set the value of an attribute for this zipfile entry. * This method is not supported. * * @throws UnsupportedOperationException (unchecked) * Always thrown, because this method is not implemented for this document * type. * * @since 1.5, 2001-06-30 */ public void setAttribute(String attr, Object val) throws Exception, UnsupportedOperationException //implements tribble.search.DocumentI { // Not supported throw new UnsupportedOperationException("setAttribute"); } /*************************************************************************** * Get a readable input stream for this zipfile entry. * * @return * An input stream, from which the data contents of this zipfile entry can be * read. * * @throws IOException * Thrown if the input stream cannot be obtained, or if some other error * occurs. * * @throws ZipException * Thrown if the input stream cannot be obtained from the zipfile. * * * * @since 1.1, 2001-05-18 */ public synchronized InputStream getInputStream() throws IOException, ZipException //implements tribble.search.DocumentI { // Retrieve an input stream for reading this entry synchronized (m_searcher) { InputStream in; ZipFile zfile; // Insure that the zipfile is still open zfile = m_searcher.m_zipfile; if (zfile == null || m_searcher.m_serialNo != this.m_serialNo) throw new IOException("Zipfile is no longer open"); ///throw new ConcurrentModificationException(...); // Retrieve an input stream for reading this entry in = zfile.getInputStream(m_entry); return (in); } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected constructors /*************************************************************************** * Constructor. * * @param searcher * The zipfile searcher that found and created this entry. * * @param ent * The zipfile entry for this file entry. * * @param serialNo * The open count at the time the zipfile containing this entry was opened. * * @since 1.3, 2001-06-15 */ protected ZFile(ZArchive searcher, ZipEntry ent, int serialNo) { // Establish this zipfile file entry m_searcher = searcher; m_entry = ent; m_serialNo = serialNo; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Private constructors /*************************************************************************** * Default constructor. * * @since 1.1, 2001-05-18 */ private ZFile() { // Do nothing } } // End ZFile.java