Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4305164

java.util.zip.ZipFile reads "last modified" time incorrectly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • core-libs
    • sparc
    • solaris_7



      Name: tb29552 Date: 01/17/2000


      /*
      java version "1.3beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      Java(TM) HotSpot Client VM (build 1.3beta-O-release, 1.3beta-O-release,
      interpreted mode)


      When reading a .zip file using java.util.zip.ZipFile, calls to ZipEntry.getTime
      () will return the wrong date/time. This only affects reading a .zip file -
      writing works correctly. On Solaris SPARC, the bug is not present in
      JDK1.2.1_04, only in JDK1.3beta; I can't speak for other platforms.

      The cause of the problem is reading 16 bits instead of 32 bits of data for the
      time field(s). (The confusion probably arose because the time field(s) can
      either be viewed as a single 32-bit field for date and time, or one 16-bit
      field for the time and one for the date.)

        To reproduce, see test case below. Output is as below. The two dates/times
      should be almost identical (they would not be totally identical due to
      rounding) but they are clearly not.

      Java version is 1.3beta
      Setting time to: 948045699327 [Sun Jan 16 18:01:39 GMT+00:00 2000]
      Time read back as: 312832898000 [Fri Nov 30 18:01:38 GMT+00:00 1979]

      TEST CASE:
      */
      import java.io.IOException;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.util.Date;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipFile;
      import java.util.zip.ZipOutputStream;

      public class ZipBug {

          static {
      System.out.println ("Java version is " +
      System.getProperty ("java.version"));
          }

          static final File tempFile = new File ("test.zip");

          public static void main (String args []) throws IOException {
      writeZip ();
      ReadZip.readZip ();
          }

          static void writeZip () throws IOException {
      ZipOutputStream zOut;
      zOut = new ZipOutputStream (new FileOutputStream (tempFile));
      ZipEntry ze = new ZipEntry ("test");
      Date date = new Date (948045699327L); // constant date+time for testing
      long time = date.getTime ();
      System.out.println ("Setting time to: " + time + " [" + date + "]");
      ze.setTime (time);
      zOut.putNextEntry (ze);
      zOut.write (123); // 1-byte file
      zOut.close ();
          }

          // This is a separate class so that ZipBug can be run in JDK1.3beta and
          // then ZipBug.ReadZip can be run in JDK1.2.1, or vice versa.
          public static class ReadZip {

      public static void main (String args []) throws IOException {
      readZip ();
      }

      static void readZip () throws IOException {
      ZipFile zf = new ZipFile (ZipBug.tempFile);
      ZipEntry ze = (ZipEntry) zf.entries ().nextElement ();
      long time = ze.getTime ();
      Date date = new Date (time);
      System.out.println
      ("Time read back as: " + time + " [" + date + "]");
      zf.close ();
      }
          } // end class ReadZip
      }
      (Review ID: 100034)
      ======================================================================

            zlisunw Zhenghua Li (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: