JDK-6581715 : Have java.util.zip.ZipFile implement Iterable
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util.jar
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-07-17
  • Updated: 2017-05-19
  • Resolved: 2010-12-07
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
Since the request for a unification of enhanced for loops was turned down (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6349852), I suggest the following:

Make java.util.zip.ZipFile implement java.lang.Iterable<ZipEntry>.



JUSTIFICATION :
Make it easier and more consistent with other collection-type objects to iterate over the zip entries.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
ZipFile zipFile = new ZipFile("test.zip");
for(ZipEntry entry : zipFile) {
  System.out.println(entry.getName());
}

ACTUAL -
ZipFile zipFile = new ZipFile("test.zip");
for(Enumeration<? extends ZipEntry> entries = zipFile.entries(); entries.hasMoreElements();) {
  ZipEntry entry = entries.nextElement();
  System.out.println(entry.getName());
}

Comments
EVALUATION Further research/discussion indicates (1)the proposed change might break existing application (for example a sublcass of ZipFile that implements Iterable<E> already) and (2) it appears the current type implementation makes it impossible for the subclass JarFile to "implement" something like Iterable<JarEntry>...We decided it is not worth the work. Closed as "will not fix"
07-12-2010

SUGGESTED FIX Have iterator() return an Iterator based on the result of entries(); have remove() be unsupported.
17-07-2007

EVALUATION Seems reasonable, straighforward, and low-risk.
17-07-2007