Don't duplicate META-INF entries in nested directory jars

Update `ZipContent` so that `META-INF` entries are no longer duplicated
in nested jars created from directory entries. This aligns with the
behavior of the classic loader and prevents the same META-INF file from
being discovered twice.

Fixes gh-38862
This commit is contained in:
Phillip Webb 2023-12-20 17:36:06 -08:00
parent b2185282d4
commit f31ffbf927
3 changed files with 7 additions and 14 deletions

View File

@ -637,10 +637,7 @@ public final class ZipContent implements Closeable {
.load(zip.data, pos);
long namePos = pos + ZipCentralDirectoryFileHeaderRecord.FILE_NAME_OFFSET;
short nameLen = centralRecord.fileNameLength();
if (ZipString.startsWith(loader.buffer, zip.data, namePos, nameLen, META_INF) != -1) {
loader.add(centralRecord, pos, false);
}
else if (ZipString.startsWith(loader.buffer, zip.data, namePos, nameLen, directoryName) != -1) {
if (ZipString.startsWith(loader.buffer, zip.data, namePos, nameLen, directoryName) != -1) {
loader.add(centralRecord, pos, true);
}
}

View File

@ -105,9 +105,8 @@ class NestedJarFileTests {
void createWhenNestedJarDirectoryOpensJar() throws IOException {
try (NestedJarFile jar = new NestedJarFile(this.file, "d/")) {
assertThat(jar.getName()).isEqualTo(this.file.getAbsolutePath() + "!/d/");
assertThat(jar.size()).isEqualTo(3);
assertThat(jar.stream().map(JarEntry::getName)).containsExactly("META-INF/", "META-INF/MANIFEST.MF",
"9.dat");
assertThat(jar.size()).isEqualTo(1);
assertThat(jar.stream().map(JarEntry::getName)).containsExactly("9.dat");
}
}

View File

@ -210,11 +210,9 @@ class ZipContentTests {
@Test
void nestedDirectoryReturnsNestedJar() throws IOException {
try (ZipContent nested = ZipContent.open(this.file.toPath(), "d/")) {
assertThat(nested.size()).isEqualTo(3);
assertThat(nested.size()).isEqualTo(1);
assertThat(nested.getEntry("9.dat")).isNotNull();
assertThat(nested.getEntry(0).getName()).isEqualTo("META-INF/");
assertThat(nested.getEntry(1).getName()).isEqualTo("META-INF/MANIFEST.MF");
assertThat(nested.getEntry(2).getName()).isEqualTo("9.dat");
assertThat(nested.getEntry(0).getName()).isEqualTo("9.dat");
}
}
@ -230,9 +228,8 @@ class ZipContentTests {
File file = new File(this.tempDir, "included.zip");
write(file, nested.openRawZipData());
try (ZipFile loadedZipFile = new ZipFile(file)) {
assertThat(loadedZipFile.size()).isEqualTo(3);
assertThat(loadedZipFile.stream().map(ZipEntry::getName)).containsExactly("META-INF/",
"META-INF/MANIFEST.MF", "9.dat");
assertThat(loadedZipFile.size()).isEqualTo(1);
assertThat(loadedZipFile.stream().map(ZipEntry::getName)).containsExactly("9.dat");
assertThat(loadedZipFile.getEntry("9.dat")).isNotNull();
try (InputStream in = loadedZipFile.getInputStream(loadedZipFile.getEntry("9.dat"))) {
ByteArrayOutputStream out = new ByteArrayOutputStream();