Patch changes toDosTime and the calls to toDosTime (in writeLocalFileHeader and writeCentralFileHeader)

It may help to alleviate the performance bug that has been reported, I've included a unit test, but I cannot run a performance test right now, so in effect it may slow everything down even more (but I really doubt it :)).

Submitted in the hope that it will help or at least be of benefit.

Kev
Index: ZipOutputStream.java
===================================================================
RCS file: 
/home/cvspublic/ant/src/main/org/apache/tools/zip/ZipOutputStream.java,v
retrieving revision 1.25
diff -u -r1.25 ZipOutputStream.java
--- ZipOutputStream.java        5 Nov 2004 14:46:25 -0000       1.25
+++ ZipOutputStream.java        9 Nov 2004 07:30:57 -0000
@@ -25,7 +25,6 @@
 import java.io.RandomAccessFile;
 import java.io.UnsupportedEncodingException;
 import java.util.Calendar;
-import java.util.Date;
 import java.util.Hashtable;
 import java.util.Vector;
 import java.util.zip.CRC32;
@@ -219,6 +218,12 @@
     public static final int STORED = ZipEntry.STORED;
 
     /**
+     * Calendar instance
+     * 
+     */
+    private static Calendar cal = Calendar.getInstance();
+    
+    /**
      * Creates a new ZIP OutputStream filtering the underlying stream.
      *
      * @since 1.1
@@ -581,7 +586,7 @@
         written += 2;
 
         // last mod. time and date
-        writeOut(toDosTime(new Date(ze.getTime())).getBytes());
+        writeOut(toDosTime(ze.getTime()));
         written += 4;
 
         // CRC
@@ -669,7 +674,7 @@
         written += 2;
 
         // last mod. time and date
-        writeOut(toDosTime(new Date(ze.getTime())).getBytes());
+        writeOut(toDosTime(ze.getTime()));
         written += 4;
 
         // CRC
@@ -769,14 +774,13 @@
      *
      * @since 1.1
      */
-    protected static ZipLong toDosTime(Date time) {
-        Calendar cal = Calendar.getInstance();
-        cal.setTime(time);
+    protected static byte[] toDosTime(long time) {
+        cal.setTimeInMillis(time);
         int year = cal.get(Calendar.YEAR);
-        int month = cal.get(Calendar.MONTH) + 1;
         if (year < 1980) {
-            return DOS_TIME_MIN;
+            return DOS_TIME_MIN.getBytes();
         }
+        int month = cal.get(Calendar.MONTH) + 1;
         long value =  ((year - 1980) << 25)
             |         (month << 21)
             |        (cal.get(Calendar.DAY_OF_MONTH) << 16)
@@ -789,7 +793,7 @@
         result[1] = (byte) ((value & 0xFF00) >> 8);
         result[2] = (byte) ((value & 0xFF0000) >> 16);
         result[3] = (byte) ((value & 0xFF000000L) >> 24);
-        return new ZipLong(result);
+        return result;
     }
 
     /**
Index: ZipOutputStreamTest.java
===================================================================
RCS file: ZipOutputStreamTest.java
diff -N ZipOutputStreamTest.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ ZipOutputStreamTest.java    1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,81 @@
+package org.apache.tools.zip;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * @author it-kevin
+ */
+public class ZipOutputStreamTest extends TestCase {
+
+    private Calendar cal = Calendar.getInstance();
+    private ZipLong zl;
+    private Date date;
+    private final ZipLong DOS_TIME_MIN = new ZipLong(0x00002100L);
+    
+    /*
+     * @see TestCase#setUp()
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+        date = new Date();
+        cal.setTime(date);
+        int year = cal.get(Calendar.YEAR);
+        if (year < 1980) {
+            zl = DOS_TIME_MIN;
+        }
+        int month = cal.get(Calendar.MONTH) + 1;
+        long value =  ((year - 1980) << 25)
+            |         (month << 21)
+            |        (cal.get(Calendar.DAY_OF_MONTH) << 16)
+            |         (cal.get(Calendar.HOUR_OF_DAY) << 11)
+            |         (cal.get(Calendar.MINUTE) << 5)
+            |         (cal.get(Calendar.SECOND) >> 1);
+
+        byte[] result = new byte[4];
+        result[0] = (byte) ((value & 0xFF));
+        result[1] = (byte) ((value & 0xFF00) >> 8);
+        result[2] = (byte) ((value & 0xFF0000) >> 16);
+        result[3] = (byte) ((value & 0xFF000000L) >> 24);
+        zl = new ZipLong(result);
+    }
+
+    
+    
+    /*
+     * @see TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    /**
+     * Constructor for ZipOutputStreamTest.
+     * @param arg0
+     */
+    public ZipOutputStreamTest(String arg0) {
+        super(arg0);
+    }
+
+    public static Test suite() {
+               TestSuite suite = new TestSuite();
+               suite.addTest(new ZipOutputStreamTest("testToDosTime"));
+               return suite;
+       }
+    
+    public void testToDosTime() {
+        try {
+            assertEquals(zl.getValue(),new 
ZipLong(ZipOutputStream.toDosTime(date.getTime())).getValue());
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+    
+    
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to