home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / zip / ZipEntry.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.0 KB  |  262 lines

  1. /*
  2.  * @(#)ZipEntry.java    1.22 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util.zip;
  16.  
  17. import java.util.Date;
  18.  
  19. /**
  20.  * This class is used to represent a ZIP file entry.
  21.  *
  22.  * @version    1.22, 03/18/98
  23.  * @author    David Connelly
  24.  */
  25. public
  26. class ZipEntry implements ZipConstants, Cloneable {
  27.     String name;    // entry name
  28.     long time = -1;    // modification time (in DOS time)
  29.     long crc = -1;    // crc-32 of entry data
  30.     long size = -1;    // uncompressed size of entry data
  31.     int method = -1;    // compression method
  32.     byte[] extra;    // optional extra field data for entry
  33.     String comment;    // optional comment string for entry
  34.     int flag;        // general purpose bit flag
  35.     int version;    // version of ZIP this entry was made by
  36.     long csize = -1;       // compressed size of entry data
  37.     long offset;    // offset of LOC header from beginning of ZIP file
  38.  
  39.     /**
  40.      * Compression method for uncompressed entries.
  41.      */
  42.     public static final int STORED = 0;
  43.  
  44.     /**
  45.      * Compression method for compressed (deflated) entries.
  46.      */
  47.     public static final int DEFLATED = 8;
  48.  
  49.     /**
  50.      * Creates a new ZIP file entry with the specified name.
  51.      * @param name the entry name
  52.      * @exception NullPointerException if the entry name is null
  53.      * @exception IllegalArgumentException if the entry name is longer than
  54.      *          0xFFFF bytes
  55.      */
  56.     public ZipEntry(String name) {
  57.     if (name == null) {
  58.         throw new NullPointerException();
  59.     }
  60.     if (name.length() > 0xFFFF) {
  61.         throw new IllegalArgumentException("entry name too long");
  62.     }
  63.     this.name = name;
  64.     }
  65.  
  66.     /**
  67.      * Returns the name of the entry.
  68.      */
  69.     public String getName() {
  70.     return name;
  71.     }
  72.  
  73.     /**
  74.      * Sets the modification time of the entry.
  75.      * @param time the entry modification time in number of milliseconds
  76.      *           since the epoch
  77.      */
  78.     public void setTime(long time) {
  79.     this.time = javaToDosTime(time);
  80.     }
  81.  
  82.     /**
  83.      * Returns the modification time of the entry, or -1 if not specified.
  84.      */
  85.     public long getTime() {
  86.     return dosToJavaTime(time);
  87.     }
  88.  
  89.     /**
  90.      * Sets the uncompressed size of the entry data.
  91.      * @param size the uncompressed size in bytes
  92.      * @exception IllegalArgumentException if the specified size is less
  93.      *          than 0 or greater than 0xFFFFFFFF bytes
  94.      */
  95.     public void setSize(long size) {
  96.     if (size < 0 || size > 0xFFFFFFFFL) {
  97.         throw new IllegalArgumentException("invalid entry size");
  98.     }
  99.     this.size = size;
  100.     }
  101.  
  102.     /**
  103.      * Returns the uncompressed size of the entry data, or -1 if not known.
  104.      */
  105.     public long getSize() {
  106.     return size;
  107.     }
  108.  
  109.     /**
  110.      * Sets the CRC-32 checksum of the uncompressed entry data.
  111.      * @param crc the CRC-32 value
  112.      * @exception IllegalArgumentException if the specified CRC-32 value is
  113.      *          less than 0 or greater than 0xFFFFFFFF
  114.      */
  115.     public void setCrc(long crc) {
  116.     if (crc < 0 || crc > 0xFFFFFFFFL) {
  117.         throw new IllegalArgumentException("invalid entry crc-32");
  118.     }
  119.     this.crc = crc;
  120.     }
  121.  
  122.     /**
  123.      * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
  124.      * not known.
  125.      */
  126.     public long getCrc() {
  127.     return crc;
  128.     }
  129.  
  130.     /**
  131.      * Sets the compression method for the entry.
  132.      * @param method the compression method, either STORED or DEFLATED
  133.      * @exception IllegalArgumentException if the specified compression
  134.      *          method is invalid
  135.      */
  136.     public void setMethod(int method) {
  137.     if (method != STORED && method != DEFLATED) {
  138.         throw new IllegalArgumentException("invalid compression method");
  139.     }
  140.     this.method = method;
  141.     }
  142.  
  143.     /**
  144.      * Returns the compression method of the entry, or -1 if not specified.
  145.      */
  146.     public int getMethod() {
  147.     return method;
  148.     }
  149.  
  150.     /**
  151.      * Sets the optional extra field data for the entry.
  152.      * @param extra the extra field data bytes
  153.      * @exception IllegalArgumentException if the length of the specified
  154.      *          extra field data is greater than 0xFFFFF bytes
  155.      */
  156.     public void setExtra(byte[] extra) {
  157.     if (extra != null && extra.length > 0xFFFF) {
  158.         throw new IllegalArgumentException("invalid extra field length");
  159.     }
  160.     this.extra = extra;
  161.     }
  162.  
  163.     /**
  164.      * Returns the extra field data for the entry, or null if none.
  165.      */
  166.     public byte[] getExtra() {
  167.     return extra;
  168.     }
  169.  
  170.     /**
  171.      * Sets the optional comment string for the entry.
  172.      * @param comment the comment string
  173.      * @exception IllegalArgumentException if the length of the specified
  174.      *          comment string is greater than 0xFFFF bytes
  175.      */
  176.     public void setComment(String comment) {
  177.     if (comment != null && comment.length() > 0xFFFF) {
  178.         throw new IllegalArgumentException("invalid entry comment length");
  179.     }
  180.     this.comment = comment;
  181.     }
  182.   
  183.     /**
  184.      * Returns the comment string for the entry, or null if none.
  185.      */
  186.     public String getComment() {
  187.     return comment;
  188.     }
  189.  
  190.     /**
  191.      * Returns the compressed size of the entry data, or -1 if not known.
  192.      * In the case of a stored entry, the compressed size will be the same
  193.      * as the uncompressed size of the entry.
  194.      */
  195.     public long getCompressedSize() {
  196.     return csize;
  197.     }
  198.  
  199.     /**
  200.      * Returns true if this is a directory entry. A directory entry is
  201.      * defined to be one whose name ends with a '/'.
  202.      */
  203.     public boolean isDirectory() {
  204.     return name.endsWith("/");
  205.     }
  206.  
  207.     /**
  208.      * Returns a string representation of the ZIP entry.
  209.      */
  210.     public String toString() {
  211.     return getName();
  212.     }
  213.  
  214.     /*
  215.      * Converts DOS time to Java time (number of milliseconds since epoch).
  216.      */
  217.     private static long dosToJavaTime(long dtime) {
  218.     Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
  219.               (int)(((dtime >> 21) & 0x0f) - 1),
  220.               (int)((dtime >> 16) & 0x1f),
  221.               (int)((dtime >> 11) & 0x1f),
  222.               (int)((dtime >> 5) & 0x3f),
  223.               (int)((dtime << 1) & 0x3e));
  224.     return d.getTime();
  225.     }
  226.  
  227.     /*
  228.      * Converts Java time to DOS time.
  229.      */
  230.     private static long javaToDosTime(long time) {
  231.     Date d = new Date(time);
  232.     int year = d.getYear() + 1900;
  233.     if (year < 1980) {
  234.         return (1 << 21) | (1 << 16);
  235.     }
  236.     return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
  237.                d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
  238.                d.getSeconds() >> 1;
  239.     }
  240.  
  241.     /**
  242.      * Returns the hash code value for this entry.
  243.      */
  244.     public int hashCode() {
  245.     return name.hashCode();
  246.     }
  247.  
  248.     /**
  249.      * Returns a copy of this entry.
  250.      */
  251.     public Object clone() {
  252.     try {
  253.         ZipEntry e = (ZipEntry)super.clone();
  254.         e.extra = (byte[])extra.clone();
  255.         return e;
  256.     } catch (CloneNotSupportedException e) {
  257.         // This should never happen, since we are Cloneable
  258.         throw new InternalError();
  259.     }
  260.     }
  261. }
  262.