Author: bodewig Date: Mon May 20 15:33:57 2013 New Revision: 1484499 URL: http://svn.apache.org/r1484499 Log: COMPRESS-227 ensure ZipFile#getEntry only returns entries that will return non-null InputStreams in getInputStream.
nameMap isn't used before all local file headers are parsed so it is easier to only populate it then. This not only ensure it only contains ZipArchiveEntries that are known to the entries map, it also simplifies the case where an entry's name changes due to extra fields. Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=1484499&r1=1484498&r2=1484499&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java Mon May 20 15:33:57 2013 @@ -534,8 +534,6 @@ public class ZipFile { // data offset will be filled later entries.put(ze, offset); - nameMap.put(ze.getName(), ze); - byte[] cdExtraData = new byte[extraLen]; archive.readFully(cdExtraData); ze.setCentralDirectoryExtra(cdExtraData); @@ -889,12 +887,9 @@ public class ZipFile { NameAndComment nc = entriesWithoutUTF8Flag.get(ze); ZipUtil.setNameAndCommentFromExtraFields(ze, nc.name, nc.comment); - if (!orig.equals(ze.getName())) { - nameMap.remove(orig); - nameMap.put(ze.getName(), ze); - } } entries.put(ze, offsetEntry); + nameMap.put(ze.getName(), ze); } } Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java?rev=1484499&r1=1484498&r2=1484499&view=diff ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java (original) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java Mon May 20 15:33:57 2013 @@ -204,6 +204,48 @@ public class ZipFileTest extends TestCas } } + public void XtestDuplicateEntry() throws Exception { + File f = File.createTempFile("commons-compress-zipfiletest", ".zip"); + f.deleteOnExit(); + File f2 = File.createTempFile("commons-compress-zipfiletest", ".txt"); + f2.deleteOnExit(); + + OutputStream o = null; + try { + o = new FileOutputStream(f); + ZipArchiveOutputStream zo = new ZipArchiveOutputStream(o); + // simple way to ensure entries have extra data + zo.setUseZip64(Zip64Mode.Always); + + // add the same file twice + ZipArchiveEntry ze = new ZipArchiveEntry(f2, "foo"); + zo.putArchiveEntry(ze); + zo.write(new byte[0]); + zo.closeArchiveEntry(); + ze = new ZipArchiveEntry(f2, "foo"); + zo.putArchiveEntry(ze); + zo.write(new byte[0]); + zo.closeArchiveEntry(); + zo.close(); + + o.close(); + o = null; + + System.err.println("-------------------vvvvvvvvvvvvvvvvvvvvvvv-----------"); + zf = new ZipFile(f); + ze = zf.getEntry("foo"); + assertNotNull(ze); + assertNotNull(zf.getInputStream(ze)); + } finally { + System.err.println("-------------------^^^^^^^^^^^^^^^^^^^^^^^-----------"); + if (o != null) { + o.close(); + } + f.delete(); + f2.delete(); + } + } + /* * ordertest.zip has been handcrafted. *