home *** CD-ROM | disk | FTP | other *** search
- package java.util.zip;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
-
- public class ZipOutputStream extends DeflaterOutputStream implements ZipConstants {
- private ZipEntry entry;
- private Vector entries = new Vector();
- private Hashtable names = new Hashtable();
- private CRC32 crc = new CRC32();
- private long written;
- private long locoff;
- private String comment;
- private int method = 8;
- private boolean finished;
- public static final int STORED = 0;
- public static final int DEFLATED = 8;
-
- public ZipOutputStream(OutputStream var1) {
- super(var1, new Deflater(-1, true));
- }
-
- public void setComment(String var1) {
- if (var1.length() > 65535) {
- throw new IllegalArgumentException("invalid ZIP file comment");
- } else {
- this.comment = var1;
- }
- }
-
- public void setMethod(int var1) {
- if (var1 != 8 && var1 != 0) {
- throw new IllegalArgumentException("invalid compression method");
- } else {
- this.method = var1;
- }
- }
-
- public void setLevel(int var1) {
- super.def.setLevel(var1);
- }
-
- public void putNextEntry(ZipEntry var1) throws IOException {
- if (this.entry != null) {
- this.closeEntry();
- }
-
- if (var1.time == -1L) {
- var1.setTime(System.currentTimeMillis());
- }
-
- if (var1.method == -1) {
- var1.method = this.method;
- }
-
- switch (var1.method) {
- case 0:
- if (var1.size == -1L) {
- var1.size = var1.csize;
- } else if (var1.csize == -1L) {
- var1.csize = var1.size;
- } else if (var1.size != var1.csize) {
- throw new ZipException("STORED entry where compressed != uncompressed size");
- }
-
- if (var1.size == -1L || var1.crc == -1L) {
- throw new ZipException("STORED entry missing size, compressed size, or crc-32");
- }
-
- var1.version = 10;
- var1.flag = 0;
- break;
- case 8:
- if (var1.size != -1L && var1.csize != -1L && var1.crc != -1L) {
- if (var1.size == -1L || var1.csize == -1L || var1.crc == -1L) {
- throw new ZipException("DEFLATED entry missing size, compressed size, or crc-32");
- }
-
- var1.flag = 0;
- } else {
- var1.flag = 8;
- }
-
- var1.version = 20;
- break;
- default:
- throw new ZipException("unsupported compression method");
- }
-
- var1.offset = this.written;
- this.writeLOC(var1);
- if (this.names.put(var1.name, var1) != null) {
- throw new ZipException("duplicate entry: " + var1.name);
- } else {
- this.entries.addElement(var1);
- this.entry = var1;
- }
- }
-
- public void closeEntry() throws IOException {
- ZipEntry var1 = this.entry;
- if (var1 != null) {
- switch (var1.method) {
- case 0:
- if (var1.size != this.written - this.locoff) {
- throw new ZipException("invalid entry size (expected " + var1.size + " but got " + (this.written - this.locoff) + " bytes)");
- }
-
- if (var1.crc != this.crc.getValue()) {
- throw new ZipException("invalid entry crc-32 (expected 0x" + Long.toHexString(var1.size) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
- }
- break;
- case 8:
- super.def.finish();
-
- while(!super.def.finished()) {
- ((DeflaterOutputStream)this).deflate();
- }
-
- if ((var1.flag & 8) == 0) {
- if (var1.size != (long)super.def.getTotalIn()) {
- throw new ZipException("invalid entry size (expected " + var1.size + " but got " + super.def.getTotalIn() + " bytes)");
- }
-
- if (var1.csize != (long)super.def.getTotalOut()) {
- throw new ZipException("invalid entry compressed size (expected " + var1.csize + " but got " + super.def.getTotalOut() + " bytes)");
- }
-
- if (var1.crc != this.crc.getValue()) {
- throw new ZipException("invalid entry CRC-32 (expected 0x" + Long.toHexString(var1.crc) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
- }
- } else {
- var1.size = (long)super.def.getTotalIn();
- var1.csize = (long)super.def.getTotalOut();
- var1.crc = this.crc.getValue();
- this.writeEXT(var1);
- }
-
- super.def.reset();
- this.written += var1.csize;
- break;
- default:
- throw new InternalError("invalid compression method");
- }
-
- this.crc.reset();
- this.entry = null;
- }
-
- }
-
- public synchronized void write(byte[] var1, int var2, int var3) throws IOException {
- if (this.entry == null) {
- throw new ZipException("no current ZIP entry");
- } else {
- switch (this.entry.method) {
- case 0:
- this.written += (long)var3;
- if (this.written - this.locoff > this.entry.size) {
- throw new ZipException("attempt to write past end of STORED entry");
- }
-
- super.out.write(var1, var2, var3);
- break;
- case 8:
- super.write(var1, var2, var3);
- break;
- default:
- throw new InternalError("invalid compression method");
- }
-
- this.crc.update(var1, var2, var3);
- }
- }
-
- public void finish() throws IOException {
- if (!this.finished) {
- if (this.entry != null) {
- this.closeEntry();
- }
-
- if (this.entries.size() < 1) {
- throw new ZipException("ZIP file must have at least one entry");
- } else {
- long var1 = this.written;
- Enumeration var3 = this.entries.elements();
-
- while(var3.hasMoreElements()) {
- this.writeCEN((ZipEntry)var3.nextElement());
- }
-
- this.writeEND(var1, this.written - var1);
- this.finished = true;
- }
- }
- }
-
- public void close() throws IOException {
- this.finish();
- super.out.close();
- }
-
- private void writeLOC(ZipEntry var1) throws IOException {
- this.writeInt(67324752L);
- this.writeShort(var1.version);
- this.writeShort(var1.flag);
- this.writeShort(var1.method);
- this.writeInt(var1.time);
- if ((var1.flag & 8) == 8) {
- this.writeInt(0L);
- this.writeInt(0L);
- this.writeInt(0L);
- } else {
- this.writeInt(var1.crc);
- this.writeInt(var1.csize);
- this.writeInt(var1.size);
- }
-
- this.writeShort(var1.name.length());
- this.writeShort(var1.extra != null ? var1.extra.length : 0);
- this.writeAscii(var1.name);
- if (var1.extra != null) {
- this.writeBytes(var1.extra, 0, var1.extra.length);
- }
-
- this.locoff = this.written;
- }
-
- private void writeEXT(ZipEntry var1) throws IOException {
- this.writeInt(134695760L);
- this.writeInt(var1.crc);
- this.writeInt(var1.csize);
- this.writeInt(var1.size);
- }
-
- private void writeCEN(ZipEntry var1) throws IOException {
- this.writeInt(33639248L);
- this.writeShort(var1.version);
- this.writeShort(var1.version);
- this.writeShort(var1.flag);
- this.writeShort(var1.method);
- this.writeInt(var1.time);
- this.writeInt(var1.crc);
- this.writeInt(var1.csize);
- this.writeInt(var1.size);
- this.writeShort(var1.name.length());
- this.writeShort(var1.extra != null ? var1.extra.length : 0);
- this.writeShort(var1.comment != null ? var1.comment.length() : 0);
- this.writeShort(0);
- this.writeShort(0);
- this.writeInt(0L);
- this.writeInt(var1.offset);
- this.writeAscii(var1.name);
- if (var1.extra != null) {
- this.writeBytes(var1.extra, 0, var1.extra.length);
- }
-
- if (var1.comment != null) {
- this.writeAscii(var1.comment);
- }
-
- }
-
- private void writeEND(long var1, long var3) throws IOException {
- this.writeInt(101010256L);
- this.writeShort(0);
- this.writeShort(0);
- this.writeShort(this.entries.size());
- this.writeShort(this.entries.size());
- this.writeInt(var3);
- this.writeInt(var1);
- this.writeShort(this.comment != null ? this.comment.length() : 0);
- if (this.comment != null) {
- this.writeAscii(this.comment);
- }
-
- }
-
- private void writeShort(int var1) throws IOException {
- OutputStream var2 = super.out;
- var2.write(var1 & 255);
- var2.write(var1 >>> 8 & 255);
- this.written += 2L;
- }
-
- private void writeInt(long var1) throws IOException {
- OutputStream var3 = super.out;
- var3.write((int)(var1 & 255L));
- var3.write((int)(var1 >>> 8 & 255L));
- var3.write((int)(var1 >>> 16 & 255L));
- var3.write((int)(var1 >>> 24 & 255L));
- this.written += 4L;
- }
-
- private void writeAscii(String var1) throws IOException {
- byte[] var2 = new byte[var1.length()];
- var1.getBytes(0, var2.length, var2, 0);
- this.writeBytes(var2, 0, var2.length);
- }
-
- private void writeBytes(byte[] var1, int var2, int var3) throws IOException {
- super.out.write(var1, var2, var3);
- this.written += (long)var3;
- }
- }
-