Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 06f0ccd

Browse files
committedMar 9, 2025·
ZipInputStream implements Iterable
1 parent 4e1367e commit 06f0ccd

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed
 

‎src/java.base/share/classes/java/util/zip/ZipInputStream.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,8 @@
3131
import java.io.OutputStream;
3232
import java.io.PushbackInputStream;
3333
import java.nio.charset.Charset;
34+
import java.util.Iterator;
35+
import java.util.NoSuchElementException;
3436
import java.util.Objects;
3537

3638
import sun.nio.cs.UTF_8;
@@ -75,7 +77,7 @@
7577
* @author David Connelly
7678
* @since 1.1
7779
*/
78-
public class ZipInputStream extends InflaterInputStream implements ZipConstants {
80+
public class ZipInputStream extends InflaterInputStream implements ZipConstants, Iterable {
7981
private ZipEntry entry;
8082
private int flag;
8183
private CRC32 crc = new CRC32();
@@ -488,6 +490,25 @@ public void close() throws IOException {
488490
}
489491
}
490492

493+
public Iterator<ZipEntry> iterator() {
494+
return new Iterator<ZipEntry>() {
495+
private ZipEntry nextEntry;
496+
497+
public boolean hasNext() {
498+
if (nextEntry == null)
499+
nextEntry = getNextEntry();
500+
return nextEntry != null;
501+
}
502+
503+
public ZipEntry next() {
504+
if (!hasNext())
505+
throw new NoSuchElementException();
506+
507+
return currentEntry;
508+
}
509+
}
510+
}
511+
491512
private byte[] b = new byte[256];
492513

493514
/*

0 commit comments

Comments
 (0)
Please sign in to comment.