JDK-6962459 : Can' t delete zip files accessed with jar protocol
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6u10
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2010-06-18
  • Updated: 2012-03-20
  • Resolved: 2011-06-07
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
I can't delete a zip file where I read an entry with the jar protocol.

This bug is probably the same as bug #5041014 but adding a close() method to URLClassLoader described in that bug won't solve this bug.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the following test case:

import java.io.*;
import java.net.URL;
import java.util.UUID;
import java.util.zip.*;

public class ZipFileDeleteTest {
   public static void main(String[] args) throws IOException {
       // Create a zip file
       File testFile = new File(UUID.randomUUID().toString() + ".zip");
       // Write an entry in the zip file
       ZipOutputStream out = new ZipOutputStream(new FileOutputStream(testFile));
       out.putNextEntry(new ZipEntry("test"));
       out.write('a');
       out.closeEntry();
       out.close();
       // Access to the entry with jar: protocol
       URL url = new URL("jar:" + testFile.toURI().toURL() + "!/test");
       InputStream in = url.openStream();
       in.read();
       in.close();
       // Try to delete the zip file
       System.out.println(testFile.delete());
   }
}


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expected the program would display "true"
ACTUAL -
The program displayed "false" meaning the file wasn't deleted.
Under Mac OS X and Linux, it displays "true" as expected.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.*;
import java.net.URL;
import java.util.UUID;
import java.util.zip.*;

public class ZipFileDeleteTest {
   public static void main(String[] args) throws IOException {
       // Create a zip file
       File testFile = new File(UUID.randomUUID().toString() + ".zip");
       // Write an entry in the zip file
       ZipOutputStream out = new ZipOutputStream(new FileOutputStream(testFile));
       out.putNextEntry(new ZipEntry("test"));
       out.write('a');
       out.closeEntry();
       out.close();
       // Access to the entry with jar: protocol
       URL url = new URL("jar:" + testFile.toURI().toURL() + "!/test");
       InputStream in = url.openStream();
       in.read();
       in.close();
       // Try to delete the zip file
       System.out.println(testFile.delete());
   }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Probably, not using jar protocol, or using a URLStreamHandler that changes the existing behavior of  sun.net.www.protocol.jar.Handler class.

Comments
WORK AROUND Disable caching of jar files. Instead of using openStream() to retrieve the InputStream, use openConnection() to retrieve the URLConnection, then disable caching (for this connection only). Use getInputStream to open an input stream to the zip file. < InputStream in = url.openStream(); <<<<<Removed ---- > URLConnection uc = url.openConnection(); > uc.setUseCaches(false); > InputStream in = uc.getInputStream();
07-06-2011

EVALUATION Caching of jar files by default means that even though the stream is closed a handle to the jar file will remain open, preventing it from being deleted on Windows. If the requirement is to delete the file then caching should be disabled using URLConnenction.setUseCaches(false). Closing as a duplicate of 5105410.
07-06-2011