You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/01/31 12:46:15 UTC

(commons-compress) branch master updated: COMPRESS-658: fix formatting the lowest expressable DOS time (#472)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new 6be61f586 COMPRESS-658: fix formatting the lowest expressable DOS time (#472)
6be61f586 is described below

commit 6be61f58683ae4f40f7e8e0e47ce82b255210c9f
Author: Arnout Engelen <ar...@engelen.eu>
AuthorDate: Wed Jan 31 13:46:09 2024 +0100

    COMPRESS-658: fix formatting the lowest expressable DOS time (#472)
    
    Since COMPRESS-613, we add an 'extra time data' field when the ZIP entry
    timestamp is outside the range the DOS timestamp used in other parts of the
    header can handle.
    
    Before, it incorrectly also added this header when the ZIP entry timestamp
    was exactly the lowest possible timestamp expressable as a DOS timestamp.
    
    This had the consequence that, when reading a ZIP file with such entries and
    writing it back again, ZipEntry would 'invent' an 'extra time data' field
    where none was needed.
    
    With this change, it correctly considers the lowest expressable date as
    expressable, and avoids inventing the extra time data field in this case.
---
 .../java/org/apache/commons/compress/archivers/zip/ZipUtil.java     | 6 +++++-
 .../java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java | 3 +++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
index e3ea4ba53..19caa0d30 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
@@ -76,6 +76,9 @@ public abstract class ZipUtil {
      */
     private static final long DOSTIME_BEFORE_1980 = 1 << 21 | 1 << 16; // 0x210000
 
+    /** Java time representation of the smallest date/time ZIP can handle */
+    private static final long DOSTIME_BEFORE_1980_AS_JAVA_TIME = dosToJavaTime(DOSTIME_BEFORE_1980);
+
     /**
      * Approximately 128 years, in milliseconds (ignoring leap years, etc.).
      *
@@ -230,7 +233,8 @@ public abstract class ZipUtil {
      * @since 1.23
      */
     public static boolean isDosTime(final long time) {
-        return time <= UPPER_DOSTIME_BOUND && javaToDosTime(time) != DOSTIME_BEFORE_1980;
+        return time <= UPPER_DOSTIME_BOUND &&
+                (time == DOSTIME_BEFORE_1980_AS_JAVA_TIME || javaToDosTime(time) != DOSTIME_BEFORE_1980);
     }
 
     private static LocalDateTime javaEpochToLocalDateTime(final long time) {
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java
index 2a521f7f2..f48031d27 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipUtilTest.java
@@ -160,6 +160,9 @@ public class ZipUtilTest {
         assertTrue(ZipUtil.isDosTime(toLocalInstant("1980-01-03T00:00:00").toEpochMilli()));
         assertTrue(ZipUtil.isDosTime(toLocalInstant("2097-11-27T00:00:00").toEpochMilli()));
         assertFalse(ZipUtil.isDosTime(toLocalInstant("2099-01-01T00:00:00").toEpochMilli()));
+        // The lowest data/time expressable as DOS Time, see comment in ZipUtil#DOSTIME_BEFORE_1980
+        long lowestExpressableDosTime = 1 << 21 | 1 << 16; // 0x210000
+        assertTrue(ZipUtil.isDosTime(ZipUtil.dosToJavaTime(lowestExpressableDosTime)));
     }
 
     @Test