javawindowszipfile-manager

File Manager of windows 10 showing wrong timezone for zip preview


While creating zip I am adding the last modified date as the current time mili for all zipEntries. But previewing the file in file-manager shows the lastModifiedate in WET timezone (same timezone as the build server from where the zip is created).

But if I open the same pdf in Winrar application it shows the lastModifiedDate in same timezone as local

Please find the sample code snippet below

ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath + fileName)), Charset.forName("UTF-8"));
ZipEntry zEntry = new ZipEntry("filePath");
zEntry.setLastModifiedTime(FileTime.fromMillis(System.currentTimeMillis()));
zout.putNextEntry(zEntry);

//file write logic in zip file with BufferedInputStream

zout.flush();
zout.closeEntry();

How to resolve this issue?


Solution

  • I don't know the resolution to your problem, but I can provide some hints that may get you there.

    The base zip file format stores a local last modified time with no time zone. If that time is used for display or extraction, it will be the same local time anywhere, and so will be incorrect if looked at in a time zone different from the one in which it was made.

    The zip format is old, and that deficiency was corrected with "extra" fields in the file headers and central directory. Normally Java's ZipOutputStream does not write such an extra field. However if you use setLastModifiedTime, it does write an "extended timestamp" (0x5455) field with a UT time, not a local time. That extended timestamp field uses the Unix format for such a time (seconds since Jan 1, 1970 UTC).

    Now it is up to whatever is viewing or extracting the zip file to look at and use that extra field. Or not. I cannot speak for what your file manager or WinRAR are doing in that case. There are other extra fields defined for storing times in zip files, including an "NTFS" extra field (0x000a), that has the Windows modification, access, and creation times. Different zip file software may be looking for some, all, or none of these and other extra fields in the zip file headers.