jdk/src/java.base/share/classes/java/util/zip/ZipOutputStream.java
changeset 39772 c2a5d2de5253
parent 37781 71ed5645f17c
child 45434 4582657c7260
equal deleted inserted replaced
39771:ab558f578f30 39772:c2a5d2de5253
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   419             }
   419             }
   420         }
   420         }
   421         byte[] nameBytes = zc.getBytes(e.name);
   421         byte[] nameBytes = zc.getBytes(e.name);
   422         writeShort(nameBytes.length);
   422         writeShort(nameBytes.length);
   423 
   423 
   424         int elenEXTT = 0;               // info-zip extended timestamp
   424         int elenEXTT = 0;         // info-zip extended timestamp
   425         int flagEXTT = 0;
   425         int flagEXTT = 0;
       
   426         long umtime = -1;
       
   427         long uatime = -1;
       
   428         long uctime = -1;
   426         if (e.mtime != null) {
   429         if (e.mtime != null) {
   427             elenEXTT += 4;
   430             elenEXTT += 4;
   428             flagEXTT |= EXTT_FLAG_LMT;
   431             flagEXTT |= EXTT_FLAG_LMT;
       
   432             umtime = fileTimeToUnixTime(e.mtime);
   429         }
   433         }
   430         if (e.atime != null) {
   434         if (e.atime != null) {
   431             elenEXTT += 4;
   435             elenEXTT += 4;
   432             flagEXTT |= EXTT_FLAG_LAT;
   436             flagEXTT |= EXTT_FLAG_LAT;
       
   437             uatime = fileTimeToUnixTime(e.atime);
   433         }
   438         }
   434         if (e.ctime != null) {
   439         if (e.ctime != null) {
   435             elenEXTT += 4;
   440             elenEXTT += 4;
   436             flagEXTT |= EXTT_FLAT_CT;
   441             flagEXTT |= EXTT_FLAT_CT;
   437         }
   442             uctime = fileTimeToUnixTime(e.ctime);
   438         if (flagEXTT != 0)
   443         }
   439             elen += (elenEXTT + 5);    // headid(2) + size(2) + flag(1) + data
   444         if (flagEXTT != 0) {
       
   445             // to use ntfs time if any m/a/ctime is beyond unixtime upper bound
       
   446             if (umtime > UPPER_UNIXTIME_BOUND ||
       
   447                 uatime > UPPER_UNIXTIME_BOUND ||
       
   448                 uctime > UPPER_UNIXTIME_BOUND) {
       
   449                 elen += 36;                // NTFS time, total 36 bytes
       
   450             } else {
       
   451                 elen += (elenEXTT + 5);    // headid(2) + size(2) + flag(1) + data
       
   452             }
       
   453         }
   440         writeShort(elen);
   454         writeShort(elen);
   441         writeBytes(nameBytes, 0, nameBytes.length);
   455         writeBytes(nameBytes, 0, nameBytes.length);
   442         if (hasZip64) {
   456         if (hasZip64) {
   443             writeShort(ZIP64_EXTID);
   457             writeShort(ZIP64_EXTID);
   444             writeShort(16);
   458             writeShort(16);
   445             writeLong(e.size);
   459             writeLong(e.size);
   446             writeLong(e.csize);
   460             writeLong(e.csize);
   447         }
   461         }
   448         if (flagEXTT != 0) {
   462         if (flagEXTT != 0) {
   449             writeShort(EXTID_EXTT);
   463             if (umtime > UPPER_UNIXTIME_BOUND ||
   450             writeShort(elenEXTT + 1);      // flag + data
   464                 uatime > UPPER_UNIXTIME_BOUND ||
   451             writeByte(flagEXTT);
   465                 uctime > UPPER_UNIXTIME_BOUND) {
   452             if (e.mtime != null)
   466                 writeShort(EXTID_NTFS);    // id
   453                 writeInt(fileTimeToUnixTime(e.mtime));
   467                 writeShort(32);            // data size
   454             if (e.atime != null)
   468                 writeInt(0);               // reserved
   455                 writeInt(fileTimeToUnixTime(e.atime));
   469                 writeShort(0x0001);        // NTFS attr tag
   456             if (e.ctime != null)
   470                 writeShort(24);
   457                 writeInt(fileTimeToUnixTime(e.ctime));
   471                 writeLong(e.mtime == null ? WINDOWS_TIME_NOT_AVAILABLE
       
   472                                           : fileTimeToWinTime(e.mtime));
       
   473                 writeLong(e.atime == null ? WINDOWS_TIME_NOT_AVAILABLE
       
   474                                           : fileTimeToWinTime(e.atime));
       
   475                 writeLong(e.ctime == null ? WINDOWS_TIME_NOT_AVAILABLE
       
   476                                           : fileTimeToWinTime(e.ctime));
       
   477             } else {
       
   478                 writeShort(EXTID_EXTT);
       
   479                 writeShort(elenEXTT + 1);  // flag + data
       
   480                 writeByte(flagEXTT);
       
   481                 if (e.mtime != null)
       
   482                     writeInt(umtime);
       
   483                 if (e.atime != null)
       
   484                     writeInt(uatime);
       
   485                 if (e.ctime != null)
       
   486                     writeInt(uctime);
       
   487             }
   458         }
   488         }
   459         writeExtra(e.extra);
   489         writeExtra(e.extra);
   460         locoff = written;
   490         locoff = written;
   461     }
   491     }
   462 
   492 
   526             elen += (elenZIP64 + 4);// + headid(2) + datasize(2)
   556             elen += (elenZIP64 + 4);// + headid(2) + datasize(2)
   527         }
   557         }
   528         // cen info-zip extended timestamp only outputs mtime
   558         // cen info-zip extended timestamp only outputs mtime
   529         // but set the flag for a/ctime, if present in loc
   559         // but set the flag for a/ctime, if present in loc
   530         int flagEXTT = 0;
   560         int flagEXTT = 0;
       
   561         long umtime = -1;
       
   562         long uatime = -1;
       
   563         long uctime = -1;
   531         if (e.mtime != null) {
   564         if (e.mtime != null) {
   532             elen += 4;              // + mtime(4)
       
   533             flagEXTT |= EXTT_FLAG_LMT;
   565             flagEXTT |= EXTT_FLAG_LMT;
       
   566             umtime = fileTimeToUnixTime(e.mtime);
   534         }
   567         }
   535         if (e.atime != null) {
   568         if (e.atime != null) {
   536             flagEXTT |= EXTT_FLAG_LAT;
   569             flagEXTT |= EXTT_FLAG_LAT;
       
   570             uatime = fileTimeToUnixTime(e.atime);
   537         }
   571         }
   538         if (e.ctime != null) {
   572         if (e.ctime != null) {
   539             flagEXTT |= EXTT_FLAT_CT;
   573             flagEXTT |= EXTT_FLAT_CT;
       
   574             uctime = fileTimeToUnixTime(e.ctime);
   540         }
   575         }
   541         if (flagEXTT != 0) {
   576         if (flagEXTT != 0) {
   542             elen += 5;             // headid + sz + flag
   577             // to use ntfs time if any m/a/ctime is beyond unixtime upper bound
       
   578             if (umtime > UPPER_UNIXTIME_BOUND ||
       
   579                 uatime > UPPER_UNIXTIME_BOUND ||
       
   580                 uctime > UPPER_UNIXTIME_BOUND) {
       
   581                 elen += 36;         // NTFS time total 36 bytes
       
   582             } else {
       
   583                 elen += 9;          // headid(2) + sz(2) + flag(1) + mtime (4)
       
   584             }
   543         }
   585         }
   544         writeShort(elen);
   586         writeShort(elen);
   545         byte[] commentBytes;
   587         byte[] commentBytes;
   546         if (e.comment != null) {
   588         if (e.comment != null) {
   547             commentBytes = zc.getBytes(e.comment);
   589             commentBytes = zc.getBytes(e.comment);
   566                 writeLong(e.csize);
   608                 writeLong(e.csize);
   567             if (offset == ZIP64_MAGICVAL)
   609             if (offset == ZIP64_MAGICVAL)
   568                 writeLong(xentry.offset);
   610                 writeLong(xentry.offset);
   569         }
   611         }
   570         if (flagEXTT != 0) {
   612         if (flagEXTT != 0) {
   571             writeShort(EXTID_EXTT);
   613             if (umtime > UPPER_UNIXTIME_BOUND ||
   572             if (e.mtime != null) {
   614                 uatime > UPPER_UNIXTIME_BOUND ||
   573                 writeShort(5);      // flag + mtime
   615                 uctime > UPPER_UNIXTIME_BOUND) {
   574                 writeByte(flagEXTT);
   616                 writeShort(EXTID_NTFS);    // id
   575                 writeInt(fileTimeToUnixTime(e.mtime));
   617                 writeShort(32);            // data size
       
   618                 writeInt(0);               // reserved
       
   619                 writeShort(0x0001);        // NTFS attr tag
       
   620                 writeShort(24);
       
   621                 writeLong(e.mtime == null ? WINDOWS_TIME_NOT_AVAILABLE
       
   622                                           : fileTimeToWinTime(e.mtime));
       
   623                 writeLong(e.atime == null ? WINDOWS_TIME_NOT_AVAILABLE
       
   624                                           : fileTimeToWinTime(e.atime));
       
   625                 writeLong(e.ctime == null ? WINDOWS_TIME_NOT_AVAILABLE
       
   626                                           : fileTimeToWinTime(e.ctime));
   576             } else {
   627             } else {
   577                 writeShort(1);      // flag only
   628                 writeShort(EXTID_EXTT);
   578                 writeByte(flagEXTT);
   629                 if (e.mtime != null) {
       
   630                     writeShort(5);      // flag + mtime
       
   631                     writeByte(flagEXTT);
       
   632                     writeInt(umtime);
       
   633                 } else {
       
   634                     writeShort(1);      // flag only
       
   635                     writeByte(flagEXTT);
       
   636                 }
   579             }
   637             }
   580         }
   638         }
   581         writeExtra(e.extra);
   639         writeExtra(e.extra);
   582         if (commentBytes != null) {
   640         if (commentBytes != null) {
   583             writeBytes(commentBytes, 0, Math.min(commentBytes.length, 0xffff));
   641             writeBytes(commentBytes, 0, Math.min(commentBytes.length, 0xffff));