From 860965237a746e481071a656377633f84dfb388b Mon Sep 17 00:00:00 2001 From: Srikanth Reddy Lingala Date: Wed, 10 Nov 2021 06:31:38 +0100 Subject: [PATCH] #365 Improve file header equals check to include offset of local file header --- .../net/lingala/zip4j/model/FileHeader.java | 24 ++++++++++++++++++ .../lingala/zip4j/RemoveFilesFromZipIT.java | 12 +++++++++ .../zip_with_duplicate_entries.zip | Bin 0 -> 6034 bytes 3 files changed, 36 insertions(+) create mode 100644 src/test/resources/test-archives/zip_with_duplicate_entries.zip 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 0000000000000000000000000000000000000000..5f883af32282df5bf73107bc5d4bb2d3576918a1 GIT binary patch literal 6034 zcmeHLWmuG3+a(1laR?EHk^yN6X^DZM8>G7#B!&=?4grw{K|;E_8$n7s2Bf>Y1_TM= zgL97O9M5^*AMgKf=9=riWx1HDNHZC z`NrGqG)dT6#xINrw&;}#x$u=ECp7k!aot@ZqaPKQw(&n$qUWCIP8m>LJuGEx9znc?2@G`h%R^uDf&8r zwaPB2Zye)iQJtpa;pim(8wkRTb0-E zGw`PBjA|0g0q>aO8>S0w$tsbdOPXG~8G3^`Blnbkv2H`y;{)KvGr=HaB%)ufGq5&t zurRyzZ&zp1cD44t$En5vDjKTR9OCJQf;Tc<3mSMmDb_O4Cw4l@((p>CD(by}h=b4f zZj89B=~(*mSXH$^wPtHKZP6xjv%4eQc5b|ghf7fd)FIQZnTK)~(r#~6n^sNwwq>Op zg(%w;q4M@wfw5+Z)okk@I65Qrhx4#UAIG`?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