package pregozip;

import java.util.Enumeration;
import java.util.zip.*;

/*******************************************************************************
 * <p>Class ZipReader</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * @author Markus Wilthaner
 * @version 1.0
 * <p>Extends ZipFile by fileExists, a function that checks if the file is in
 * the archive.</p>
 *******************************************************************************
 */

public class ZipReader extends ZipFile {

 /*****************************************************************************
  * <p>Constructor</p>
  * <p>Tries to open the archive filename - throws an Exception if file opening
  * doesn't work.
  *****************************************************************************/
  public ZipReader(String filename) throws Exception {
    super(filename);
  }

 /*****************************************************************************
  * <p>fileExists</p>
  * <p>Searches the archive for the file relativeFilename. If the file doesn't
  * exist, false is returned.</p>
  * <p>If the file does exist, the lastModified Date of the file is compared
  * to lastModified. If the file in the archive is newer, true is returned.</p>
  * For update purposes: If the function returns false, the file needs to be
  * updated.</p>
  *****************************************************************************/
  public boolean fileExists(String relativeFilename, long lastModified) throws Exception {

    Enumeration ZipEntries;
    ZipEntry zEntry;

    ZipEntries = this.entries();
    while(ZipEntries.hasMoreElements() ) {
      zEntry = (ZipEntry)(ZipEntries.nextElement());

      if(zEntry.getName().equals(relativeFilename)) {
        if(zEntry.getTime() > lastModified) {
          return true;
        }
      }
    }
    return false;
  }
}
