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

garydgregory 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 0c0d3d508 Document duplicate entry lookup and extraction behavior 
(f013) (#870)
0c0d3d508 is described below

commit 0c0d3d508bf89c5c42497250433940fbd308391a
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Dec 4 08:52:52 2026 -0500

    Document duplicate entry lookup and extraction behavior (f013) (#870)
    
    Clarify that ZipFile returns the first matching entry while extraction
    may overwrite earlier output. Explain output-path collisions and
    validation considerations without changing extraction behavior.
    
    Add regression tests for ZIP duplicates, normalized aliases, and TAR
    updates through seekable and streaming readers.
---
 src/changes/changes.xml                            |   1 +
 .../compress/archivers/examples/Expander.java      |  12 +++
 .../commons/compress/archivers/zip/ZipFile.java    |  13 +++
 .../compress/archivers/examples/ExpanderTest.java  | 107 +++++++++++++++++++++
 4 files changed, 133 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a99cdd0c0..dc66eefa1 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -277,6 +277,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump Bump 
io.airlift:aircompressor from 2.0.2 to 2.0.3 #760.</action>
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
commons-io:commons-io from 2.21.0 to 2.22.0.</action>
       <action type="update" dev="ggregory" due-to="Dependabot">Bump 
org.slf4j:slf4j-* from 2.0.16 to 2.0.19, use the BOM POM 
org.slf4j:slf4j-bom.</action>
+      <action type="update" dev="ggregory" due-to="Claude Security, Gary 
Gregory">Document duplicate entry lookup and output-path collisions in ZipFile 
and Expander without changing extraction behavior.</action>
       <!-- REMOVE -->
       <action type="remove" dev="pkarwasz" due-to="Piotr P. Karwasz">Deprecate 
IOUtils.readFully and IOUtils.skip.</action>
       <action type="remove" dev="ggregory" due-to="Piotr P. Karwasz">Deprecate 
FlushShieldFilterOutputStream.</action>
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java 
b/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java
index 93ff61d5b..7fc0b16ca 100644
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java
+++ b/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java
@@ -47,6 +47,18 @@
 /**
  * Provides a high level API for expanding archives.
  *
+ * <p>
+ * Entries are processed in the order supplied by the archive reader. When 
extracting to a directory, later file entries may overwrite files written by 
earlier
+ * entries that resolve to the same output file. Different entry names may 
also refer to the same output file after path normalization or because of file 
system
+ * naming rules, such as case insensitivity.
+ * </p>
+ *
+ * <p>
+ * Validating an entry obtained by name, for example with {@link 
ZipFile#getEntry(String)}, does not validate the complete extraction result. 
Applications that
+ * validate entry contents should validate and extract the same entry 
instances using the same archive reader, and check for colliding output paths 
across all
+ * extracted entries. Different archive readers may interpret entry names or 
entry order differently.
+ * </p>
+ *
  * @since 1.17
  */
 public class Expander {
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java 
b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
index dc549b236..7f1e51940 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
@@ -1240,10 +1240,23 @@ public Iterable<ZipArchiveEntry> 
getEntriesInPhysicalOrder(final String name) {
 
     /**
      * Gets a named entry or {@code null} if no entry by that name exists.
+     *
      * <p>
      * If multiple entries with the same name exist the first entry in the 
archive's central directory by that name is returned.
      * </p>
      *
+     * <p>
+     * ZIP archives may contain multiple entries with the same name. 
Extracting all entries with an overwrite policy may leave a later entry's 
contents in the
+     * output file, so an application that validates the entry returned by 
this method may extract different bytes. Use {@link #getEntries(String)} to 
detect
+     * duplicate entry names. Different names may also resolve to the same 
output file, so checking duplicate names alone does not detect all output-path
+     * collisions.
+     * </p>
+     *
+     * <p>
+     * Applications that validate entry contents should extract the validated 
entry instance using this {@link ZipFile} and prevent other entries from writing
+     * to the same output file.
+     * </p>
+     *
      * @param name name of the entry.
      * @return The ZipArchiveEntry corresponding to the given name - or {@code 
null} if not present.
      */
diff --git 
a/src/test/java/org/apache/commons/compress/archivers/examples/ExpanderTest.java
 
b/src/test/java/org/apache/commons/compress/archivers/examples/ExpanderTest.java
index 9c6eddbb2..8344c9860 100644
--- 
a/src/test/java/org/apache/commons/compress/archivers/examples/ExpanderTest.java
+++ 
b/src/test/java/org/apache/commons/compress/archivers/examples/ExpanderTest.java
@@ -21,6 +21,7 @@
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assumptions.assumeFalse;
@@ -34,6 +35,7 @@
 import java.nio.channels.FileChannel;
 import java.nio.channels.SeekableByteChannel;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
 
 import org.apache.commons.compress.AbstractTest;
@@ -44,9 +46,15 @@
 import org.apache.commons.compress.archivers.StreamingNotSupportedException;
 import org.apache.commons.compress.archivers.sevenz.SevenZFile;
 import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
 import org.apache.commons.compress.archivers.tar.TarFile;
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.apache.commons.compress.archivers.zip.ZipFile;
+import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 class ExpanderTest extends AbstractTest {
 
@@ -59,6 +67,39 @@ private void assertHelloWorld(final String fileName, final 
String suffix) throws
         assertArrayEquals(expected, actual);
     }
 
+    /**
+     * Checks the contents selected by an exact ZIP entry name.
+     *
+     * @param zip      the archive to read.
+     * @param name     the entry name.
+     * @param expected the expected contents.
+     * @throws IOException if the entry cannot be read.
+     */
+    private void assertZipEntryContents(final ZipFile zip, final String name, 
final String expected) throws IOException {
+        final ZipArchiveEntry entry = zip.getEntry(name);
+        assertNotNull(entry, name);
+        try (InputStream in = zip.getInputStream(entry)) {
+            assertArrayEquals(expected.getBytes(UTF_8), 
IOUtils.toByteArray(in));
+        }
+    }
+
+    /**
+     * Expands the fixture through the requested ZIP reader.
+     *
+     * @param zip       the seekable archive reader.
+     * @param streaming whether to use a separate streaming reader.
+     * @throws IOException if extraction fails.
+     */
+    private void expandZip(final ZipFile zip, final boolean streaming) throws 
IOException {
+        if (streaming) {
+            try (InputStream in = Files.newInputStream(archive.toPath()); 
ZipArchiveInputStream stream = new ZipArchiveInputStream(in)) {
+                new Expander().expand(stream, tempResultDir);
+            }
+        } else {
+            new Expander().expand(zip, tempResultDir);
+        }
+    }
+
     private void setup7z() throws IOException {
         archive = newTempFile("test.7z");
         final File dummy = newTempFile("x");
@@ -82,6 +123,30 @@ private void setup7z() throws IOException {
         }
     }
 
+    /**
+     * Creates two entries that share an output path, followed by an unrelated 
entry.
+     *
+     * @param format    the archive format.
+     * @param firstName the first entry's name.
+     * @throws IOException if the archive cannot be written.
+     */
+    private void setupArchiveWithCollidingEntries(final String format, final 
String firstName) throws IOException {
+        archive = newTempFile("collisions." + format);
+        final Path contentFile = newTempFile("entry-content").toPath();
+        final String[] names = {firstName, "policy.json", "after.txt"};
+        final String[] contents = {"Original policy with longer content", 
"Updated policy", "After policy"};
+        try (OutputStream out = Files.newOutputStream(archive.toPath());
+                ArchiveOutputStream<ArchiveEntry> aos = 
ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(format, out)) {
+            for (int i = 0; i < names.length; i++) {
+                final byte[] data = contents[i].getBytes(UTF_8);
+                Files.write(contentFile, data);
+                aos.putArchiveEntry(aos.createArchiveEntry(contentFile, 
names[i]));
+                aos.write(data);
+                aos.closeArchiveEntry();
+            }
+        }
+    }
+
     private void setupTar() throws IOException, ArchiveException {
         archive = newTempFile("test.tar");
         final File dummy = newTempFile("x");
@@ -258,6 +323,23 @@ void testSevenZTwoFileVersionWithAutoDetection() throws 
IOException, ArchiveExce
         verifyTargetDir();
     }
 
+    @ParameterizedTest(name = "streaming={0}")
+    @ValueSource(booleans = {false, true})
+    void testTarDuplicateEntries(final boolean streaming) throws IOException {
+        setupArchiveWithCollidingEntries("tar", "policy.json");
+        if (streaming) {
+            try (InputStream in = Files.newInputStream(archive.toPath()); 
TarArchiveInputStream stream = new TarArchiveInputStream(in)) {
+                new Expander().expand(stream, tempResultDir);
+            }
+        } else {
+            try (TarFile tar = TarFile.builder().setFile(archive).get()) {
+                new Expander().expand(tar, tempResultDir);
+            }
+        }
+        assertArrayEquals("Updated policy".getBytes(UTF_8), 
Files.readAllBytes(tempResultDir.toPath().resolve("policy.json")));
+        assertArrayEquals("After policy".getBytes(UTF_8), 
Files.readAllBytes(tempResultDir.toPath().resolve("after.txt")));
+    }
+
     @Test
     void testTarFileVersion() throws IOException, ArchiveException {
         setupTar();
@@ -267,6 +349,18 @@ void testTarFileVersion() throws IOException, 
ArchiveException {
         verifyTargetDir();
     }
 
+    @ParameterizedTest(name = "streaming={0}")
+    @ValueSource(booleans = {false, true})
+    void testZipDuplicateEntries(final boolean streaming) throws IOException {
+        setupArchiveWithCollidingEntries("zip", "policy.json");
+        try (ZipFile zip = ZipFile.builder().setFile(archive).get()) {
+            assertZipEntryContents(zip, "policy.json", "Original policy with 
longer content");
+            expandZip(zip, streaming);
+        }
+        assertArrayEquals("Updated policy".getBytes(UTF_8), 
Files.readAllBytes(tempResultDir.toPath().resolve("policy.json")));
+        assertArrayEquals("After policy".getBytes(UTF_8), 
Files.readAllBytes(tempResultDir.toPath().resolve("after.txt")));
+    }
+
     @Test
     void testZipFileVersion() throws IOException, ArchiveException {
         setupZip();
@@ -276,6 +370,19 @@ void testZipFileVersion() throws IOException, 
ArchiveException {
         verifyTargetDir();
     }
 
+    @ParameterizedTest(name = "streaming={0}")
+    @ValueSource(booleans = {false, true})
+    void testZipNormalizedEntryNames(final boolean streaming) throws 
IOException {
+        setupArchiveWithCollidingEntries("zip", "./policy.json");
+        try (ZipFile zip = ZipFile.builder().setFile(archive).get()) {
+            assertZipEntryContents(zip, "./policy.json", "Original policy with 
longer content");
+            assertZipEntryContents(zip, "policy.json", "Updated policy");
+            expandZip(zip, streaming);
+        }
+        assertArrayEquals("Updated policy".getBytes(UTF_8), 
Files.readAllBytes(tempResultDir.toPath().resolve("policy.json")));
+        assertArrayEquals("After policy".getBytes(UTF_8), 
Files.readAllBytes(tempResultDir.toPath().resolve("after.txt")));
+    }
+
     private void verifyTargetDir() throws IOException {
         assertTrue(new File(tempResultDir, "a").isDirectory(), "a has not been 
created");
         assertTrue(new File(tempResultDir, "a/b").isDirectory(), "a/b has not 
been created");

Reply via email to