A suggestion: if the library has logging capability, then log a warning saying that the archive was closed in the finalize method. That will serve as a clue to the library user that they forgot to close the archive.

-Adrian

On 4/17/2011 9:28 PM, bode...@apache.org wrote:
Author: bodewig
Date: Mon Apr 18 04:28:14 2011
New Revision: 1094224

URL: http://svn.apache.org/viewvc?rev=1094224&view=rev
Log:
add a finalize method to ZipFile as suggested on the user list

Modified:
     commons/proper/compress/trunk/src/changes/changes.xml
     
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1094224&r1=1094223&r2=1094224&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Mon Apr 18 04:28:14 
2011
@@ -45,6 +45,10 @@ The<action>  type attribute can be add,u
    </properties>
    <body>
      <release version="1.2" date="as in SVN" description="Release 1.2">
+<action type="update" date="2011-04-18">
+        ZipFile now implements finalize which closes the underlying
+        file.
+</action>
        <action issue="COMPRESS-117" type="update" date="2011-03-23">
          Certain tar files not recognised by ArchiveStreamFactory.
        </action>

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=1094224&r1=1094223&r2=1094224&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
 Mon Apr 18 04:28:14 2011
@@ -112,6 +112,11 @@ public class ZipFile {
      private final boolean useUnicodeExtraFields;

      /**
+     * Whether the file is closed.
+     */
+    private boolean closed;
+
+    /**
       * Opens the given file for reading, assuming "UTF8" for file names.
       *
       * @param f the archive.
@@ -208,8 +213,11 @@ public class ZipFile {
       * Closes the archive.
       * @throws IOException if an error occurs closing the archive.
       */
-    public void close() throws IOException {
-        archive.close();
+    public synchronized void close() throws IOException {
+        if (!closed) {
+            closed = true;
+            archive.close();
+        }
      }

      /**
@@ -307,6 +315,19 @@ public class ZipFile {
          }
      }

+    /**
+     * Ensures that the close method of this zipfile is called when
+     * there are no more references to it.
+     * @see close()
+     */
+    protected void finalize() throws Throwable {
+        try {
+            close();
+        } finally {
+            super.finalize();
+        }
+    }
+
      private static final int CFH_LEN =
          /* version made by                 */ SHORT
          /* version needed to extract       */ + SHORT



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to