home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / java / util / zip / ZipOutputStream.class (.txt) < prev   
Encoding:
Java Class File  |  1998-04-23  |  6.7 KB  |  309 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;
  16.    private String comment;
  17.    private int method = 8;
  18.    private boolean finished;
  19.    public static final int STORED = 0;
  20.    public static final int DEFLATED = 8;
  21.  
  22.    public ZipOutputStream(OutputStream var1) {
  23.       super(var1, new Deflater(-1, true));
  24.    }
  25.  
  26.    public void setComment(String var1) {
  27.       if (var1.length() > 65535) {
  28.          throw new IllegalArgumentException("invalid ZIP file comment");
  29.       } else {
  30.          this.comment = var1;
  31.       }
  32.    }
  33.  
  34.    public void setMethod(int var1) {
  35.       if (var1 != 8 && var1 != 0) {
  36.          throw new IllegalArgumentException("invalid compression method");
  37.       } else {
  38.          this.method = var1;
  39.       }
  40.    }
  41.  
  42.    public void setLevel(int var1) {
  43.       super.def.setLevel(var1);
  44.    }
  45.  
  46.    public void putNextEntry(ZipEntry var1) throws IOException {
  47.       if (this.entry != null) {
  48.          this.closeEntry();
  49.       }
  50.  
  51.       if (var1.time == -1L) {
  52.          var1.setTime(System.currentTimeMillis());
  53.       }
  54.  
  55.       if (var1.method == -1) {
  56.          var1.method = this.method;
  57.       }
  58.  
  59.       switch (var1.method) {
  60.          case 0:
  61.             if (var1.size == -1L) {
  62.                var1.size = var1.csize;
  63.             } else if (var1.csize == -1L) {
  64.                var1.csize = var1.size;
  65.             } else if (var1.size != var1.csize) {
  66.                throw new ZipException("STORED entry where compressed != uncompressed size");
  67.             }
  68.  
  69.             if (var1.size == -1L || var1.crc == -1L) {
  70.                throw new ZipException("STORED entry missing size, compressed size, or crc-32");
  71.             }
  72.  
  73.             var1.version = 10;
  74.             var1.flag = 0;
  75.             break;
  76.          case 8:
  77.             if (var1.size != -1L && var1.csize != -1L && var1.crc != -1L) {
  78.                if (var1.size == -1L || var1.csize == -1L || var1.crc == -1L) {
  79.                   throw new ZipException("DEFLATED entry missing size, compressed size, or crc-32");
  80.                }
  81.  
  82.                var1.flag = 0;
  83.             } else {
  84.                var1.flag = 8;
  85.             }
  86.  
  87.             var1.version = 20;
  88.             break;
  89.          default:
  90.             throw new ZipException("unsupported compression method");
  91.       }
  92.  
  93.       var1.offset = this.written;
  94.       this.writeLOC(var1);
  95.       if (this.names.put(var1.name, var1) != null) {
  96.          throw new ZipException("duplicate entry: " + var1.name);
  97.       } else {
  98.          this.entries.addElement(var1);
  99.          this.entry = var1;
  100.       }
  101.    }
  102.  
  103.    public void closeEntry() throws IOException {
  104.       ZipEntry var1 = this.entry;
  105.       if (var1 != null) {
  106.          switch (var1.method) {
  107.             case 0:
  108.                if (var1.size != this.written - this.locoff) {
  109.                   throw new ZipException("invalid entry size (expected " + var1.size + " but got " + (this.written - this.locoff) + " bytes)");
  110.                }
  111.  
  112.                if (var1.crc != this.crc.getValue()) {
  113.                   throw new ZipException("invalid entry crc-32 (expected 0x" + Long.toHexString(var1.size) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
  114.                }
  115.                break;
  116.             case 8:
  117.                super.def.finish();
  118.  
  119.                while(!super.def.finished()) {
  120.                   ((DeflaterOutputStream)this).deflate();
  121.                }
  122.  
  123.                if ((var1.flag & 8) == 0) {
  124.                   if (var1.size != (long)super.def.getTotalIn()) {
  125.                      throw new ZipException("invalid entry size (expected " + var1.size + " but got " + super.def.getTotalIn() + " bytes)");
  126.                   }
  127.  
  128.                   if (var1.csize != (long)super.def.getTotalOut()) {
  129.                      throw new ZipException("invalid entry compressed size (expected " + var1.csize + " but got " + super.def.getTotalOut() + " bytes)");
  130.                   }
  131.  
  132.                   if (var1.crc != this.crc.getValue()) {
  133.                      throw new ZipException("invalid entry CRC-32 (expected 0x" + Long.toHexString(var1.crc) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
  134.                   }
  135.                } else {
  136.                   var1.size = (long)super.def.getTotalIn();
  137.                   var1.csize = (long)super.def.getTotalOut();
  138.                   var1.crc = this.crc.getValue();
  139.                   this.writeEXT(var1);
  140.                }
  141.  
  142.                super.def.reset();
  143.                this.written += var1.csize;
  144.                break;
  145.             default:
  146.                throw new InternalError("invalid compression method");
  147.          }
  148.  
  149.          this.crc.reset();
  150.          this.entry = null;
  151.       }
  152.  
  153.    }
  154.  
  155.    public synchronized void write(byte[] var1, int var2, int var3) throws IOException {
  156.       if (this.entry == null) {
  157.          throw new ZipException("no current ZIP entry");
  158.       } else {
  159.          switch (this.entry.method) {
  160.             case 0:
  161.                this.written += (long)var3;
  162.                if (this.written - this.locoff > this.entry.size) {
  163.                   throw new ZipException("attempt to write past end of STORED entry");
  164.                }
  165.  
  166.                super.out.write(var1, var2, var3);
  167.                break;
  168.             case 8:
  169.                super.write(var1, var2, var3);
  170.                break;
  171.             default:
  172.                throw new InternalError("invalid compression method");
  173.          }
  174.  
  175.          this.crc.update(var1, var2, var3);
  176.       }
  177.    }
  178.  
  179.    public void finish() throws IOException {
  180.       if (!this.finished) {
  181.          if (this.entry != null) {
  182.             this.closeEntry();
  183.          }
  184.  
  185.          if (this.entries.size() < 1) {
  186.             throw new ZipException("ZIP file must have at least one entry");
  187.          } else {
  188.             long var1 = this.written;
  189.             Enumeration var3 = this.entries.elements();
  190.  
  191.             while(var3.hasMoreElements()) {
  192.                this.writeCEN((ZipEntry)var3.nextElement());
  193.             }
  194.  
  195.             this.writeEND(var1, this.written - var1);
  196.             this.finished = true;
  197.          }
  198.       }
  199.    }
  200.  
  201.    public void close() throws IOException {
  202.       this.finish();
  203.       super.out.close();
  204.    }
  205.  
  206.    private void writeLOC(ZipEntry var1) throws IOException {
  207.       this.writeInt(67324752L);
  208.       this.writeShort(var1.version);
  209.       this.writeShort(var1.flag);
  210.       this.writeShort(var1.method);
  211.       this.writeInt(var1.time);
  212.       if ((var1.flag & 8) == 8) {
  213.          this.writeInt(0L);
  214.          this.writeInt(0L);
  215.          this.writeInt(0L);
  216.       } else {
  217.          this.writeInt(var1.crc);
  218.          this.writeInt(var1.csize);
  219.          this.writeInt(var1.size);
  220.       }
  221.  
  222.       this.writeShort(var1.name.length());
  223.       this.writeShort(var1.extra != null ? var1.extra.length : 0);
  224.       this.writeAscii(var1.name);
  225.       if (var1.extra != null) {
  226.          this.writeBytes(var1.extra, 0, var1.extra.length);
  227.       }
  228.  
  229.       this.locoff = this.written;
  230.    }
  231.  
  232.    private void writeEXT(ZipEntry var1) throws IOException {
  233.       this.writeInt(134695760L);
  234.       this.writeInt(var1.crc);
  235.       this.writeInt(var1.csize);
  236.       this.writeInt(var1.size);
  237.    }
  238.  
  239.    private void writeCEN(ZipEntry var1) throws IOException {
  240.       this.writeInt(33639248L);
  241.       this.writeShort(var1.version);
  242.       this.writeShort(var1.version);
  243.       this.writeShort(var1.flag);
  244.       this.writeShort(var1.method);
  245.       this.writeInt(var1.time);
  246.       this.writeInt(var1.crc);
  247.       this.writeInt(var1.csize);
  248.       this.writeInt(var1.size);
  249.       this.writeShort(var1.name.length());
  250.       this.writeShort(var1.extra != null ? var1.extra.length : 0);
  251.       this.writeShort(var1.comment != null ? var1.comment.length() : 0);
  252.       this.writeShort(0);
  253.       this.writeShort(0);
  254.       this.writeInt(0L);
  255.       this.writeInt(var1.offset);
  256.       this.writeAscii(var1.name);
  257.       if (var1.extra != null) {
  258.          this.writeBytes(var1.extra, 0, var1.extra.length);
  259.       }
  260.  
  261.       if (var1.comment != null) {
  262.          this.writeAscii(var1.comment);
  263.       }
  264.  
  265.    }
  266.  
  267.    private void writeEND(long var1, long var3) throws IOException {
  268.       this.writeInt(101010256L);
  269.       this.writeShort(0);
  270.       this.writeShort(0);
  271.       this.writeShort(this.entries.size());
  272.       this.writeShort(this.entries.size());
  273.       this.writeInt(var3);
  274.       this.writeInt(var1);
  275.       this.writeShort(this.comment != null ? this.comment.length() : 0);
  276.       if (this.comment != null) {
  277.          this.writeAscii(this.comment);
  278.       }
  279.  
  280.    }
  281.  
  282.    private void writeShort(int var1) throws IOException {
  283.       OutputStream var2 = super.out;
  284.       var2.write(var1 & 255);
  285.       var2.write(var1 >>> 8 & 255);
  286.       this.written += 2L;
  287.    }
  288.  
  289.    private void writeInt(long var1) throws IOException {
  290.       OutputStream var3 = super.out;
  291.       var3.write((int)(var1 & 255L));
  292.       var3.write((int)(var1 >>> 8 & 255L));
  293.       var3.write((int)(var1 >>> 16 & 255L));
  294.       var3.write((int)(var1 >>> 24 & 255L));
  295.       this.written += 4L;
  296.    }
  297.  
  298.    private void writeAscii(String var1) throws IOException {
  299.       byte[] var2 = new byte[var1.length()];
  300.       var1.getBytes(0, var2.length, var2, 0);
  301.       this.writeBytes(var2, 0, var2.length);
  302.    }
  303.  
  304.    private void writeBytes(byte[] var1, int var2, int var3) throws IOException {
  305.       super.out.write(var1, var2, var3);
  306.       this.written += (long)var3;
  307.    }
  308. }
  309.