This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new cd97b39c6 TarFile now implements IOIterable<TarArchiveEntry>
cd97b39c6 is described below

commit cd97b39c6bb80be49c1aebf98f2433a4415bfad3
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Sun Sep 21 19:51:34 2025 -0400

    TarFile now implements IOIterable<TarArchiveEntry>
---
 src/changes/changes.xml                            |  1 +
 .../commons/compress/archivers/tar/TarFile.java    | 50 +++++++++++++++++++---
 2 files changed, 46 insertions(+), 5 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4f5f9cd2a..221a07693 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -121,6 +121,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
SnappyCompressorInputStream.getUncompressedSize() and deprecate 
getSize().</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
ArchiveInputStream.ArchiveInputStream(InputStream, Charset) as a public 
constructor, it was private.</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory, Piotr P. 
Karwasz">Introduce builders for all ArchiveInputStream implementations and 
deprecate some constructors.</action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">TarFile now 
implements IOIterable&lt;TarArchiveEntry&gt;.</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
org.apache.commons:commons-parent from 85 to 88 #707.</action>
       <action type="update" dev="ppkarwasz" due-to="Raeps">Extract duplicate 
code in org.apache.commons.compress.harmony.pack200.IntList.</action>
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java 
b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
index b92a6459f..9c1d13a62 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
@@ -28,6 +28,7 @@
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -39,6 +40,8 @@
 import org.apache.commons.compress.utils.BoundedArchiveInputStream;
 import org.apache.commons.compress.utils.BoundedSeekableByteChannelInputStream;
 import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
+import org.apache.commons.io.function.IOIterable;
+import org.apache.commons.io.function.IOIterator;
 import org.apache.commons.io.input.BoundedInputStream;
 
 /**
@@ -46,7 +49,7 @@
  *
  * @since 1.21
  */
-public class TarFile implements Closeable {
+public class TarFile implements Closeable, IOIterable<TarArchiveEntry> {
 
     private final class BoundedTarEntryInputStream extends 
BoundedArchiveInputStream {
 
@@ -299,10 +302,7 @@ public TarFile(final SeekableByteChannel archive, final 
int blockSize, final int
         this.recordBuffer = ByteBuffer.allocate(this.recordSize);
         this.blockSize = blockSize;
         this.lenient = lenient;
-        TarArchiveEntry entry;
-        while ((entry = getNextTarEntry()) != null) {
-            entries.add(entry);
-        }
+        forEach(entries::add);
     }
 
     /**
@@ -490,6 +490,38 @@ private boolean isEOFRecord(final ByteBuffer headerBuf) {
         return headerBuf == null || 
ArchiveUtils.isArrayZero(headerBuf.array(), recordSize);
     }
 
+    @Override
+    public IOIterator<TarArchiveEntry> iterator() {
+        return new IOIterator<TarArchiveEntry>() {
+
+            private TarArchiveEntry next;
+
+            @Override
+            public boolean hasNext() throws IOException {
+                if (next == null) {
+                    next = getNextTarEntry();
+                }
+                return next != null;
+            }
+
+            @Override
+            public TarArchiveEntry next() throws IOException {
+                if (next == null) {
+                    next = getNextTarEntry();
+                }
+                final TarArchiveEntry tmp = next;
+                next = null;
+                return tmp;
+            }
+
+            @Override
+            public Iterator<TarArchiveEntry> unwrap() {
+                return null;
+            }
+
+        };
+    }
+
     /**
      * Adds the sparse chunks from the current entry to the sparse chunks, 
including any additional sparse entries following the current entry.
      *
@@ -595,4 +627,12 @@ private void tryToConsumeSecondEOFRecord() throws 
IOException {
             }
         }
     }
+
+    @Override
+    public Iterable<TarArchiveEntry> unwrap() {
+        // Commons IO 2.21.0:
+        // return asIterable();
+        return null;
+    }
+
 }

Reply via email to