< prev index next >

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

Print this page
rev 11478 : 8073497: Lazy conversion of ZipEntry time
Reviewed-by: sherman, plevart
   1 /*
   2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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


  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 time = -1;     // last modification time


  45     FileTime mtime;     // last modification time, from extra field data
  46     FileTime atime;     // last access time, from extra field data
  47     FileTime ctime;     // creation time, from extra field data
  48     long crc = -1;      // crc-32 of entry data
  49     long size = -1;     // uncompressed size of entry data
  50     long csize = -1;    // compressed size of entry data
  51     int method = -1;    // compression method
  52     int flag = 0;       // general purpose flag
  53     byte[] extra;       // optional extra field data for entry
  54     String comment;     // optional comment string for entry
  55 
  56     /**
  57      * Compression method for uncompressed entries.
  58      */
  59     public static final int STORED = 0;
  60 
  61     /**
  62      * Compression method for compressed (deflated) entries.
  63      */
  64     public static final int DEFLATED = 8;
  65 
  66     /**






















  67      * Creates a new zip entry with the specified name.
  68      *
  69      * @param  name
  70      *         The entry name
  71      *
  72      * @throws NullPointerException if the entry name is null
  73      * @throws IllegalArgumentException if the entry name is longer than
  74      *         0xFFFF bytes
  75      */
  76     public ZipEntry(String name) {
  77         Objects.requireNonNull(name, "name");
  78         if (name.length() > 0xFFFF) {
  79             throw new IllegalArgumentException("entry name too long");
  80         }
  81         this.name = name;
  82     }
  83 
  84     /**
  85      * Creates a new zip entry with fields taken from the specified
  86      * zip entry.
  87      *
  88      * @param  e
  89      *         A zip Entry object
  90      *
  91      * @throws NullPointerException if the entry object is null
  92      */
  93     public ZipEntry(ZipEntry e) {
  94         Objects.requireNonNull(e, "entry");
  95         name = e.name;
  96         time = e.time;
  97         mtime = e.mtime;
  98         atime = e.atime;
  99         ctime = e.ctime;
 100         crc = e.crc;
 101         size = e.size;
 102         csize = e.csize;
 103         method = e.method;
 104         flag = e.flag;
 105         extra = e.extra;
 106         comment = e.comment;
 107     }
 108 
 109     /**
 110      * Creates a new un-initialized zip entry
 111      */
 112     ZipEntry() {}
 113 
 114     /**
 115      * Returns the name of the entry.
 116      * @return the name of the entry


 120     }
 121 
 122     /**
 123      * Sets the last modification time of the entry.
 124      *
 125      * <p> If the entry is output to a ZIP file or ZIP file formatted
 126      * output stream the last modification time set by this method will
 127      * be stored into the {@code date and time fields} of the zip file
 128      * entry and encoded in standard {@code MS-DOS date and time format}.
 129      * The {@link java.util.TimeZone#getDefault() default TimeZone} is
 130      * used to convert the epoch time to the MS-DOS data and time.
 131      *
 132      * @param  time
 133      *         The last modification time of the entry in milliseconds
 134      *         since the epoch
 135      *
 136      * @see #getTime()
 137      * @see #getLastModifiedTime()
 138      */
 139     public void setTime(long time) {
 140         this.time = time;



 141         this.mtime = null;



 142     }
 143 
 144     /**
 145      * Returns the last modification time of the entry.
 146      *
 147      * <p> If the entry is read from a ZIP file or ZIP file formatted
 148      * input stream, this is the last modification time from the {@code
 149      * date and time fields} of the zip file entry. The
 150      * {@link java.util.TimeZone#getDefault() default TimeZone} is used
 151      * to convert the standard MS-DOS formatted date and time to the
 152      * epoch time.
 153      *
 154      * @return  The last modification time of the entry in milliseconds
 155      *          since the epoch, or -1 if not specified
 156      *
 157      * @see #setTime(long)
 158      * @see #setLastModifiedTime(FileTime)
 159      */
 160     public long getTime() {
 161         return time;



 162     }
 163 
 164     /**
 165      * Sets the last modification time of the entry.
 166      *
 167      * <p> When output to a ZIP file or ZIP file formatted output stream
 168      * the last modification time set by this method will be stored into
 169      * zip file entry's {@code date and time fields} in {@code standard
 170      * MS-DOS date and time format}), and the extended timestamp fields
 171      * in {@code optional extra data} in UTC time.
 172      *
 173      * @param  time
 174      *         The last modification time of the entry
 175      * @return This zip entry
 176      *
 177      * @throws NullPointerException if the {@code time} is null
 178      *
 179      * @see #getLastModifiedTime()
 180      * @since 1.8
 181      */
 182     public ZipEntry setLastModifiedTime(FileTime time) {
 183         this.mtime = Objects.requireNonNull(time, "lastModifiedTime");
 184         this.time = time.to(TimeUnit.MILLISECONDS);
 185         return this;
 186     }
 187 
 188     /**
 189      * Returns the last modification time of the entry.
 190      *
 191      * <p> If the entry is read from a ZIP file or ZIP file formatted
 192      * input stream, this is the last modification time from the zip
 193      * file entry's {@code optional extra data} if the extended timestamp
 194      * fields are present. Otherwise the last modification time is read
 195      * from the entry's {@code date and time fields}, the {@link
 196      * java.util.TimeZone#getDefault() default TimeZone} is used to convert
 197      * the standard MS-DOS formatted date and time to the epoch time.
 198      *
 199      * @return The last modification time of the entry, null if not specified
 200      *
 201      * @see #setLastModifiedTime(FileTime)
 202      * @since 1.8
 203      */
 204     public FileTime getLastModifiedTime() {
 205         if (mtime != null)
 206             return mtime;
 207         if (time == -1)
 208             return null;
 209         return FileTime.from(time, TimeUnit.MILLISECONDS);
 210     }
 211 
 212     /**
 213      * Sets the last access time of the entry.
 214      *
 215      * <p> If set, the last access time will be stored into the extended
 216      * timestamp fields of entry's {@code optional extra data}, when output
 217      * to a ZIP file or ZIP file formatted stream.
 218      *
 219      * @param  time
 220      *         The last access time of the entry
 221      * @return This zip entry
 222      *
 223      * @throws NullPointerException if the {@code time} is null
 224      *
 225      * @see #getLastAccessTime()
 226      * @since 1.8
 227      */
 228     public ZipEntry setLastAccessTime(FileTime time) {
 229         this.atime = Objects.requireNonNull(time, "lastAccessTime");


   1 /*
   2  * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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


  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
  52     long csize = -1;    // compressed size of entry data
  53     int method = -1;    // compression method
  54     int flag = 0;       // general purpose flag
  55     byte[] extra;       // optional extra field data for entry
  56     String comment;     // optional comment string for entry
  57 
  58     /**
  59      * Compression method for uncompressed entries.
  60      */
  61     public static final int STORED = 0;
  62 
  63     /**
  64      * Compression method for compressed (deflated) entries.
  65      */
  66     public static final int DEFLATED = 8;
  67 
  68     /**
  69      * DOS time constant for representing timestamps before 1980.
  70      */
  71     static final long DOSTIME_BEFORE_1980 = (1 << 21) | (1 << 16);
  72 
  73     /**
  74      * Approximately 128 years, in milliseconds (ignoring leap years etc).
  75      *
  76      * This establish an approximate high-bound value for DOS times in
  77      * milliseconds since epoch, used to enable an efficient but
  78      * sufficient bounds check to avoid generating extended last modified
  79      * time entries.
  80      *
  81      * Calculating the exact number is locale dependent, would require loading
  82      * TimeZone data eagerly, and would make little practical sense. Since DOS
  83      * times theoretically go to 2107 - with compatibility not guaranteed
  84      * after 2099 - setting this to a time that is before but near 2099
  85      * should be sufficient.
  86      */
  87     private static final long UPPER_DOSTIME_BOUND =
  88             128L * 365 * 24 * 60 * 60 * 1000;
  89 
  90     /**
  91      * Creates a new zip entry with the specified name.
  92      *
  93      * @param  name
  94      *         The entry name
  95      *
  96      * @throws NullPointerException if the entry name is null
  97      * @throws IllegalArgumentException if the entry name is longer than
  98      *         0xFFFF bytes
  99      */
 100     public ZipEntry(String name) {
 101         Objects.requireNonNull(name, "name");
 102         if (name.length() > 0xFFFF) {
 103             throw new IllegalArgumentException("entry name too long");
 104         }
 105         this.name = name;
 106     }
 107 
 108     /**
 109      * Creates a new zip entry with fields taken from the specified
 110      * zip entry.
 111      *
 112      * @param  e
 113      *         A zip Entry object
 114      *
 115      * @throws NullPointerException if the entry object is null
 116      */
 117     public ZipEntry(ZipEntry e) {
 118         Objects.requireNonNull(e, "entry");
 119         name = e.name;
 120         xdostime = e.xdostime;
 121         mtime = e.mtime;
 122         atime = e.atime;
 123         ctime = e.ctime;
 124         crc = e.crc;
 125         size = e.size;
 126         csize = e.csize;
 127         method = e.method;
 128         flag = e.flag;
 129         extra = e.extra;
 130         comment = e.comment;
 131     }
 132 
 133     /**
 134      * Creates a new un-initialized zip entry
 135      */
 136     ZipEntry() {}
 137 
 138     /**
 139      * Returns the name of the entry.
 140      * @return the name of the entry


 144     }
 145 
 146     /**
 147      * Sets the last modification time of the entry.
 148      *
 149      * <p> If the entry is output to a ZIP file or ZIP file formatted
 150      * output stream the last modification time set by this method will
 151      * be stored into the {@code date and time fields} of the zip file
 152      * entry and encoded in standard {@code MS-DOS date and time format}.
 153      * The {@link java.util.TimeZone#getDefault() default TimeZone} is
 154      * used to convert the epoch time to the MS-DOS data and time.
 155      *
 156      * @param  time
 157      *         The last modification time of the entry in milliseconds
 158      *         since the epoch
 159      *
 160      * @see #getTime()
 161      * @see #getLastModifiedTime()
 162      */
 163     public void setTime(long time) {
 164         this.xdostime = javaToExtendedDosTime(time);
 165         // Avoid setting the mtime field if time is in the valid
 166         // range for a DOS time
 167         if (xdostime != DOSTIME_BEFORE_1980 && time <= UPPER_DOSTIME_BOUND) {
 168             this.mtime = null;
 169         } else {
 170             this.mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
 171         }
 172     }
 173 
 174     /**
 175      * Returns the last modification time of the entry.
 176      *
 177      * <p> If the entry is read from a ZIP file or ZIP file formatted
 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));
 218         return this;
 219     }
 220 
 221     /**
 222      * Returns the last modification time of the entry.
 223      *
 224      * <p> If the entry is read from a ZIP file or ZIP file formatted
 225      * input stream, this is the last modification time from the zip
 226      * file entry's {@code optional extra data} if the extended timestamp
 227      * fields are present. Otherwise the last modification time is read
 228      * from the entry's {@code date and time fields}, the {@link
 229      * java.util.TimeZone#getDefault() default TimeZone} is used to convert
 230      * the standard MS-DOS formatted date and time to the epoch time.
 231      *
 232      * @return The last modification time of the entry, null if not specified
 233      *
 234      * @see #setLastModifiedTime(FileTime)
 235      * @since 1.8
 236      */
 237     public FileTime getLastModifiedTime() {
 238         if (mtime != null)
 239             return mtime;
 240         if (xdostime == -1)
 241             return null;
 242         return FileTime.from(getTime(), TimeUnit.MILLISECONDS);
 243     }
 244 
 245     /**
 246      * Sets the last access time of the entry.
 247      *
 248      * <p> If set, the last access time will be stored into the extended
 249      * timestamp fields of entry's {@code optional extra data}, when output
 250      * to a ZIP file or ZIP file formatted stream.
 251      *
 252      * @param  time
 253      *         The last access time of the entry
 254      * @return This zip entry
 255      *
 256      * @throws NullPointerException if the {@code time} is null
 257      *
 258      * @see #getLastAccessTime()
 259      * @since 1.8
 260      */
 261     public ZipEntry setLastAccessTime(FileTime time) {
 262         this.atime = Objects.requireNonNull(time, "lastAccessTime");


< prev index next >