diff --git a/java/java-tests/testData/vfs/sample.jar b/java/java-tests/testData/vfs/sample.jar new file mode 100644 index 000000000000..acbe629b519d Binary files /dev/null and b/java/java-tests/testData/vfs/sample.jar differ diff --git a/platform/core-impl/src/com/intellij/openapi/vfs/impl/ZipHandlerBase.java b/platform/core-impl/src/com/intellij/openapi/vfs/impl/ZipHandlerBase.java index b250d3ff26d5..94991d5b9238 100644 --- a/platform/core-impl/src/com/intellij/openapi/vfs/impl/ZipHandlerBase.java +++ b/platform/core-impl/src/com/intellij/openapi/vfs/impl/ZipHandlerBase.java @@ -50,7 +50,7 @@ public abstract class ZipHandlerBase extends ArchiveHandler { } @NotNull - protected Map buildEntryMapForZipFile(ZipFile zip) { + protected Map buildEntryMapForZipFile(@NotNull ZipFile zip) { Map map = new ZipEntryMap(zip.size()); map.put("", createRootEntry()); @@ -80,6 +80,7 @@ public abstract class ZipHandlerBase extends ArchiveHandler { EntryInfo info = map.get(entryName); if (info != null) return info; + entryName = StringUtil.trimStart(entryName, "/"); Trinity path = splitPathAndFix(entryName); EntryInfo parentInfo = getOrCreate(path.first, map, zip); if (".".equals(path.second)) { diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/JarFileSystemTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/JarFileSystemTest.java index 7bf992a9cbbf..701e360f1c46 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/JarFileSystemTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/JarFileSystemTest.java @@ -3,6 +3,7 @@ package com.intellij.openapi.vfs.local; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.application.ex.PathManagerEx; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.io.FileAttributes; import com.intellij.openapi.util.io.FileUtil; @@ -17,6 +18,7 @@ import com.intellij.openapi.vfs.newvfs.BulkFileListener; import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent; import com.intellij.openapi.vfs.newvfs.events.VFileEvent; import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.testFramework.UsefulTestCase; import com.intellij.testFramework.fixtures.BareTestFixtureTestCase; import com.intellij.testFramework.rules.TempDirectory; import com.intellij.util.containers.ContainerUtil; @@ -297,6 +299,19 @@ public class JarFileSystemTest extends BareTestFixtureTestCase { } } + @Test + public void testCrazyJarWithDuplicateEntriesMustNotCrashAnything() { + String jarPath = PathManagerEx.getTestDataPath() + "/vfs/sample.jar"; + VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(jarPath); + assertNotNull(vFile); + + VirtualFile jarRoot = JarFileSystem.getInstance().getRootByLocal(vFile); + assertNotNull(jarRoot); + String[] children = JarFileSystem.getInstance().list(jarRoot); + assertEquals("com", UsefulTestCase.assertOneElement(children)); + assertEquals("Hello.class", UsefulTestCase.assertOneElement(JarFileSystem.getInstance().list(jarRoot.findFileByRelativePath("com")))); + } + @NotNull private static VirtualFile findByPath(String path) { VirtualFile file = JarFileSystem.getInstance().findFileByPath(path);