JDK-6302424 : ZipEntry.getTime() returns modified time of the ZipFile, not the entry.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.jar
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-07-27
  • Updated: 2011-02-16
  • Resolved: 2005-07-28
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]


EXTRA RELEVANT SYSTEM CONFIGURATION :
NTFS file system

A DESCRIPTION OF THE PROBLEM :
Using java.util.zip.ZipEntry to set and get the time of the entries.

ZipEntry.setTime(X) works correctly. (Checked via 3rd party zip program).
However, when using ZipEntry.getTime() it returns the time of the ZipFile, not the time of the Entry in the ZipFile.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a zip file add entries and set their time stamps.
See source code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
for every file passed to the method you should get 2 statements on the console:
[filename] PreZip: [filetimeinMS]
InZip: [entrytimeinMS]
ie.
c:\test.txt PreZip: 1121699509549
InZip: 1121699509549


ACTUAL -
c:\test.txt PreZip: 1121699509549
InZip: 1121699508000

the 2 times should be identical, however, [entrytimeinMS] will be the time of the ZipFile instead. If you open said ZipFile with WinZip etc, the time reported by them for the entries will be correct.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.zip.*;
import java.io.*;

class  test
{
	public static void main(String[] args) 
	{
		String con[] = { "File1", "File2" };
		test t = new test();
		t.createZip("test.zip", con);
	}
	public void createZip(String zipfilename, String[] contents) {
	try {
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipfilename));
			
		for (int i=0; i<contents.length; i++) {
			ZipEntry ze = new ZipEntry(contents[i]);
			zos.setMethod(ZipOutputStream.DEFLATED);  //indicate deflated
			zos.setLevel(Deflater.DEFAULT_COMPRESSION);  //use default level
			zos.putNextEntry(ze);
			File fin = new File(contents[i]);
			FileInputStream ins = new FileInputStream(fin);
			System.out.println(contents[i] + " PreZip: " + fin.lastModified());
			ze.setTime(fin.lastModified());
			System.out.println("InZip: " + ze.getTime());
			int bread;
			byte[] bin = new byte[4096];
			while ( (bread = ins.read(bin, 0, 4096)) > -1) {
				zos.write(bin, 0, bread);				}
			zos.closeEntry();
		}
			zos.close();
		}
		catch (Exception x) {
			System.out.println(x.toString());
		}
	}
}
---------- END SOURCE ----------

Comments
EVALUATION I think there's some confusion here. The timestamps are reported in milliseconds, but the granularity of the timestamps in the zip file format is only the ancient DOS timestamp granularity, i.e. 2 seconds. I don't believe the submitter's assertion that the timestamp of the zip file is being used. See the zip file spec http://www.info-zip.org/pub/infozip/doc/ date and time fields: (2 bytes each) The date and time are encoded in standard MS-DOS format. There are extensions to the zip file format that support NTFS or Unix-style timestamps, but they are not implemented, and would not be portable. Nevertheless, it would be good to provide them. However, this would be a lot of work. I filed a separate RFE for this. See 6303183.
28-07-2005