8206389: JarEntry.setCreation/LastAccessTime without setLastModifiedTime causes Invalid CEN header
Reviewed-by: alanb, martin
--- a/src/java.base/share/classes/java/util/zip/ZipOutputStream.java Mon Jul 09 13:35:55 2018 -0400
+++ b/src/java.base/share/classes/java/util/zip/ZipOutputStream.java Mon Jul 09 13:08:30 2018 -0700
@@ -582,7 +582,9 @@
uctime > UPPER_UNIXTIME_BOUND) {
elen += 36; // NTFS time total 36 bytes
} else {
- elen += 9; // headid(2) + sz(2) + flag(1) + mtime (4)
+ elen += 5; // headid(2) + sz(2) + flag(1)
+ if (e.mtime != null)
+ elen += 4; // + mtime (4)
}
}
writeShort(elen);
--- a/test/jdk/java/util/zip/TestExtraTime.java Mon Jul 09 13:35:55 2018 -0400
+++ b/test/jdk/java/util/zip/TestExtraTime.java Mon Jul 09 13:08:30 2018 -0700
@@ -23,7 +23,8 @@
/**
* @test
- * @bug 4759491 6303183 7012868 8015666 8023713 8068790 8076641 8075526 8130914 8161942
+ * @bug 4759491 6303183 7012868 8015666 8023713 8068790 8076641 8075526 8130914
+ * 8161942 8206389
* @summary Test ZOS and ZIS timestamp in extra field correctly
*/
@@ -96,6 +97,7 @@
testNullHandling();
testTagOnlyHandling();
testTimeConversions();
+ testNullMtime();
}
static void test(FileTime mtime, FileTime atime, FileTime ctime,
@@ -192,9 +194,11 @@
ze.getTime(),
ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
*/
- if (mtime.to(TimeUnit.SECONDS) !=
- ze.getLastModifiedTime().to(TimeUnit.SECONDS))
+ if (mtime != null &&
+ mtime.to(TimeUnit.SECONDS) !=
+ ze.getLastModifiedTime().to(TimeUnit.SECONDS)) {
throw new RuntimeException("Timestamp: storing mtime failed!");
+ }
if (atime != null &&
atime.to(TimeUnit.SECONDS) !=
ze.getLastAccessTime().to(TimeUnit.SECONDS))
@@ -305,4 +309,52 @@
Files.delete(zpath);
}
}
+
+ static void checkLastModifiedTimeDOS(FileTime mtime, ZipEntry ze) {
+ FileTime lmt = ze.getLastModifiedTime();
+ if ((lmt.to(TimeUnit.SECONDS) >>> 1) != (mtime.to(TimeUnit.SECONDS) >>> 1) ||
+ lmt.to(TimeUnit.MILLISECONDS) != ze.getTime() ||
+ lmt.to(TimeUnit.MILLISECONDS) % 1000 != 0) {
+ throw new RuntimeException("Timestamp: storing mtime in dos format failed!");
+ }
+ }
+
+ static void testNullMtime() throws Throwable {
+ Instant now = Instant.now();
+ FileTime ctime = FileTime.from(now);
+ FileTime atime = FileTime.from(now.plusSeconds(7));
+ FileTime mtime = FileTime.from(now.plusSeconds(13));
+ System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
+ mtime, atime, ctime);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (ZipOutputStream zos = new ZipOutputStream(baos)) {
+ ZipEntry ze = new ZipEntry("TestExtraTime.java");
+ ze.setCreationTime(ctime);
+ ze.setLastAccessTime(atime);
+ // ze.setLastModifiedTime(now);
+ ze.setTime(mtime.toMillis());
+ zos.putNextEntry(ze);
+ zos.write(new byte[] { 1,2 ,3, 4});
+ }
+
+ try (ZipInputStream zis = new ZipInputStream(
+ new ByteArrayInputStream(baos.toByteArray()))) {
+ ZipEntry ze = zis.getNextEntry();
+ // check LOC
+ check(null, atime, ctime, ze, null);
+ checkLastModifiedTimeDOS(mtime, ze);
+ }
+
+ Path zpath = Paths.get(System.getProperty("test.dir", "."),
+ "TestExtraTime.zip");
+ Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
+ try (ZipFile zf = new ZipFile(zpath.toFile())) {
+ ZipEntry ze = zf.getEntry("TestExtraTime.java");
+ // check CEN
+ checkLastModifiedTimeDOS(mtime, ze);
+ } finally {
+ Files.delete(zpath);
+ }
+ }
}