diff --git a/src/main/java/net/lingala/zip4j/model/FileHeader.java b/src/main/java/net/lingala/zip4j/model/FileHeader.java index b9aedc63..2640f09b 100755 --- a/src/main/java/net/lingala/zip4j/model/FileHeader.java +++ b/src/main/java/net/lingala/zip4j/model/FileHeader.java @@ -18,6 +18,8 @@ import net.lingala.zip4j.headers.HeaderSignature; +import java.util.Objects; + public class FileHeader extends AbstractFileHeader { private int versionMadeBy; @@ -92,4 +94,26 @@ public void setFileComment(String fileComment) { public String toString() { return getFileName(); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + FileHeader that = (FileHeader) o; + return determineOffsetOfLocalFileHeader(this) == determineOffsetOfLocalFileHeader(that); + } + + @Override + public int hashCode() { + return Objects.hash(getFileName(), determineOffsetOfLocalFileHeader(this)); + } + + private long determineOffsetOfLocalFileHeader(FileHeader fileHeader) { + if (fileHeader.getZip64ExtendedInfo() != null) { + return fileHeader.getZip64ExtendedInfo().getOffsetLocalHeader(); + } + + return fileHeader.getOffsetLocalHeader(); + } } diff --git a/src/test/java/net/lingala/zip4j/RemoveFilesFromZipIT.java b/src/test/java/net/lingala/zip4j/RemoveFilesFromZipIT.java index a81ff237..909e5bdd 100644 --- a/src/test/java/net/lingala/zip4j/RemoveFilesFromZipIT.java +++ b/src/test/java/net/lingala/zip4j/RemoveFilesFromZipIT.java @@ -234,6 +234,18 @@ public void testRemoveEntryWithAnotherSimilarNameFromZipRemovesOnlyTheEntryToBeR assertZipFileDoesNotContainsFileByName(new ZipFile(zipFileUnderTest), fileNameToRemove); } + @Test + public void testRemoveEntryFromAZipFileWithDuplicateEntriesRemovesSuccessfully() throws IOException { + TestUtils.copyFile(TestUtils.getTestArchiveFromResources("zip_with_duplicate_entries.zip"), generatedZipFile); + ZipFile zipFile = new ZipFile(generatedZipFile); + int actualNumberOfEntries = zipFile.getFileHeaders().size(); + zipFile.removeFile("sample.pdf"); + + zipFile = new ZipFile(generatedZipFile); + assertThat(zipFile.getFileHeaders().size()).isEqualTo(actualNumberOfEntries - 1); + assertZipFileDoesNotContainsFileByName(zipFile, "sample.pdf"); + } + private void testRemoveEntryFromZipWhichHasCentralDirEntriesInDifferentOrderThanLocalEntries( String fileNameToRemove) throws IOException { TestUtils.copyFile(TestUtils.getTestArchiveFromResources("cen_dir_entries_diff_order_as_local_entries.zip"), diff --git a/src/test/resources/test-archives/zip_with_duplicate_entries.zip b/src/test/resources/test-archives/zip_with_duplicate_entries.zip new file mode 100644 index 00000000..5f883af3 Binary files /dev/null and b/src/test/resources/test-archives/zip_with_duplicate_entries.zip differ