This is the mail archive of the java-patches@sources.redhat.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

ZipFile/JarFile OPEN_DELETE mode


Hi,

Since 1.3 ZipFile and JarFile have a new constructor that can take a
OPEN_DELETE mode. When constructed with OPEN_DELETE the ZipFile is
deleted when closed or garbege collected.

I made the following patch. OK to commit?

2000-08-27  Mark Wielaard  <mark@klomp.org>

    * java/util/zip/ZipFile.java: Implement OPEN_DELETE mode, new constructor,
    close can delete the file, finalize calls close.
    * java/util/jar/JarFile.java: Constructor that takes mode now calls super.


Cheers,

Mark
Index: java/util/jar/JarFile.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/jar/JarFile.java,v
retrieving revision 1.6
diff -u -r1.6 JarFile.java
--- JarFile.java	2000/08/20 19:40:25	1.6
+++ JarFile.java	2000/08/27 20:34:18
@@ -129,15 +129,17 @@
     }
 
     /**
-     * XXX - not yet implemented in java.util.zip.ZipFile
+     * Creates a new JarFile with the indicated mode, tries to read the
+     * manifest and if the manifest exists and verify is true verfies it.
      *
      * @param file the file to open to open as a jar file
      * @param verify checks manifest and entries when true and a manifest
      * exists, when false no checks are made
-     * @param mode XXX - see ZipFile
-     * @exception FileNotFoundException XXX
-     * @exception IOException XXX
-     * @exception IllegalArgumentException XXX
+     * @param mode either ZipFile.OPEN_READ or
+     *             (ZipFile.OPEN_READ | ZipFile.OPEN_DELETE)
+     * @exception FileNotFoundException if the file does not exist
+     * @exception IOException if another IO exception occurs while reading
+     * @exception IllegalArgumentException when given an illegal mode
      * 
      * @since 1.3
      */
@@ -145,12 +147,10 @@
                                                     FileNotFoundException,
                                                     IOException,
                                                     IllegalArgumentException {
-        // XXX - For now don't use super(file, mode)
-        this(file, verify);
-        /* super(file, mode);
-           manifest = readManifest();
-           if (verify)
-               verify(); */
+        super(file, mode);
+        manifest = readManifest();
+        if (verify)
+            verify();
     }
 
     // Methods
Index: java/util/zip/ZipFile.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/zip/ZipFile.java,v
retrieving revision 1.11
diff -u -r1.11 ZipFile.java
--- ZipFile.java	2000/08/20 21:51:19	1.11
+++ ZipFile.java	2000/08/27 20:34:19
@@ -18,15 +18,32 @@
 
 public class ZipFile implements ZipConstants
 {
+  public static final int OPEN_READ = 1;
+  public static final int OPEN_DELETE = 4;
+
   public ZipFile (String fname) throws IOException
   {
-    file = new RandomAccessFile(fname, "r");
-    name = fname;
-    readDirectory ();
+    this(new File(fname));
   }
 
   public ZipFile (File f) throws IOException
   {
+    this(f, OPEN_READ);
+  }
+
+  public ZipFile (File f, int mode) throws IOException
+  {
+    if (mode != OPEN_READ && mode != (OPEN_READ | OPEN_DELETE))
+        throw new IllegalArgumentException
+            ("mode can only be OPEN_READ or OPEN_READ | OPEN_DELETE");
+
+    if ((mode & OPEN_DELETE) != 0) {
+        delete_on_close = f;
+        // f.deleteOnExit(); XXX - Not yet implemented in libgcj
+    } else {
+        delete_on_close = null;
+    }
+
     file = new RandomAccessFile(f, "r");
     name = f.getName();
     readDirectory ();
@@ -107,6 +124,8 @@
     file.close();
     entries = null;
     numEntries = 0;
+    if (delete_on_close != null)
+        delete_on_close.delete();
   }
 
   public ZipEntry getEntry(String name)
@@ -148,6 +167,10 @@
       return numEntries;
   }
 
+  protected void finalize () throws IOException {
+    close();
+  }
+
   private int readu2 () throws IOException
   {
     int byte0 = file.read();
@@ -173,6 +196,9 @@
   int numEntries;
   RandomAccessFile file;
   String name;
+  /** File to delete on close or null. */
+  File delete_on_close;
+    
 }
 
 final class ZipEnumeration implements java.util.Enumeration

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]