8246129: ZIP entries created for DOS epoch include local timezone metadata

Reviewed-by: lancea
This commit is contained in:
Claes Redestad 2020-06-10 20:53:04 +02:00
parent e47b2bc8c3
commit 99136026b8
3 changed files with 170 additions and 23 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -133,11 +133,6 @@ public class ZipEntry implements ZipConstants, Cloneable {
comment = e.comment;
}
/**
* Creates a new un-initialized zip entry
*/
ZipEntry() {}
/**
* Returns the name of the entry.
* @return the name of the entry
@ -167,10 +162,15 @@ public class ZipEntry implements ZipConstants, Cloneable {
this.xdostime = javaToExtendedDosTime(time);
// Avoid setting the mtime field if time is in the valid
// range for a DOS time
if (xdostime != DOSTIME_BEFORE_1980 && time <= UPPER_DOSTIME_BOUND) {
if (this.xdostime != DOSTIME_BEFORE_1980 && time <= UPPER_DOSTIME_BOUND) {
this.mtime = null;
} else {
this.mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
int localYear = javaEpochToLocalDateTime(time).getYear();
if (localYear >= 1980 && localYear <= 2099) {
this.mtime = null;
} else {
this.mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
}
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -131,14 +131,8 @@ class ZipUtils {
/**
* Converts Java time to DOS time.
*/
private static long javaToDosTime(long time) {
Instant instant = Instant.ofEpochMilli(time);
LocalDateTime ldt = LocalDateTime.ofInstant(
instant, ZoneId.systemDefault());
private static long javaToDosTime(LocalDateTime ldt) {
int year = ldt.getYear() - 1980;
if (year < 0) {
return (1 << 21) | (1 << 16);
}
return (year << 25 |
ldt.getMonthValue() << 21 |
ldt.getDayOfMonth() << 16 |
@ -154,14 +148,17 @@ class ZipUtils {
* @param time milliseconds since epoch
* @return DOS time with 2s remainder encoded into upper half
*/
public static long javaToExtendedDosTime(long time) {
if (time < 0) {
return ZipEntry.DOSTIME_BEFORE_1980;
static long javaToExtendedDosTime(long time) {
LocalDateTime ldt = javaEpochToLocalDateTime(time);
if (ldt.getYear() >= 1980) {
return javaToDosTime(ldt) + ((time % 2000) << 32);
}
long dostime = javaToDosTime(time);
return (dostime != ZipEntry.DOSTIME_BEFORE_1980)
? dostime + ((time % 2000) << 32)
: ZipEntry.DOSTIME_BEFORE_1980;
return ZipEntry.DOSTIME_BEFORE_1980;
}
static LocalDateTime javaEpochToLocalDateTime(long time) {
Instant instant = Instant.ofEpochMilli(time);
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}
/**