am bbb8672d: Merge "Fix ConcurrentCloseTest\'s write test to ensure we\'re really blocked in write(2)."

* commit 'bbb8672d27383e03f3b334e8738d4a4d0e737731':
  Fix ConcurrentCloseTest's write test to ensure we're really blocked in write(2).
diff --git a/luni/src/main/java/java/util/zip/ZipFile.java b/luni/src/main/java/java/util/zip/ZipFile.java
index 4dacb82..172141c 100644
--- a/luni/src/main/java/java/util/zip/ZipFile.java
+++ b/luni/src/main/java/java/util/zip/ZipFile.java
@@ -350,7 +350,10 @@
         byte[] hdrBuf = new byte[CENHDR]; // Reuse the same buffer for each entry.
         for (int i = 0; i < numEntries; ++i) {
             ZipEntry newEntry = new ZipEntry(hdrBuf, bufferedStream);
-            entries.put(newEntry.getName(), newEntry);
+            String entryName = newEntry.getName();
+            if (entries.put(entryName, newEntry) != null) {
+                throw new ZipException("Duplicate entry name: " + entryName);
+            }
         }
     }
 
diff --git a/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java b/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java
index fa7b8c7..49dc050 100644
--- a/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java
+++ b/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java
@@ -17,9 +17,11 @@
 package libcore.java.util.zip;
 
 import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Enumeration;
@@ -31,6 +33,7 @@
 import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 import junit.framework.TestCase;
+import libcore.io.IoUtils;
 
 public final class ZipFileTest extends TestCase {
     /**
@@ -54,6 +57,67 @@
         zipFile.close();
     }
 
+    private static void replaceBytes(byte[] original, byte[] replacement, byte[] buffer) {
+        // Gotcha here: original and replacement must be the same length
+        assertEquals(original.length, replacement.length);
+        boolean found;
+        for(int i=0; i < buffer.length - original.length; i++) {
+            found = false;
+            if (buffer[i] == original[0]) {
+                found = true;
+                for (int j=0; j < original.length; j++) {
+                    if (buffer[i+j] != original[j]) {
+                        found = false;
+                        break;
+                    }
+                }
+            }
+            if (found) {
+                for (int j=0; j < original.length; j++) {
+                    buffer[i+j] = replacement[j];
+                }
+            }
+        }
+    }
+
+    /**
+     * Make sure we don't fail silently for duplicate entries.
+     * b/8219321
+     */
+    public void testDuplicateEntries() throws IOException {
+        String entryName = "test_file_name1";
+        String tmpName = "test_file_name2";
+
+        // create the template data
+        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+        ZipOutputStream out = new ZipOutputStream(bytesOut);
+        ZipEntry ze1 = new ZipEntry(tmpName);
+        out.putNextEntry(ze1);
+        out.closeEntry();
+        ZipEntry ze2 = new ZipEntry(entryName);
+        out.putNextEntry(ze2);
+        out.closeEntry();
+        out.close();
+
+        // replace the bytes we don't like
+        byte[] buf = bytesOut.toByteArray();
+        replaceBytes(tmpName.getBytes(), entryName.getBytes(), buf);
+
+        // write the result to a file
+        File badZip = File.createTempFile("badzip", "zip");
+        badZip.deleteOnExit();
+        FileOutputStream outstream = new FileOutputStream(badZip);
+        outstream.write(buf);
+        outstream.close();
+
+        // see if we can still handle it
+        try {
+            ZipFile bad = new ZipFile(badZip);
+            fail();
+        } catch (ZipException expected) {
+        }
+    }
+
     public void testInflatingStreamsRequiringZipRefill() throws IOException {
         int originalSize = 1024 * 1024;
         byte[] readBuffer = new byte[8192];