1 /*
2 * Copyright (c) 1995, 2017, 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
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 * @since 1.1
43 */
44 public
45 class ZipEntry implements ZipConstants, Cloneable {
46
47 String name; // entry name
48 long xdostime = -1; // last modification time (in extended DOS time,
49 // where milliseconds lost in conversion might
50 // be encoded into the upper half)
51 FileTime mtime; // last modification time, from extra field data
52 FileTime atime; // last access time, from extra field data
53 FileTime ctime; // creation time, from extra field data
54 long crc = -1; // crc-32 of entry data
55 long size = -1; // uncompressed size of entry data
56 long csize = -1; // compressed size of entry data
57 int method = -1; // compression method
58 int flag = 0; // general purpose flag
59 byte[] extra; // optional extra field data for entry
60 String comment; // optional comment string for entry
61
62 /**
63 * Compression method for uncompressed entries.
64 */
65 public static final int STORED = 0;
66
67 /**
68 * Compression method for compressed (deflated) entries.
69 */
70 public static final int DEFLATED = 8;
71
72 /**
73 * DOS time constant for representing timestamps before 1980.
74 */
75 static final long DOSTIME_BEFORE_1980 = (1 << 21) | (1 << 16);
76
77 /**
78 * Approximately 128 years, in milliseconds (ignoring leap years etc).
636 }
637
638 /**
639 * Returns the comment string for the entry.
640 *
641 * @return the comment string for the entry, or null if none
642 *
643 * @see #setComment(String)
644 */
645 public String getComment() {
646 return comment;
647 }
648
649 /**
650 * Returns true if this is a directory entry. A directory entry is
651 * defined to be one whose name ends with a '/'.
652 * @return true if this is a directory entry
653 */
654 public boolean isDirectory() {
655 return name.endsWith("/");
656 }
657
658 /**
659 * Returns a string representation of the ZIP entry.
660 */
661 public String toString() {
662 return getName();
663 }
664
665 /**
666 * Returns the hash code value for this entry.
667 */
668 public int hashCode() {
669 return name.hashCode();
670 }
671
672 /**
673 * Returns a copy of this entry.
674 */
675 public Object clone() {
|
1 /*
2 * Copyright (c) 1995, 2018, 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
23 * questions.
24 */
25
26 package java.util.zip;
27
28 import static java.util.zip.ZipConstants64.*;
29 import static java.util.zip.ZipUtils.*;
30
31 import java.nio.file.attribute.FileTime;
32 import java.nio.file.attribute.PosixFilePermission;
33 import java.nio.file.attribute.PosixFilePermissions;
34 import java.time.LocalDateTime;
35 import java.time.ZoneId;
36 import java.time.ZonedDateTime;
37 import java.util.Objects;
38 import java.util.Optional;
39 import java.util.Set;
40 import java.util.concurrent.TimeUnit;
41
42 /**
43 * This class is used to represent a ZIP file entry.
44 *
45 * @author David Connelly
46 * @since 1.1
47 */
48 public
49 class ZipEntry implements ZipConstants, Cloneable {
50
51 String name; // entry name
52 long xdostime = -1; // last modification time (in extended DOS time,
53 // where milliseconds lost in conversion might
54 // be encoded into the upper half)
55 FileTime mtime; // last modification time, from extra field data
56 FileTime atime; // last access time, from extra field data
57 FileTime ctime; // creation time, from extra field data
58 long crc = -1; // crc-32 of entry data
59 long size = -1; // uncompressed size of entry data
60 long csize = -1; // compressed size of entry data
61 int method = -1; // compression method
62 int flag = 0; // general purpose flag
63 int posixPerms = -1; // posix permissions
64 byte[] extra; // optional extra field data for entry
65 String comment; // optional comment string for entry
66
67 /**
68 * Compression method for uncompressed entries.
69 */
70 public static final int STORED = 0;
71
72 /**
73 * Compression method for compressed (deflated) entries.
74 */
75 public static final int DEFLATED = 8;
76
77 /**
78 * DOS time constant for representing timestamps before 1980.
79 */
80 static final long DOSTIME_BEFORE_1980 = (1 << 21) | (1 << 16);
81
82 /**
83 * Approximately 128 years, in milliseconds (ignoring leap years etc).
641 }
642
643 /**
644 * Returns the comment string for the entry.
645 *
646 * @return the comment string for the entry, or null if none
647 *
648 * @see #setComment(String)
649 */
650 public String getComment() {
651 return comment;
652 }
653
654 /**
655 * Returns true if this is a directory entry. A directory entry is
656 * defined to be one whose name ends with a '/'.
657 * @return true if this is a directory entry
658 */
659 public boolean isDirectory() {
660 return name.endsWith("/");
661 }
662
663 /**
664 * Returns the set of Posix file permissions that are associated with
665 * this ZipEntry. This information might not be present all times,
666 * e.g. it is platform dependend. Also, when reading a zip file via
667 * {@link ZipInputStream}, the posix permissions will be null
668 * because data is stored in the CEN of the zip file which is not
669 * evaluated then. If you rely on Posix permissions, you need to access
670 * the zip file via {@link ZipFile}.
671 *
672 * @return The set of Posix File permissions, in case they are associated.
673 *
674 * @since 12
675 */
676 public Optional<Set<PosixFilePermission>> getPosixPermissions() {
677 if (posixPerms == -1) {
678 return Optional.empty();
679 }
680 return Optional.of(PosixFilePermissions.fromFlags(posixPerms));
681 }
682
683 /**
684 * Sets the set of Posix file permissions for this ZipEntry.
685 *
686 * @param permissions A set of PosixFilePermissions. If the value is null,
687 * no permission information will be stored in the zip
688 * file.
689 *
690 * @since 12
691 */
692 public void setPosixPermissions(Set<PosixFilePermission> permissions) {
693 if (permissions == null) {
694 posixPerms = -1;
695 return;
696 }
697 posixPerms = PosixFilePermissions.toFlags(permissions);
698 }
699
700 /**
701 * Returns a string representation of the ZIP entry.
702 */
703 public String toString() {
704 return getName();
705 }
706
707 /**
708 * Returns the hash code value for this entry.
709 */
710 public int hashCode() {
711 return name.hashCode();
712 }
713
714 /**
715 * Returns a copy of this entry.
716 */
717 public Object clone() {
|