11package software .coley .llzip .format .model ;
22
3- import software .coley .llzip .format .compression .ZipCompressions ;
43import software .coley .llzip .format .compression .Decompressor ;
4+ import software .coley .llzip .format .compression .ZipCompressions ;
5+ import software .coley .llzip .format .read .ZipReaderStrategy ;
56import software .coley .llzip .util .ByteData ;
67import software .coley .llzip .util .ByteDataUtil ;
78
@@ -63,10 +64,35 @@ public void read(ByteData data, long offset) {
6364 fileData = data .sliceOf (offset + 30 + fileNameLength + extraFieldLength , fileDataLength );
6465 }
6566
67+ /**
68+ * Checks if the contents do not match those described in {@link CentralDirectoryFileHeader}.
69+ * If this is the case you will probably want to change your ZIP reading configuration.
70+ * <p>
71+ * You can override the {@link ZipReaderStrategy#postProcessLocalFileHeader(LocalFileHeader)}
72+ * to call {@link #adoptLinkedCentralDirectoryValues()}. The implementations of {@link ZipReaderStrategy}
73+ * are non-final, so you can extend them to add the override.
74+ *
75+ * @return {@code true} when the contents of this file header do not match those outlined by the associated
76+ * {@link CentralDirectoryFileHeader}.
77+ */
78+ public boolean hasDifferentValuesThanCentralDirectoryHeader () {
79+ if (linkedDirectoryFileHeader == null ) return false ;
80+ if (versionNeededToExtract != linkedDirectoryFileHeader .getVersionNeededToExtract ()) return true ;
81+ if (generalPurposeBitFlag != linkedDirectoryFileHeader .getGeneralPurposeBitFlag ()) return true ;
82+ if (compressionMethod != linkedDirectoryFileHeader .getCompressionMethod ()) return true ;
83+ if (lastModFileTime != linkedDirectoryFileHeader .getLastModFileTime ()) return true ;
84+ if (lastModFileDate != linkedDirectoryFileHeader .getLastModFileDate ()) return true ;
85+ if (crc32 != linkedDirectoryFileHeader .getCrc32 ()) return true ;
86+ if (compressedSize != linkedDirectoryFileHeader .getCompressedSize ()) return true ;
87+ if (uncompressedSize != linkedDirectoryFileHeader .getUncompressedSize ()) return true ;
88+ if (fileNameLength != linkedDirectoryFileHeader .getFileNameLength ()) return true ;
89+ return !Objects .equals (getFileNameAsString (), linkedDirectoryFileHeader .getFileNameAsString ());
90+ }
91+
6692 /**
6793 * When called before being {@link #freeze() frozen} values can be adopted from the linked
6894 * {@link #getLinkedDirectoryFileHeader() CentralDirectoryFileHeader}.
69- * <br >
95+ * <p >
7096 * In some cases the {@link LocalFileHeader} file size may be 0, but the authoritative CEN states a non-0 value,
7197 * which you may want to adopt.
7298 */
@@ -95,7 +121,7 @@ public void adoptLinkedCentralDirectoryValues() {
95121
96122 /**
97123 * Clears the reference to the source {@link ByteData}, preventing further modification.
98- * <br >
124+ * <p >
99125 * Prevents usage of {@link #adoptLinkedCentralDirectoryValues()}.
100126 */
101127 public void freeze () {
@@ -236,8 +262,8 @@ public void setCrc32(int crc32) {
236262 * Be aware that these attributes can be falsified.
237263 * Different zip-parsing programs treat the files differently
238264 * and may not adhere to what you expect from the zip specification.
239- * <br >
240- * When in doubt, trust {@code data. length()} from {@link #getFileData ()}.
265+ * <p >
266+ * When in doubt, trust the length provided by the {@link #getLinkedDirectoryFileHeader ()}.
241267 *
242268 * @return Compressed size of {@link #getFileData()}.
243269 */
@@ -257,6 +283,9 @@ public void setCompressedSize(long compressedSize) {
257283 * Be aware that these attributes can be falsified.
258284 * Different zip-parsing programs treat the files differently
259285 * and may not adhere to what you expect from the zip specification.
286+ * <p>
287+ * When in doubt, trust the length provided by the {@link #getLinkedDirectoryFileHeader()} or
288+ * {@code data.length()} from {@link #getFileData()}.
260289 *
261290 * @return Uncompressed size after {@link #decompress(Decompressor)} is used on {@link #getFileData()}.
262291 */
0 commit comments