home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / ZipEntry.java < prev    next >
Text File  |  1997-05-20  |  7KB  |  255 lines

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