X5455_ExtendedTimestamp.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.archivers.zip;

  20. import java.io.Serializable;
  21. import java.nio.file.attribute.FileTime;
  22. import java.util.Arrays;
  23. import java.util.Date;
  24. import java.util.Objects;
  25. import java.util.zip.ZipException;

  26. import org.apache.commons.compress.utils.TimeUtils;
  27. import org.apache.commons.io.file.attribute.FileTimes;

  28. /**
  29.  * <p>
  30.  * An extra field that stores additional file and directory timestamp data for ZIP entries. Each ZIP entry can include up to three timestamps (modify, access,
  31.  * create*). The timestamps are stored as 32 bit signed integers representing seconds since UNIX epoch (Jan 1st, 1970, UTC). This field improves on ZIP's
  32.  * default timestamp granularity, since it allows one to store additional timestamps, and, in addition, the timestamps are stored using per-second granularity
  33.  * (zip's default behavior can only store timestamps to the nearest <em>even</em> second).
  34.  * </p>
  35.  * <p>
  36.  * Unfortunately, 32 (signed) bits can only store dates up to the year 2037, and so this extra field will eventually be obsolete. Enjoy it while it lasts!
  37.  * </p>
  38.  * <ul>
  39.  * <li><b>modifyTime:</b> most recent time of file/directory modification (or file/dir creation if the entry has not been modified since it was created).</li>
  40.  * <li><b>accessTime:</b> most recent time file/directory was opened (e.g., read from disk). Many people disable their operating systems from updating this
  41.  * value using the NOATIME mount option to optimize disk behavior, and thus it's not always reliable. In those cases it's always equal to modifyTime.</li>
  42.  * <li><b>*createTime:</b> modern Linux file systems (e.g., ext2 and newer) do not appear to store a value like this, and so it's usually omitted altogether in
  43.  * the ZIP extra field. Perhaps other UNIX systems track this.</li>
  44.  * </ul>
  45.  * <p>
  46.  * We're using the field definition given in Info-Zip's source archive: zip-3.0.tar.gz/proginfo/extrafld.txt
  47.  * </p>
  48.  *
  49.  * <pre>
  50.  * Value         Size        Description
  51.  * -----         ----        -----------
  52.  * 0x5455        Short       tag for this extra block type ("UT")
  53.  * TSize         Short       total data size for this block
  54.  * Flags         Byte        info bits
  55.  * (ModTime)     Long        time of last modification (UTC/GMT)
  56.  * (AcTime)      Long        time of last access (UTC/GMT)
  57.  * (CrTime)      Long        time of original creation (UTC/GMT)
  58.  *
  59.  * Central-header version:
  60.  *
  61.  * Value         Size        Description
  62.  * -----         ----        -----------
  63.  * 0x5455        Short       tag for this extra block type ("UT")
  64.  * TSize         Short       total data size for this block
  65.  * Flags         Byte        info bits (refers to local header!)
  66.  * (ModTime)     Long        time of last modification (UTC/GMT)
  67.  * </pre>
  68.  *
  69.  * @since 1.5
  70.  */
  71. public class X5455_ExtendedTimestamp implements ZipExtraField, Cloneable, Serializable {
  72.     private static final long serialVersionUID = 1L;

  73.     /**
  74.      * The header ID for this extra field.
  75.      *
  76.      * @since 1.23
  77.      */
  78.     public static final ZipShort HEADER_ID = new ZipShort(0x5455);

  79.     /**
  80.      * The bit set inside the flags by when the last modification time is present in this extra field.
  81.      */
  82.     public static final byte MODIFY_TIME_BIT = 1;
  83.     /**
  84.      * The bit set inside the flags by when the lasr access time is present in this extra field.
  85.      */
  86.     public static final byte ACCESS_TIME_BIT = 2;
  87.     /**
  88.      * The bit set inside the flags by when the original creation time is present in this extra field.
  89.      */
  90.     public static final byte CREATE_TIME_BIT = 4;

  91.     /**
  92.      * Utility method converts java.util.Date (milliseconds since epoch) into a ZipLong (seconds since epoch).
  93.      * <p/>
  94.      * Also makes sure the converted ZipLong is not too big to fit in 32 unsigned bits.
  95.      *
  96.      * @param d java.util.Date to convert to ZipLong
  97.      * @return ZipLong
  98.      */
  99.     private static ZipLong dateToZipLong(final Date d) {
  100.         if (d == null) {
  101.             return null;
  102.         }
  103.         return unixTimeToZipLong(d.getTime() / 1000);
  104.     }

  105.     /**
  106.      * Utility method converts {@link FileTime} into a ZipLong (seconds since epoch).
  107.      * <p/>
  108.      * Also makes sure the converted ZipLong is not too big to fit in 32 unsigned bits.
  109.      *
  110.      * @param time {@link FileTime} to convert to ZipLong
  111.      * @return ZipLong
  112.      */
  113.     private static ZipLong fileTimeToZipLong(final FileTime time) {
  114.         return time == null ? null : unixTimeToZipLong(TimeUtils.toUnixTime(time));
  115.     }

  116.     private static FileTime unixTimeToFileTime(final ZipLong unixTime) {
  117.         return unixTime != null ? FileTimes.fromUnixTime(unixTime.getIntValue()) : null;
  118.     }
  119.     // The 3 boolean fields (below) come from this flag's byte. The remaining 5 bits
  120.     // are ignored according to the current version of the spec (December 2012).

  121.     private static ZipLong unixTimeToZipLong(final long unixTime) {
  122.         if (!FileTimes.isUnixTime(unixTime)) {
  123.             throw new IllegalArgumentException("X5455 timestamps must fit in a signed 32 bit integer: " + unixTime);
  124.         }
  125.         return new ZipLong(unixTime);
  126.     }

  127.     private static Date zipLongToDate(final ZipLong unixTime) {
  128.         return unixTime != null ? new Date(unixTime.getIntValue() * 1000L) : null;
  129.     }

  130.     private byte flags;
  131.     // Note: even if bit1 and bit2 are set, the Central data will still not contain
  132.     // access/create fields: only local data ever holds those! This causes
  133.     // some of our implementation to look a little odd, with seemingly spurious
  134.     // != null and length checks.
  135.     private boolean bit0_modifyTimePresent;
  136.     private boolean bit1_accessTimePresent;

  137.     private boolean bit2_createTimePresent;

  138.     private ZipLong modifyTime;

  139.     private ZipLong accessTime;

  140.     private ZipLong createTime;

  141.     /**
  142.      * Constructor for X5455_ExtendedTimestamp.
  143.      */
  144.     public X5455_ExtendedTimestamp() {
  145.     }

  146.     @Override
  147.     public Object clone() throws CloneNotSupportedException {
  148.         return super.clone();
  149.     }

  150.     @Override
  151.     public boolean equals(final Object o) {
  152.         if (o instanceof X5455_ExtendedTimestamp) {
  153.             final X5455_ExtendedTimestamp xf = (X5455_ExtendedTimestamp) o;

  154.             // The ZipLong==ZipLong clauses handle the cases where both are null.
  155.             // and only last 3 bits of flags matter.
  156.             return (flags & 0x07) == (xf.flags & 0x07) && Objects.equals(modifyTime, xf.modifyTime) && Objects.equals(accessTime, xf.accessTime)
  157.                     && Objects.equals(createTime, xf.createTime);
  158.         }
  159.         return false;
  160.     }

  161.     /**
  162.      * Gets the access time as a {@link FileTime} of this ZIP entry, or null if no such timestamp exists in the ZIP entry. The milliseconds are always zeroed
  163.      * out, since the underlying data offers only per-second precision.
  164.      *
  165.      * @return modify time as {@link FileTime} or null.
  166.      * @since 1.23
  167.      */
  168.     public FileTime getAccessFileTime() {
  169.         return unixTimeToFileTime(accessTime);
  170.     }

  171.     /**
  172.      * Gets the access time as a java.util.Date of this ZIP entry, or null if no such timestamp exists in the ZIP entry. The milliseconds are always zeroed out,
  173.      * since the underlying data offers only per-second precision.
  174.      *
  175.      * @return access time as java.util.Date or null.
  176.      */
  177.     public Date getAccessJavaTime() {
  178.         return zipLongToDate(accessTime);
  179.     }

  180.     /**
  181.      * Gets the access time (seconds since epoch) of this ZIP entry as a ZipLong object, or null if no such timestamp exists in the ZIP entry.
  182.      *
  183.      * @return access time (seconds since epoch) or null.
  184.      */
  185.     public ZipLong getAccessTime() {
  186.         return accessTime;
  187.     }

  188.     /**
  189.      * Gets the actual data to put into central directory data - without Header-ID or length specifier.
  190.      *
  191.      * @return the central directory data
  192.      */
  193.     @Override
  194.     public byte[] getCentralDirectoryData() {
  195.         // Truncate out create & access time (last 8 bytes) from
  196.         // the copy of the local data we obtained:
  197.         return Arrays.copyOf(getLocalFileDataData(), getCentralDirectoryLength().getValue());
  198.     }

  199.     /**
  200.      * Gets the length of the extra field in the local file data - without Header-ID or length specifier.
  201.      *
  202.      * <p>
  203.      * For X5455 the central length is often smaller than the local length, because central cannot contain access or create timestamps.
  204.      * </p>
  205.      *
  206.      * @return a {@code ZipShort} for the length of the data of this extra field
  207.      */
  208.     @Override
  209.     public ZipShort getCentralDirectoryLength() {
  210.         return new ZipShort(1 + (bit0_modifyTimePresent ? 4 : 0));
  211.     }

  212.     /**
  213.      * Gets the create time as a {@link FileTime} of this ZIP entry, or null if no such timestamp exists in the ZIP entry. The milliseconds are always zeroed
  214.      * out, since the underlying data offers only per-second precision.
  215.      *
  216.      * @return modify time as {@link FileTime} or null.
  217.      * @since 1.23
  218.      */
  219.     public FileTime getCreateFileTime() {
  220.         return unixTimeToFileTime(createTime);
  221.     }

  222.     /**
  223.      * <p>
  224.      * Gets the create time as a java.util.Date of this ZIP entry, or null if no such timestamp exists in the ZIP entry. The milliseconds are always zeroed out,
  225.      * since the underlying data offers only per-second precision.
  226.      * </p>
  227.      * <p>
  228.      * Note: modern Linux file systems (e.g., ext2) do not appear to store a "create time" value, and so it's usually omitted altogether in the ZIP extra field.
  229.      * Perhaps other UNIX systems track this.
  230.      * </p>
  231.      *
  232.      * @return create time as java.util.Date or null.
  233.      */
  234.     public Date getCreateJavaTime() {
  235.         return zipLongToDate(createTime);
  236.     }

  237.     /**
  238.      * <p>
  239.      * Gets the create time (seconds since epoch) of this ZIP entry as a ZipLong object, or null if no such timestamp exists in the ZIP entry.
  240.      * </p>
  241.      * <p>
  242.      * Note: modern Linux file systems (e.g., ext2) do not appear to store a "create time" value, and so it's usually omitted altogether in the ZIP extra field.
  243.      * Perhaps other UNIX systems track this.
  244.      * </p>
  245.      *
  246.      * @return create time (seconds since epoch) or null.
  247.      */
  248.     public ZipLong getCreateTime() {
  249.         return createTime;
  250.     }

  251.     /**
  252.      * Gets flags byte. The flags byte tells us which of the three datestamp fields are present in the data:
  253.      *
  254.      * <pre>
  255.      * bit0 - modify time
  256.      * bit1 - access time
  257.      * bit2 - create time
  258.      * </pre>
  259.      *
  260.      * Only first 3 bits of flags are used according to the latest version of the spec (December 2012).
  261.      *
  262.      * @return flags byte indicating which of the three datestamp fields are present.
  263.      */
  264.     public byte getFlags() {
  265.         return flags;
  266.     }

  267.     /**
  268.      * Gets the Header-ID.
  269.      *
  270.      * @return the value for the header id for this extrafield
  271.      */
  272.     @Override
  273.     public ZipShort getHeaderId() {
  274.         return HEADER_ID;
  275.     }

  276.     /**
  277.      * Gets the actual data to put into local file data - without Header-ID or length specifier.
  278.      *
  279.      * @return get the data
  280.      */
  281.     @Override
  282.     public byte[] getLocalFileDataData() {
  283.         final byte[] data = new byte[getLocalFileDataLength().getValue()];
  284.         int pos = 0;
  285.         data[pos++] = 0;
  286.         if (bit0_modifyTimePresent) {
  287.             data[0] |= MODIFY_TIME_BIT;
  288.             System.arraycopy(modifyTime.getBytes(), 0, data, pos, 4);
  289.             pos += 4;
  290.         }
  291.         if (bit1_accessTimePresent && accessTime != null) {
  292.             data[0] |= ACCESS_TIME_BIT;
  293.             System.arraycopy(accessTime.getBytes(), 0, data, pos, 4);
  294.             pos += 4;
  295.         }
  296.         if (bit2_createTimePresent && createTime != null) {
  297.             data[0] |= CREATE_TIME_BIT;
  298.             System.arraycopy(createTime.getBytes(), 0, data, pos, 4);
  299.             pos += 4; // NOSONAR - assignment as documentation
  300.         }
  301.         return data;
  302.     }

  303.     /**
  304.      * Gets the length of the extra field in the local file data - without Header-ID or length specifier.
  305.      *
  306.      * @return a {@code ZipShort} for the length of the data of this extra field
  307.      */
  308.     @Override
  309.     public ZipShort getLocalFileDataLength() {
  310.         return new ZipShort(1 + (bit0_modifyTimePresent ? 4 : 0) + (bit1_accessTimePresent && accessTime != null ? 4 : 0)
  311.                 + (bit2_createTimePresent && createTime != null ? 4 : 0));
  312.     }

  313.     /**
  314.      * Gets the modify time as a {@link FileTime} of this ZIP entry, or null if no such timestamp exists in the ZIP entry. The milliseconds are always zeroed
  315.      * out, since the underlying data offers only per-second precision.
  316.      *
  317.      * @return modify time as {@link FileTime} or null.
  318.      * @since 1.23
  319.      */
  320.     public FileTime getModifyFileTime() {
  321.         return unixTimeToFileTime(modifyTime);
  322.     }

  323.     /**
  324.      * Gets the modify time as a java.util.Date of this ZIP entry, or null if no such timestamp exists in the ZIP entry. The milliseconds are always zeroed out,
  325.      * since the underlying data offers only per-second precision.
  326.      *
  327.      * @return modify time as java.util.Date or null.
  328.      */
  329.     public Date getModifyJavaTime() {
  330.         return zipLongToDate(modifyTime);
  331.     }

  332.     /**
  333.      * Gets the modify time (seconds since epoch) of this ZIP entry as a ZipLong object, or null if no such timestamp exists in the ZIP entry.
  334.      *
  335.      * @return modify time (seconds since epoch) or null.
  336.      */
  337.     public ZipLong getModifyTime() {
  338.         return modifyTime;
  339.     }

  340.     @Override
  341.     public int hashCode() {
  342.         int hc = -123 * (flags & 0x07); // only last 3 bits of flags matter
  343.         if (modifyTime != null) {
  344.             hc ^= modifyTime.hashCode();
  345.         }
  346.         if (accessTime != null) {
  347.             // Since accessTime is often same as modifyTime,
  348.             // this prevents them from XOR negating each other.
  349.             hc ^= Integer.rotateLeft(accessTime.hashCode(), 11);
  350.         }
  351.         if (createTime != null) {
  352.             hc ^= Integer.rotateLeft(createTime.hashCode(), 22);
  353.         }
  354.         return hc;
  355.     }

  356.     /**
  357.      * Tests whether bit0 of the flags byte is set or not, which should correspond to the presence or absence of a modify timestamp in this particular ZIP
  358.      * entry.
  359.      *
  360.      * @return true if bit0 of the flags byte is set.
  361.      */
  362.     public boolean isBit0_modifyTimePresent() {
  363.         return bit0_modifyTimePresent;
  364.     }

  365.     /**
  366.      * Tests whether bit1 of the flags byte is set or not, which should correspond to the presence or absence of a "last access" timestamp in this particular
  367.      * ZIP entry.
  368.      *
  369.      * @return true if bit1 of the flags byte is set.
  370.      */
  371.     public boolean isBit1_accessTimePresent() {
  372.         return bit1_accessTimePresent;
  373.     }

  374.     /**
  375.      * Tests whether bit2 of the flags byte is set or not, which should correspond to the presence or absence of a create timestamp in this particular ZIP
  376.      * entry.
  377.      *
  378.      * @return true if bit2 of the flags byte is set.
  379.      */
  380.     public boolean isBit2_createTimePresent() {
  381.         return bit2_createTimePresent;
  382.     }

  383.     /**
  384.      * Doesn't do anything special since this class always uses the same parsing logic for both central directory and local file data.
  385.      */
  386.     @Override
  387.     public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) throws ZipException {
  388.         reset();
  389.         parseFromLocalFileData(buffer, offset, length);
  390.     }

  391.     /**
  392.      * Populate data from this array as if it was in local file data.
  393.      *
  394.      * @param data   an array of bytes
  395.      * @param offset the start offset
  396.      * @param length the number of bytes in the array from offset
  397.      * @throws java.util.zip.ZipException on error
  398.      */
  399.     @Override
  400.     public void parseFromLocalFileData(final byte[] data, int offset, final int length) throws ZipException {
  401.         reset();
  402.         if (length < 1) {
  403.             throw new ZipException("X5455_ExtendedTimestamp too short, only " + length + " bytes");
  404.         }
  405.         final int len = offset + length;
  406.         setFlags(data[offset++]);
  407.         if (bit0_modifyTimePresent && offset + 4 <= len) {
  408.             modifyTime = new ZipLong(data, offset);
  409.             offset += 4;
  410.         } else {
  411.             bit0_modifyTimePresent = false;
  412.         }
  413.         if (bit1_accessTimePresent && offset + 4 <= len) {
  414.             accessTime = new ZipLong(data, offset);
  415.             offset += 4;
  416.         } else {
  417.             bit1_accessTimePresent = false;
  418.         }
  419.         if (bit2_createTimePresent && offset + 4 <= len) {
  420.             createTime = new ZipLong(data, offset);
  421.             offset += 4; // NOSONAR - assignment as documentation
  422.         } else {
  423.             bit2_createTimePresent = false;
  424.         }
  425.     }

  426.     /**
  427.      * Reset state back to newly constructed state. Helps us make sure parse() calls always generate clean results.
  428.      */
  429.     private void reset() {
  430.         setFlags((byte) 0);
  431.         this.modifyTime = null;
  432.         this.accessTime = null;
  433.         this.createTime = null;
  434.     }

  435.     /**
  436.      * <p>
  437.      * Sets the acccess time as a {@link FileTime} of this ZIP entry. Supplied value is truncated to per-second precision (milliseconds zeroed-out).
  438.      * </p>
  439.      * <p>
  440.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  441.      * flags is also set.
  442.      * </p>
  443.      *
  444.      * @param time access time as {@link FileTime}
  445.      * @since 1.23
  446.      */
  447.     public void setAccessFileTime(final FileTime time) {
  448.         setAccessTime(fileTimeToZipLong(time));
  449.     }

  450.     /**
  451.      * <p>
  452.      * Sets the access time as a java.util.Date of this ZIP entry. Supplied value is truncated to per-second precision (milliseconds zeroed-out).
  453.      * </p>
  454.      * <p>
  455.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  456.      * flags is also set.
  457.      * </p>
  458.      *
  459.      * @param d access time as java.util.Date
  460.      */
  461.     public void setAccessJavaTime(final Date d) {
  462.         setAccessTime(dateToZipLong(d));
  463.     }

  464.     /**
  465.      * <p>
  466.      * Sets the access time (seconds since epoch) of this ZIP entry using a ZipLong object
  467.      * </p>
  468.      * <p>
  469.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  470.      * flags is also set.
  471.      * </p>
  472.      *
  473.      * @param l ZipLong of the access time (seconds per epoch)
  474.      */
  475.     public void setAccessTime(final ZipLong l) {
  476.         bit1_accessTimePresent = l != null;
  477.         flags = (byte) (l != null ? flags | ACCESS_TIME_BIT : flags & ~ACCESS_TIME_BIT);
  478.         this.accessTime = l;
  479.     }

  480.     /**
  481.      * <p>
  482.      * Sets the create time as a {@link FileTime} of this ZIP entry. Supplied value is truncated to per-second precision (milliseconds zeroed-out).
  483.      * </p>
  484.      * <p>
  485.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  486.      * flags is also set.
  487.      * </p>
  488.      *
  489.      * @param time create time as {@link FileTime}
  490.      * @since 1.23
  491.      */
  492.     public void setCreateFileTime(final FileTime time) {
  493.         setCreateTime(fileTimeToZipLong(time));
  494.     }

  495.     /**
  496.      * <p>
  497.      * Sets the create time as a java.util.Date of this ZIP entry. Supplied value is truncated to per-second precision (milliseconds zeroed-out).
  498.      * </p>
  499.      * <p>
  500.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  501.      * flags is also set.
  502.      * </p>
  503.      *
  504.      * @param d create time as java.util.Date
  505.      */
  506.     public void setCreateJavaTime(final Date d) {
  507.         setCreateTime(dateToZipLong(d));
  508.     }

  509.     /**
  510.      * <p>
  511.      * Sets the create time (seconds since epoch) of this ZIP entry using a ZipLong object
  512.      * </p>
  513.      * <p>
  514.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  515.      * flags is also set.
  516.      * </p>
  517.      *
  518.      * @param l ZipLong of the create time (seconds per epoch)
  519.      */
  520.     public void setCreateTime(final ZipLong l) {
  521.         bit2_createTimePresent = l != null;
  522.         flags = (byte) (l != null ? flags | CREATE_TIME_BIT : flags & ~CREATE_TIME_BIT);
  523.         this.createTime = l;
  524.     }

  525.     /**
  526.      * Sets flags byte. The flags byte tells us which of the three datestamp fields are present in the data:
  527.      *
  528.      * <pre>
  529.      * bit0 - modify time
  530.      * bit1 - access time
  531.      * bit2 - create time
  532.      * </pre>
  533.      *
  534.      * Only first 3 bits of flags are used according to the latest version of the spec (December 2012).
  535.      *
  536.      * @param flags flags byte indicating which of the three datestamp fields are present.
  537.      */
  538.     public void setFlags(final byte flags) {
  539.         this.flags = flags;
  540.         this.bit0_modifyTimePresent = (flags & MODIFY_TIME_BIT) == MODIFY_TIME_BIT;
  541.         this.bit1_accessTimePresent = (flags & ACCESS_TIME_BIT) == ACCESS_TIME_BIT;
  542.         this.bit2_createTimePresent = (flags & CREATE_TIME_BIT) == CREATE_TIME_BIT;
  543.     }

  544.     /**
  545.      * <p>
  546.      * Sets the modify time as a {@link FileTime} of this ZIP entry. Supplied value is truncated to per-second precision (milliseconds zeroed-out).
  547.      * </p>
  548.      * <p>
  549.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  550.      * flags is also set.
  551.      * </p>
  552.      *
  553.      * @param time modify time as {@link FileTime}
  554.      * @since 1.23
  555.      */
  556.     public void setModifyFileTime(final FileTime time) {
  557.         setModifyTime(fileTimeToZipLong(time));
  558.     }

  559.     /**
  560.      * <p>
  561.      * Sets the modify time as a java.util.Date of this ZIP entry. Supplied value is truncated to per-second precision (milliseconds zeroed-out).
  562.      * </p>
  563.      * <p>
  564.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  565.      * flags is also set.
  566.      * </p>
  567.      *
  568.      * @param d modify time as java.util.Date
  569.      */
  570.     public void setModifyJavaTime(final Date d) {
  571.         setModifyTime(dateToZipLong(d));
  572.     }

  573.     /**
  574.      * <p>
  575.      * Sets the modify time (seconds since epoch) of this ZIP entry using a ZipLong object.
  576.      * </p>
  577.      * <p>
  578.      * Note: the setters for flags and timestamps are decoupled. Even if the timestamp is not-null, it will only be written out if the corresponding bit in the
  579.      * flags is also set.
  580.      * </p>
  581.      *
  582.      * @param l ZipLong of the modify time (seconds per epoch)
  583.      */
  584.     public void setModifyTime(final ZipLong l) {
  585.         bit0_modifyTimePresent = l != null;
  586.         flags = (byte) (l != null ? flags | MODIFY_TIME_BIT : flags & ~MODIFY_TIME_BIT);
  587.         this.modifyTime = l;
  588.     }

  589.     /**
  590.      * Returns a String representation of this class useful for debugging purposes.
  591.      *
  592.      * @return A String representation of this class useful for debugging purposes.
  593.      */
  594.     @Override
  595.     public String toString() {
  596.         final StringBuilder buf = new StringBuilder();
  597.         buf.append("0x5455 Zip Extra Field: Flags=");
  598.         buf.append(Integer.toBinaryString(ZipUtil.unsignedIntToSignedByte(flags))).append(" ");
  599.         if (bit0_modifyTimePresent && modifyTime != null) {
  600.             final Date m = getModifyJavaTime();
  601.             buf.append(" Modify:[").append(m).append("] ");
  602.         }
  603.         if (bit1_accessTimePresent && accessTime != null) {
  604.             final Date a = getAccessJavaTime();
  605.             buf.append(" Access:[").append(a).append("] ");
  606.         }
  607.         if (bit2_createTimePresent && createTime != null) {
  608.             final Date c = getCreateJavaTime();
  609.             buf.append(" Create:[").append(c).append("] ");
  610.         }
  611.         return buf.toString();
  612.     }

  613. }