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 55 long csize = -1; // compressed size of entry data 56 int method = -1; // compression method 57 int flag = 0; // general purpose flag 58 byte[] extra; // optional extra field data for entry 59 String comment; // optional comment string for entry 60 61 /** 62 * Compression method for uncompressed entries. 63 */ 64 public static final int STORED = 0; 65 66 /** 67 * Compression method for compressed (deflated) entries. 68 */ 69 public static final int DEFLATED = 8; 70 71 /** 72 * DOS time constant for representing timestamps before 1980. 73 */ 74 static final long DOSTIME_BEFORE_1980 = (1 << 21) | (1 << 16); 75 76 /** 77 * Approximately 128 years, in milliseconds (ignoring leap years etc). 78 * 79 * This establish an approximate high-bound value for DOS times in 114 * 115 * @param e 116 * A zip Entry object 117 * 118 * @throws NullPointerException if the entry object is null 119 */ 120 public ZipEntry(ZipEntry e) { 121 Objects.requireNonNull(e, "entry"); 122 name = e.name; 123 xdostime = e.xdostime; 124 mtime = e.mtime; 125 atime = e.atime; 126 ctime = e.ctime; 127 crc = e.crc; 128 size = e.size; 129 csize = e.csize; 130 method = e.method; 131 flag = e.flag; 132 extra = e.extra; 133 comment = e.comment; 134 } 135 136 /** 137 * Creates a new un-initialized zip entry 138 */ 139 ZipEntry() {} 140 141 /** 142 * Returns the name of the entry. 143 * @return the name of the entry 144 */ 145 public String getName() { 146 return name; 147 } 148 149 /** 150 * Sets the last modification time of the entry. 151 * 152 * <p> If the entry is output to a ZIP file or ZIP file formatted 153 * output stream the last modification time set by this method will 655 656 /** 657 * Returns the hash code value for this entry. 658 */ 659 public int hashCode() { 660 return name.hashCode(); 661 } 662 663 /** 664 * Returns a copy of this entry. 665 */ 666 public Object clone() { 667 try { 668 ZipEntry e = (ZipEntry)super.clone(); 669 e.extra = (extra == null) ? null : extra.clone(); 670 return e; 671 } catch (CloneNotSupportedException e) { 672 // This should never happen, since we are Cloneable 673 throw new InternalError(e); 674 } 675 } 676 } | 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 55 long csize = -1; // compressed size of entry data 56 int method = -1; // compression method 57 int flag = 0; // general purpose flag 58 byte[] extra; // optional extra field data for entry 59 String comment; // optional comment string for entry 60 ZipCryption zipCryption = null; // encryption / decryption engine 61 byte[] encryptionHeader; // ZIP encryption header 62 boolean passphraseValidated = false; // passphrase validation is not yet. 63 64 /** 65 * Compression method for uncompressed entries. 66 */ 67 public static final int STORED = 0; 68 69 /** 70 * Compression method for compressed (deflated) entries. 71 */ 72 public static final int DEFLATED = 8; 73 74 /** 75 * DOS time constant for representing timestamps before 1980. 76 */ 77 static final long DOSTIME_BEFORE_1980 = (1 << 21) | (1 << 16); 78 79 /** 80 * Approximately 128 years, in milliseconds (ignoring leap years etc). 81 * 82 * This establish an approximate high-bound value for DOS times in 117 * 118 * @param e 119 * A zip Entry object 120 * 121 * @throws NullPointerException if the entry object is null 122 */ 123 public ZipEntry(ZipEntry e) { 124 Objects.requireNonNull(e, "entry"); 125 name = e.name; 126 xdostime = e.xdostime; 127 mtime = e.mtime; 128 atime = e.atime; 129 ctime = e.ctime; 130 crc = e.crc; 131 size = e.size; 132 csize = e.csize; 133 method = e.method; 134 flag = e.flag; 135 extra = e.extra; 136 comment = e.comment; 137 zipCryption = e.zipCryption; 138 encryptionHeader = e.encryptionHeader; 139 } 140 141 /** 142 * Creates a new un-initialized zip entry 143 */ 144 ZipEntry() {} 145 146 /** 147 * Returns the name of the entry. 148 * @return the name of the entry 149 */ 150 public String getName() { 151 return name; 152 } 153 154 /** 155 * Sets the last modification time of the entry. 156 * 157 * <p> If the entry is output to a ZIP file or ZIP file formatted 158 * output stream the last modification time set by this method will 660 661 /** 662 * Returns the hash code value for this entry. 663 */ 664 public int hashCode() { 665 return name.hashCode(); 666 } 667 668 /** 669 * Returns a copy of this entry. 670 */ 671 public Object clone() { 672 try { 673 ZipEntry e = (ZipEntry)super.clone(); 674 e.extra = (extra == null) ? null : extra.clone(); 675 return e; 676 } catch (CloneNotSupportedException e) { 677 // This should never happen, since we are Cloneable 678 throw new InternalError(e); 679 } 680 } 681 682 /** 683 * Set passphrase for ZIP encryption and decryption. 684 * @param passphrase Passphrase for encryption and decryption. 685 */ 686 public void setPassphrase(String passphrase) { 687 zipCryption = new TraditionalZipCryption(passphrase); 688 } 689 690 ZipCryption getZipCryption() { 691 return zipCryption; 692 } 693 694 /** 695 * Get the state whether encrypted entry or not. 696 * @return true if passphrase is required. 697 */ 698 public boolean isPassphraseRequired() { 699 return (flag & 1) == 1; 700 } 701 702 /** 703 * Validate passphrase for this entry. 704 * @return true if passphrase is valid, or this entry is not encrypted. 705 * @throws ZipException Passphrase is not set or Encryption header is not set. 706 */ 707 public boolean isValidPassphrase() throws ZipException{ 708 if (!isPassphraseRequired()) { 709 return true; 710 } 711 712 if (zipCryption == null) { 713 throw new ZipException("Passphrase is not set."); 714 } 715 if (passphraseValidated) { 716 return true; 717 } 718 if (encryptionHeader == null) { 719 throw new ZipException("Encryption header is not set."); 720 } 721 722 zipCryption.reset(); 723 byte[] tmp = encryptionHeader.clone(); 724 zipCryption.decryptBytes(tmp); 725 passphraseValidated = zipCryption.isValid(this, tmp); 726 return passphraseValidated; 727 } 728 } |