//============================================================================== // tribble/search/zip/ZArchive.java //============================================================================== package tribble.search.zip; // System imports import java.io.File; import java.io.IOException; import java.lang.Exception; import java.lang.String; import java.lang.UnsupportedOperationException; import java.util.Hashtable; import java.util.Enumeration; import java.util.Vector; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; // Local imports import tribble.search.DocumentI; import tribble.search.DocumentFilterI; import tribble.search.DocumentSearcherI; /******************************************************************************* * Zipfile searcher. * * @version $Revision: 1.5 $ $Date: 2001/06/18 02:56:40 $ * @since 2001-05-18 * @author * David R. Tribble * (david@tribble.com). *
* Copyright * ©2001 by David R. Tribble, all rights reserved.
* * @see ZFile */ public class ZArchive implements DocumentSearcherI { // Identification /** Revision information. */ static final String REV = "@(#)tribble/search/zip/ZArchive.java $Revision: 1.5 $ $Date: 2001/06/18 02:56:40 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constants /** Series number. */ public static final int SERIES = 200; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected variables /** Zipfile filename. */ protected File m_fname; /** Zipfile file. */ protected ZipFile m_zipfile; /** * Zipfile open counter. * This counter is incremented every time this zipfile is opened. It is used * by {@link ZFile} objects to detect whether or not they have become * invalidated. */ protected int m_serialNo; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constructors /*************************************************************************** * Default constructor. * * @since 1.1, 2001-05-18 */ public ZArchive() { // Do nothing } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public methods /*************************************************************************** * Initialize this searcher. * *

* Note: This method does not actually do anything. * * @param parms * A table containing name/value pairs with which to initialize this zipfile * searcher. This can be null. * * @since 1.1, 2001-05-18 */ public void initialize(Hashtable parms) //implements tribble.search.DocumentSearcherI { // Do nothing } /*************************************************************************** * Open a given pathname for this zipfile searcher. * * @param path * A pathname specifying the location of a zipfile to open and search. * This name must specify a filename on the local machine. * * @throws IOException * Thrown if the specified path does not exist or is not accessible, or if it * is not a regular file, or if it is malformed, or if some other error * occurs. * * @since 1.1, 2001-05-18 */ public synchronized void open(String path) throws IOException, ZipException //implements tribble.search.DocumentSearcherI { File fname; // Insure that this file is not already open if (m_zipfile != null) close(); // Find and open the pathname fname = new File(path); // Check that the filename is a file if (!fname.isFile()) throw new IOException("Not a file: " + fname.toString()); // Save the zipfile name info m_fname = fname; // Open the zipfile m_zipfile = new ZipFile(m_fname); m_serialNo++; } /*************************************************************************** * Close this zipfile searcher. * * @throws IOException * Thrown if an error occurs. * * @since 1.1, 2001-05-18 */ public synchronized void close() throws IOException //implements tribble.search.DocumentSearcherI { // Close the zipfile if (m_zipfile != null) m_zipfile.close(); // Clear the zipfile info m_zipfile = null; m_fname = null; } /*************************************************************************** * Retrieve file entries from this zipfile matching given search criteria. * * @param filt * A filter specifying file searching criteria. This can be null, in which * case all possible file entries for this searcher are returned. * * @return * A {@link ZIterator} object containing an enumeration of {@link ZFile} * objects representing the list of file entries found by this searcher, or * null if there are no matching file entries to be found. * * @throws IOException * Thrown if the specified criteria are malformed, or if some other error * occurs. * * @since 1.4, 2001-06-15 */ public synchronized Enumeration find(DocumentFilterI filt) throws IOException, Exception //implements tribble.search.DocumentSearcherI { Enumeration iter; Vector list; ZIterator ziter; // Get a list of all the entries in the zipfile iter = m_zipfile.entries(); // Create a filtered entry list from the zipfile entries list = new Vector(); while (iter != null && iter.hasMoreElements()) { ZipEntry ent; ZFile zent; // Build a file entry for the entry from the zipfile ent = (ZipEntry) iter.nextElement(); zent = new ZFile(this, ent, m_serialNo); // Filter the entry if (filt == null || filt.accept(zent)) list.add(zent); } // Create an entry iterator from the filtered entry vector ziter = new ZIterator(this, list); return (ziter); } /*************************************************************************** * Create a new file entry in this zipfile searcher. * *

* Note *

* This method is not supported. * * @param name * The name of the new file entry to create in this zipfile. * * @param append * Specifies whether or not to append written data to the end of the file. * * @return * A new file entry corresponding to the newly created file entry. * * @throws UnsupportedOperationException * Thrown, always. * * @since 1.4, 2001-06-15 */ public DocumentI create(String name, boolean append) throws UnsupportedOperationException { // Unsupported throw new UnsupportedOperationException(); } /*************************************************************************** * Determine if new zipfile entries can be created by this searcher. * * @return * False, always. * * @since 1.1, 2001-06-02 */ public boolean createSupported() { // Not supported return (false); } /*************************************************************************** * Determine if zipfile entries can be written to this searcher. * * @return * False, always. * * @since 1.1, 2001-06-02 */ public boolean outputSupported() { // Not supported return (false); } } // End ZArchive.java