src/java.base/share/classes/java/util/zip/ZipEntry.java

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import static java.util.zip.ZipUtils.*;
  29 import java.nio.file.attribute.FileTime;
  30 import java.util.Objects;
  31 import java.util.concurrent.TimeUnit;



  32 
  33 import static java.util.zip.ZipConstants64.*;
  34 
  35 /**
  36  * This class is used to represent a ZIP file entry.
  37  *
  38  * @author      David Connelly
  39  */
  40 public
  41 class ZipEntry implements ZipConstants, Cloneable {
  42 
  43     String name;        // entry name
  44     long xdostime = -1; // last modification time (in extended DOS time,
  45                         // where milliseconds lost in conversion might
  46                         // be encoded into the upper half)
  47     FileTime mtime;     // last modification time, from extra field data
  48     FileTime atime;     // last access time, from extra field data
  49     FileTime ctime;     // creation time, from extra field data
  50     long crc = -1;      // crc-32 of entry data
  51     long size = -1;     // uncompressed size of entry data


 178      * input stream, this is the last modification time from the {@code
 179      * date and time fields} of the zip file entry. The
 180      * {@link java.util.TimeZone#getDefault() default TimeZone} is used
 181      * to convert the standard MS-DOS formatted date and time to the
 182      * epoch time.
 183      *
 184      * @return  The last modification time of the entry in milliseconds
 185      *          since the epoch, or -1 if not specified
 186      *
 187      * @see #setTime(long)
 188      * @see #setLastModifiedTime(FileTime)
 189      */
 190     public long getTime() {
 191         if (mtime != null) {
 192             return mtime.toMillis();
 193         }
 194         return (xdostime != -1) ? extendedDosToJavaTime(xdostime) : -1;
 195     }
 196 
 197     /**















































































 198      * Sets the last modification time of the entry.
 199      *
 200      * <p> When output to a ZIP file or ZIP file formatted output stream
 201      * the last modification time set by this method will be stored into
 202      * zip file entry's {@code date and time fields} in {@code standard
 203      * MS-DOS date and time format}), and the extended timestamp fields
 204      * in {@code optional extra data} in UTC time.
 205      *
 206      * @param  time
 207      *         The last modification time of the entry
 208      * @return This zip entry
 209      *
 210      * @throws NullPointerException if the {@code time} is null
 211      *
 212      * @see #getLastModifiedTime()
 213      * @since 1.8
 214      */
 215     public ZipEntry setLastModifiedTime(FileTime time) {
 216         this.mtime = Objects.requireNonNull(time, "lastModifiedTime");
 217         this.xdostime = javaToExtendedDosTime(time.to(TimeUnit.MILLISECONDS));


 481                     }
 482                     break;
 483                 case EXTID_NTFS:
 484                     if (sz < 32) // reserved  4 bytes + tag 2 bytes + size 2 bytes
 485                         break;   // m[a|c]time 24 bytes
 486                     int pos = off + 4;               // reserved 4 bytes
 487                     if (get16(extra, pos) !=  0x0001 || get16(extra, pos + 2) != 24)
 488                         break;
 489                     mtime = winTimeToFileTime(get64(extra, pos + 4));
 490                     atime = winTimeToFileTime(get64(extra, pos + 12));
 491                     ctime = winTimeToFileTime(get64(extra, pos + 20));
 492                     break;
 493                 case EXTID_EXTT:
 494                     int flag = Byte.toUnsignedInt(extra[off]);
 495                     int sz0 = 1;
 496                     // The CEN-header extra field contains the modification
 497                     // time only, or no timestamp at all. 'sz' is used to
 498                     // flag its presence or absence. But if mtime is present
 499                     // in LOC it must be present in CEN as well.
 500                     if ((flag & 0x1) != 0 && (sz0 + 4) <= sz) {
 501                         mtime = unixTimeToFileTime(get32(extra, off + sz0));
 502                         sz0 += 4;
 503                     }
 504                     if ((flag & 0x2) != 0 && (sz0 + 4) <= sz) {
 505                         atime = unixTimeToFileTime(get32(extra, off + sz0));
 506                         sz0 += 4;
 507                     }
 508                     if ((flag & 0x4) != 0 && (sz0 + 4) <= sz) {
 509                         ctime = unixTimeToFileTime(get32(extra, off + sz0));
 510                         sz0 += 4;
 511                     }
 512                     break;
 513                  default:
 514                 }
 515                 off += sz;
 516             }
 517         }
 518         this.extra = extra;
 519     }
 520 
 521     /**
 522      * Returns the extra field data for the entry.
 523      *
 524      * @return the extra field data for the entry, or null if none
 525      *
 526      * @see #setExtra(byte[])
 527      */
 528     public byte[] getExtra() {
 529         return extra;




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import static java.util.zip.ZipUtils.*;
  29 import java.nio.file.attribute.FileTime;
  30 import java.util.Objects;
  31 import java.util.concurrent.TimeUnit;
  32 import java.time.LocalDateTime;
  33 import java.time.ZonedDateTime;
  34 import java.time.ZoneId;
  35 
  36 import static java.util.zip.ZipConstants64.*;
  37 
  38 /**
  39  * This class is used to represent a ZIP file entry.
  40  *
  41  * @author      David Connelly
  42  */
  43 public
  44 class ZipEntry implements ZipConstants, Cloneable {
  45 
  46     String name;        // entry name
  47     long xdostime = -1; // last modification time (in extended DOS time,
  48                         // where milliseconds lost in conversion might
  49                         // be encoded into the upper half)
  50     FileTime mtime;     // last modification time, from extra field data
  51     FileTime atime;     // last access time, from extra field data
  52     FileTime ctime;     // creation time, from extra field data
  53     long crc = -1;      // crc-32 of entry data
  54     long size = -1;     // uncompressed size of entry data


 181      * input stream, this is the last modification time from the {@code
 182      * date and time fields} of the zip file entry. The
 183      * {@link java.util.TimeZone#getDefault() default TimeZone} is used
 184      * to convert the standard MS-DOS formatted date and time to the
 185      * epoch time.
 186      *
 187      * @return  The last modification time of the entry in milliseconds
 188      *          since the epoch, or -1 if not specified
 189      *
 190      * @see #setTime(long)
 191      * @see #setLastModifiedTime(FileTime)
 192      */
 193     public long getTime() {
 194         if (mtime != null) {
 195             return mtime.toMillis();
 196         }
 197         return (xdostime != -1) ? extendedDosToJavaTime(xdostime) : -1;
 198     }
 199 
 200     /**
 201      * Sets the last modification time of the entry in local date-time.
 202      *
 203      * <p> If the entry is output to a ZIP file or ZIP file formatted
 204      * output stream the last modification time set by this method will
 205      * be stored into the {@code date and time fields} of the zip file
 206      * entry and encoded in standard {@code MS-DOS date and time format}.
 207      * If the date-time set is out of the range of the standard {@code
 208      * MS-DOS date and time format}, the time will also be stored into
 209      * zip file entry's extended timestamp fields in {@code optional
 210      * extra data} in UTC time. The {@link java.time.ZoneId#systemDefault()
 211      * system default TimeZone} is used to convert the local date-time
 212      * to UTC time.
 213      *
 214      * <p> {@code LocalDateTime} uses a precision of nanoseconds, whereas
 215      * this class uses a precision of milliseconds. The conversion will
 216      * truncate any excess precision information as though the amount in
 217      * nanoseconds was subject to integer division by one million.
 218      *
 219      * @param  time
 220      *         The last modification time of the entry in local date-time
 221      *
 222      * @see #getTimeLocal()
 223      * @since 1.9
 224      */
 225     public void setTimeLocal(LocalDateTime time) {
 226         int year = time.getYear() - 1980;
 227         if (year < 0) {
 228             this.xdostime = DOSTIME_BEFORE_1980;
 229         } else {
 230             this.xdostime = (year << 25 |
 231                 time.getMonthValue() << 21 |
 232                 time.getDayOfMonth() << 16 |
 233                 time.getHour() << 11 |
 234                 time.getMinute() << 5 |
 235                 time.getSecond() >> 1) 
 236                 + ((long)(((time.getSecond() & 0x1) * 1000) +
 237                       time.getNano() / 1000_000) << 32);
 238         }
 239         if (xdostime != DOSTIME_BEFORE_1980 && year <= 0x7f) {
 240             this.mtime = null;
 241         } else {
 242             this.mtime = FileTime.from(
 243                 ZonedDateTime.of(time, ZoneId.systemDefault()).toInstant());
 244         }
 245     }
 246 
 247     /**
 248      * Returns the last modification time of the entry in local date-time.
 249      *
 250      * <p> If the entry is read from a ZIP file or ZIP file formatted
 251      * input stream, this is the last modification time from the zip
 252      * file entry's {@code optional extra data} if the extended timestamp
 253      * fields are present. Otherwise, the last modification time is read
 254      * from entry's standard MS-DOS formatted {@code date and time fields}.
 255      *
 256      * <p> The {@link java.time.ZoneId#systemDefault() system default TimeZone}
 257      * is used to convert the UTC time to local date-time.
 258      *
 259      * @return  The last modification time of the entry in local date-time
 260      *
 261      * @see #setTimeLocal(LocalDateTime)
 262      * @since 1.9
 263      */
 264     public LocalDateTime getTimeLocal() {
 265         if (mtime != null) {
 266             return LocalDateTime.ofInstant(mtime.toInstant(), ZoneId.systemDefault());
 267         }
 268         int ms = (int)(xdostime >> 32);
 269         return LocalDateTime.of((int)(((xdostime >> 25) & 0x7f) + 1980),
 270                              (int)((xdostime >> 21) & 0x0f),
 271                              (int)((xdostime >> 16) & 0x1f),
 272                              (int)((xdostime >> 11) & 0x1f),
 273                              (int)((xdostime >> 5) & 0x3f),
 274                              (int)((xdostime << 1) & 0x3e) + ms / 1000,
 275                              (ms % 1000) * 1000_000);
 276     }
 277 
 278 
 279     /**
 280      * Sets the last modification time of the entry.
 281      *
 282      * <p> When output to a ZIP file or ZIP file formatted output stream
 283      * the last modification time set by this method will be stored into
 284      * zip file entry's {@code date and time fields} in {@code standard
 285      * MS-DOS date and time format}), and the extended timestamp fields
 286      * in {@code optional extra data} in UTC time.
 287      *
 288      * @param  time
 289      *         The last modification time of the entry
 290      * @return This zip entry
 291      *
 292      * @throws NullPointerException if the {@code time} is null
 293      *
 294      * @see #getLastModifiedTime()
 295      * @since 1.8
 296      */
 297     public ZipEntry setLastModifiedTime(FileTime time) {
 298         this.mtime = Objects.requireNonNull(time, "lastModifiedTime");
 299         this.xdostime = javaToExtendedDosTime(time.to(TimeUnit.MILLISECONDS));


 563                     }
 564                     break;
 565                 case EXTID_NTFS:
 566                     if (sz < 32) // reserved  4 bytes + tag 2 bytes + size 2 bytes
 567                         break;   // m[a|c]time 24 bytes
 568                     int pos = off + 4;               // reserved 4 bytes
 569                     if (get16(extra, pos) !=  0x0001 || get16(extra, pos + 2) != 24)
 570                         break;
 571                     mtime = winTimeToFileTime(get64(extra, pos + 4));
 572                     atime = winTimeToFileTime(get64(extra, pos + 12));
 573                     ctime = winTimeToFileTime(get64(extra, pos + 20));
 574                     break;
 575                 case EXTID_EXTT:
 576                     int flag = Byte.toUnsignedInt(extra[off]);
 577                     int sz0 = 1;
 578                     // The CEN-header extra field contains the modification
 579                     // time only, or no timestamp at all. 'sz' is used to
 580                     // flag its presence or absence. But if mtime is present
 581                     // in LOC it must be present in CEN as well.
 582                     if ((flag & 0x1) != 0 && (sz0 + 4) <= sz) {
 583                         mtime = unixTimeToFileTime(get32S(extra, off + sz0));
 584                         sz0 += 4;
 585                     }
 586                     if ((flag & 0x2) != 0 && (sz0 + 4) <= sz) {
 587                         atime = unixTimeToFileTime(get32S(extra, off + sz0));
 588                         sz0 += 4;
 589                     }
 590                     if ((flag & 0x4) != 0 && (sz0 + 4) <= sz) {
 591                         ctime = unixTimeToFileTime(get32S(extra, off + sz0));
 592                         sz0 += 4;
 593                     }
 594                     break;
 595                  default:
 596                 }
 597                 off += sz;
 598             }
 599         }
 600         this.extra = extra;
 601     }
 602 
 603     /**
 604      * Returns the extra field data for the entry.
 605      *
 606      * @return the extra field data for the entry, or null if none
 607      *
 608      * @see #setExtra(byte[])
 609      */
 610     public byte[] getExtra() {
 611         return extra;