Skip to content

Commit

Permalink
Don't duplicate META-INF entries in nested directory jars
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Dec 21, 2023
1 parent b218528 commit f31ffbf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,7 @@ private static ZipContent loadNestedDirectory(Source source, ZipContent zip, Ent
.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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ void createWhenNestedJarFileOpensJar() throws IOException {
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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,9 @@ void nestedJarFileWhenNameEndsInSlashThrowsException() {
@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");
}
}

Expand All @@ -230,9 +228,8 @@ void getDataWhenNestedDirectoryReturnsVirtualZipDataBlock() throws IOException {
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();
Expand Down

0 comments on commit f31ffbf

Please sign in to comment.