home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / java / util / zip / ZipOutputStream.class (.txt) < prev   
Encoding:
Java Class File  |  1979-12-31  |  6.1 KB  |  370 lines

  1. package java.util.zip;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.Enumeration;
  6. import java.util.Hashtable;
  7. import java.util.Vector;
  8.  
  9. public class ZipOutputStream extends DeflaterOutputStream implements ZipConstants {
  10.    private ZipEntry entry;
  11.    private Vector entries = new Vector();
  12.    private Hashtable names = new Hashtable();
  13.    private CRC32 crc = new CRC32();
  14.    private long written;
  15.    private long locoff = 0L;
  16.    private String comment;
  17.    private int method = 8;
  18.    private boolean finished;
  19.    private boolean closed = false;
  20.    public static final int STORED = 0;
  21.    public static final int DEFLATED = 8;
  22.  
  23.    private void ensureOpen() throws IOException {
  24.       if (this.closed) {
  25.          throw new IOException("Stream closed");
  26.       }
  27.    }
  28.  
  29.    public ZipOutputStream(OutputStream var1) {
  30.       super(var1, new Deflater(-1, true));
  31.    }
  32.  
  33.    public void setComment(String var1) {
  34.       if (var1.length() > 65535) {
  35.          throw new IllegalArgumentException("invalid ZIP file comment");
  36.       } else {
  37.          this.comment = var1;
  38.       }
  39.    }
  40.  
  41.    public void setMethod(int var1) {
  42.       if (var1 != 8 && var1 != 0) {
  43.          throw new IllegalArgumentException("invalid compression method");
  44.       } else {
  45.          this.method = var1;
  46.       }
  47.    }
  48.  
  49.    public void setLevel(int var1) {
  50.       super.def.setLevel(var1);
  51.    }
  52.  
  53.    public void putNextEntry(ZipEntry var1) throws IOException {
  54.       this.ensureOpen();
  55.       if (this.entry != null) {
  56.          this.closeEntry();
  57.       }
  58.  
  59.       if (var1.time == -1L) {
  60.          var1.setTime(System.currentTimeMillis());
  61.       }
  62.  
  63.       if (var1.method == -1) {
  64.          var1.method = this.method;
  65.       }
  66.  
  67.       switch (var1.method) {
  68.          case 0:
  69.             if (var1.size == -1L) {
  70.                var1.size = var1.csize;
  71.             } else if (var1.csize == -1L) {
  72.                var1.csize = var1.size;
  73.             } else if (var1.size != var1.csize) {
  74.                throw new ZipException("STORED entry where compressed != uncompressed size");
  75.             }
  76.  
  77.             if (var1.size == -1L || var1.crc == -1L) {
  78.                throw new ZipException("STORED entry missing size, compressed size, or crc-32");
  79.             }
  80.  
  81.             var1.version = 10;
  82.             var1.flag = 0;
  83.             break;
  84.          case 8:
  85.             if (var1.size != -1L && var1.csize != -1L && var1.crc != -1L) {
  86.                if (var1.size == -1L || var1.csize == -1L || var1.crc == -1L) {
  87.                   throw new ZipException("DEFLATED entry missing size, compressed size, or crc-32");
  88.                }
  89.  
  90.                var1.flag = 0;
  91.             } else {
  92.                var1.flag = 8;
  93.             }
  94.  
  95.             var1.version = 20;
  96.             break;
  97.          default:
  98.             throw new ZipException("unsupported compression method");
  99.       }
  100.  
  101.       var1.offset = this.written;
  102.       this.writeLOC(var1);
  103.       if (this.names.put(var1.name, var1) != null) {
  104.          throw new ZipException("duplicate entry: " + var1.name);
  105.       } else {
  106.          this.entries.addElement(var1);
  107.          this.entry = var1;
  108.       }
  109.    }
  110.  
  111.    public void closeEntry() throws IOException {
  112.       this.ensureOpen();
  113.       ZipEntry var1 = this.entry;
  114.       if (var1 != null) {
  115.          switch (var1.method) {
  116.             case 0:
  117.                if (var1.size != this.written - this.locoff) {
  118.                   throw new ZipException("invalid entry size (expected " + var1.size + " but got " + (this.written - this.locoff) + " bytes)");
  119.                }
  120.  
  121.                if (var1.crc != this.crc.getValue()) {
  122.                   throw new ZipException("invalid entry crc-32 (expected 0x" + Long.toHexString(var1.crc) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
  123.                }
  124.                break;
  125.             case 8:
  126.                super.def.finish();
  127.  
  128.                while(!super.def.finished()) {
  129.                   ((DeflaterOutputStream)this).deflate();
  130.                }
  131.  
  132.                if ((var1.flag & 8) == 0) {
  133.                   if (var1.size != (long)super.def.getTotalIn()) {
  134.                      throw new ZipException("invalid entry size (expected " + var1.size + " but got " + super.def.getTotalIn() + " bytes)");
  135.                   }
  136.  
  137.                   if (var1.csize != (long)super.def.getTotalOut()) {
  138.                      throw new ZipException("invalid entry compressed size (expected " + var1.csize + " but got " + super.def.getTotalOut() + " bytes)");
  139.                   }
  140.  
  141.                   if (var1.crc != this.crc.getValue()) {
  142.                      throw new ZipException("invalid entry CRC-32 (expected 0x" + Long.toHexString(var1.crc) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
  143.                   }
  144.                } else {
  145.                   var1.size = (long)super.def.getTotalIn();
  146.                   var1.csize = (long)super.def.getTotalOut();
  147.                   var1.crc = this.crc.getValue();
  148.                   this.writeEXT(var1);
  149.                }
  150.  
  151.                super.def.reset();
  152.                this.written += var1.csize;
  153.                break;
  154.             default:
  155.                throw new InternalError("invalid compression method");
  156.          }
  157.  
  158.          this.crc.reset();
  159.          this.entry = null;
  160.       }
  161.  
  162.    }
  163.  
  164.    public synchronized void write(byte[] var1, int var2, int var3) throws IOException {
  165.       this.ensureOpen();
  166.       if (var2 >= 0 && var2 <= var1.length && var3 >= 0 && var2 + var3 <= var1.length && var2 + var3 >= 0) {
  167.          if (var3 != 0) {
  168.             if (this.entry == null) {
  169.                throw new ZipException("no current ZIP entry");
  170.             } else {
  171.                switch (this.entry.method) {
  172.                   case 0:
  173.                      this.written += (long)var3;
  174.                      if (this.written - this.locoff > this.entry.size) {
  175.                         throw new ZipException("attempt to write past end of STORED entry");
  176.                      }
  177.  
  178.                      super.out.write(var1, var2, var3);
  179.                      break;
  180.                   case 8:
  181.                      super.write(var1, var2, var3);
  182.                      break;
  183.                   default:
  184.                      throw new InternalError("invalid compression method");
  185.                }
  186.  
  187.                this.crc.update(var1, var2, var3);
  188.             }
  189.          }
  190.       } else {
  191.          throw new IndexOutOfBoundsException();
  192.       }
  193.    }
  194.  
  195.    public void finish() throws IOException {
  196.       this.ensureOpen();
  197.       if (!this.finished) {
  198.          if (this.entry != null) {
  199.             this.closeEntry();
  200.          }
  201.  
  202.          if (this.entries.size() < 1) {
  203.             throw new ZipException("ZIP file must have at least one entry");
  204.          } else {
  205.             long var1 = this.written;
  206.             Enumeration var3 = this.entries.elements();
  207.  
  208.             while(var3.hasMoreElements()) {
  209.                this.writeCEN((ZipEntry)var3.nextElement());
  210.             }
  211.  
  212.             this.writeEND(var1, this.written - var1);
  213.             this.finished = true;
  214.          }
  215.       }
  216.    }
  217.  
  218.    public void close() throws IOException {
  219.       this.finish();
  220.       super.out.close();
  221.       this.closed = true;
  222.    }
  223.  
  224.    private void writeLOC(ZipEntry var1) throws IOException {
  225.       this.writeInt(67324752L);
  226.       this.writeShort(var1.version);
  227.       this.writeShort(var1.flag);
  228.       this.writeShort(var1.method);
  229.       this.writeInt(var1.time);
  230.       if ((var1.flag & 8) == 8) {
  231.          this.writeInt(0L);
  232.          this.writeInt(0L);
  233.          this.writeInt(0L);
  234.       } else {
  235.          this.writeInt(var1.crc);
  236.          this.writeInt(var1.csize);
  237.          this.writeInt(var1.size);
  238.       }
  239.  
  240.       byte[] var2 = getUTF8Bytes(var1.name);
  241.       this.writeShort(var2.length);
  242.       this.writeShort(var1.extra != null ? var1.extra.length : 0);
  243.       this.writeBytes(var2, 0, var2.length);
  244.       if (var1.extra != null) {
  245.          this.writeBytes(var1.extra, 0, var1.extra.length);
  246.       }
  247.  
  248.       this.locoff = this.written;
  249.    }
  250.  
  251.    private void writeEXT(ZipEntry var1) throws IOException {
  252.       this.writeInt(134695760L);
  253.       this.writeInt(var1.crc);
  254.       this.writeInt(var1.csize);
  255.       this.writeInt(var1.size);
  256.    }
  257.  
  258.    private void writeCEN(ZipEntry var1) throws IOException {
  259.       this.writeInt(33639248L);
  260.       this.writeShort(var1.version);
  261.       this.writeShort(var1.version);
  262.       this.writeShort(var1.flag);
  263.       this.writeShort(var1.method);
  264.       this.writeInt(var1.time);
  265.       this.writeInt(var1.crc);
  266.       this.writeInt(var1.csize);
  267.       this.writeInt(var1.size);
  268.       byte[] var2 = getUTF8Bytes(var1.name);
  269.       this.writeShort(var2.length);
  270.       this.writeShort(var1.extra != null ? var1.extra.length : 0);
  271.       byte[] var3;
  272.       if (var1.comment != null) {
  273.          var3 = getUTF8Bytes(var1.comment);
  274.          this.writeShort(var3.length);
  275.       } else {
  276.          var3 = null;
  277.          this.writeShort(0);
  278.       }
  279.  
  280.       this.writeShort(0);
  281.       this.writeShort(0);
  282.       this.writeInt(0L);
  283.       this.writeInt(var1.offset);
  284.       this.writeBytes(var2, 0, var2.length);
  285.       if (var1.extra != null) {
  286.          this.writeBytes(var1.extra, 0, var1.extra.length);
  287.       }
  288.  
  289.       if (var3 != null) {
  290.          this.writeBytes(var3, 0, var3.length);
  291.       }
  292.  
  293.    }
  294.  
  295.    private void writeEND(long var1, long var3) throws IOException {
  296.       this.writeInt(101010256L);
  297.       this.writeShort(0);
  298.       this.writeShort(0);
  299.       this.writeShort(this.entries.size());
  300.       this.writeShort(this.entries.size());
  301.       this.writeInt(var3);
  302.       this.writeInt(var1);
  303.       if (this.comment != null) {
  304.          byte[] var5 = getUTF8Bytes(this.comment);
  305.          this.writeShort(var5.length);
  306.          this.writeBytes(var5, 0, var5.length);
  307.       } else {
  308.          this.writeShort(0);
  309.       }
  310.  
  311.    }
  312.  
  313.    private void writeShort(int var1) throws IOException {
  314.       OutputStream var2 = super.out;
  315.       var2.write(var1 >>> 0 & 255);
  316.       var2.write(var1 >>> 8 & 255);
  317.       this.written += 2L;
  318.    }
  319.  
  320.    private void writeInt(long var1) throws IOException {
  321.       OutputStream var3 = super.out;
  322.       var3.write((int)(var1 >>> 0 & 255L));
  323.       var3.write((int)(var1 >>> 8 & 255L));
  324.       var3.write((int)(var1 >>> 16 & 255L));
  325.       var3.write((int)(var1 >>> 24 & 255L));
  326.       this.written += 4L;
  327.    }
  328.  
  329.    private void writeBytes(byte[] var1, int var2, int var3) throws IOException {
  330.       super.out.write(var1, var2, var3);
  331.       this.written += (long)var3;
  332.    }
  333.  
  334.    private static byte[] getUTF8Bytes(String var0) {
  335.       char[] var1 = var0.toCharArray();
  336.       int var2 = var1.length;
  337.       int var3 = 0;
  338.  
  339.       for(int var4 = 0; var4 < var2; ++var4) {
  340.          char var5 = var1[var4];
  341.          if (var5 <= 127) {
  342.             ++var3;
  343.          } else if (var5 <= 2047) {
  344.             var3 += 2;
  345.          } else {
  346.             var3 += 3;
  347.          }
  348.       }
  349.  
  350.       byte[] var9 = new byte[var3];
  351.       int var6 = 0;
  352.  
  353.       for(int var7 = 0; var7 < var2; ++var7) {
  354.          char var8 = var1[var7];
  355.          if (var8 <= 127) {
  356.             var9[var6++] = (byte)var8;
  357.          } else if (var8 <= 2047) {
  358.             var9[var6++] = (byte)(var8 >> 6 | 192);
  359.             var9[var6++] = (byte)(var8 & 63 | 128);
  360.          } else {
  361.             var9[var6++] = (byte)(var8 >> 12 | 224);
  362.             var9[var6++] = (byte)(var8 >> 6 & 63 | 128);
  363.             var9[var6++] = (byte)(var8 & 63 | 128);
  364.          }
  365.       }
  366.  
  367.       return var9;
  368.    }
  369. }
  370.