Index: java/util/zip/ZipFile.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/util/zip/ZipFile.java,v retrieving revision 1.20 diff -u -r1.20 ZipFile.java --- java/util/zip/ZipFile.java 12 Dec 2004 19:10:29 -0000 1.20 +++ java/util/zip/ZipFile.java 27 Jan 2005 15:50:14 -0000 @@ -1,5 +1,6 @@ /* ZipFile.java -- - Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc. + Copyright (C) 2001, 2002, 2003, 2004, 2005 + Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -38,6 +39,8 @@ package java.util.zip; +import gnu.java.util.EmptyEnumeration; + import java.io.BufferedInputStream; import java.io.DataInput; import java.io.EOFException; @@ -62,6 +65,7 @@ */ public class ZipFile implements ZipConstants { + private static final int ZIP_MAGIC = 0x4b50; /** * Mode flag to open a zip file for reading. @@ -94,6 +98,7 @@ { this.raf = new RandomAccessFile(name, "r"); this.name = name; + checkZipFile(); } /** @@ -106,6 +111,7 @@ { this.raf = new RandomAccessFile(file, "r"); this.name = file.getPath(); + checkZipFile(); } /** @@ -135,6 +141,30 @@ } this.raf = new RandomAccessFile(file, "r"); this.name = file.getPath(); + checkZipFile(); + } + + private void checkZipFile() throws IOException, ZipException + { + byte[] magic = new byte[2]; + long position = raf.getFilePointer(); + raf.read(magic); + raf.seek(position); + + if (((((int) magic[1]) << 8) + ((int) magic[0])) != ZIP_MAGIC) + { + raf.close(); + throw new ZipException("Not a valid zip file"); + } + } + + /** + * Checks if file is closed and throws an exception. + */ + private void checkClosed() + { + if (closed) + throw new IllegalStateException("ZipFile has closed: " + name); } /** @@ -312,16 +342,20 @@ /** * Returns an enumeration of all Zip entries in this Zip file. + * + * @exception IllegalStateException when the ZipFile has already been closed */ public Enumeration entries() { + checkClosed(); + try { return new ZipEntryEnumeration(getEntries().values().iterator()); } catch (IOException ioe) { - return null; + return EmptyEnumeration.getInstance(); } } @@ -335,8 +369,7 @@ { synchronized(raf) { - if (closed) - throw new IllegalStateException("ZipFile has closed: " + name); + checkClosed(); if (entries == null) readEntries(); @@ -351,9 +384,13 @@ * @param the name. May contain directory components separated by * slashes ('/'). * @return the zip entry, or null if no entry with that name exists. + * + * @exception IllegalStateException when the ZipFile has already been closed */ public ZipEntry getEntry(String name) { + checkClosed(); + try { HashMap entries = getEntries(); @@ -423,11 +460,14 @@ * @param entry the entry to create an InputStream for. * @return the input stream, or null if the requested entry does not exist. * + * @exception IllegalStateException when the ZipFile has already been closed * @exception IOException if a i/o error occured. * @exception ZipException if the Zip archive is malformed. */ public InputStream getInputStream(ZipEntry entry) throws IOException { + checkClosed(); + HashMap entries = getEntries(); String name = entry.getName(); ZipEntry zipEntry = (ZipEntry) entries.get(name); @@ -459,9 +499,13 @@ /** * Returns the number of entries in this zip file. + * + * @exception IllegalStateException when the ZipFile has already been closed */ public int size() { + checkClosed(); + try { return getEntries().size();