ZipOutputStream.java 7.52 KB
Newer Older
1 2
/* ZipOutputStream.java - Create a file in zip format
   Copyright (C) 1999, 2000 Free Software Foundation, Inc.
Tom Tromey committed
3

4
This file is part of GNU Classpath.
Tom Tromey committed
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
Tom Tromey committed
26 27

package java.util.zip;
28

Tom Tromey committed
29 30
import java.io.*;

31 32 33 34
/* Written using on-line Java Platform 1.2 API Specification
 * and JCL book.
 * Believed complete and correct.
 */
Tom Tromey committed
35 36 37 38

public class ZipOutputStream extends DeflaterOutputStream
  implements ZipConstants
{
39 40 41
  public static final int STORED = 0;
  public static final int DEFLATED = 8;

42
  public void close () throws IOException
Tom Tromey committed
43
  {
44 45 46 47 48 49 50 51
    finish ();
    out.close();
  }

  public void closeEntry ()  throws IOException
  {
    int uncompressed_size = def.getTotalIn();
    int compressed_size = def.getTotalOut();
52
    long crc = filter.getChecksum().getValue();
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    bytes_written += compressed_size;

    bytes_written += put4 (0x08074b50);
    if (current.getCrc() == -1 || current.getCompressedSize() == -1
	|| current.getSize() == -1)
      {
	current.setCrc(crc);
	current.compressedSize = compressed_size;
	current.setSize(uncompressed_size);
      }
    else
      {
	if (current.getCrc() != crc
	    || current.getCompressedSize() != compressed_size
	    || current.getSize() != uncompressed_size)
	  throw new ZipException ("zip entry field incorrect");
      }
    bytes_written += put4 ((int) (current.getCrc()));
    bytes_written += put4 ((int) (current.getCompressedSize()));
    bytes_written += put4 ((int) (current.getSize()));

    current.next = chain;
    chain = current;
    current = null;
    filter = null;
  }

  public void finish () throws IOException
  {
    if (current != null)
      closeEntry ();

    // Write the central directory.
    long offset = bytes_written;
    int count = 0;
    int bytes = 0;
    while (chain != null)
      {
	bytes += write_entry (chain, false);
	++count;
	chain = chain.next;
      }

    // Write the end of the central directory record.
    put4 (0x06054b50);
    // Disk number.
    put2 (0);
    // Another disk number.
    put2 (0);
    put2 (count);
    put4 (bytes);
    put4 ((int) offset);

    byte[] c = comment.getBytes("8859_1");
    put2 (c.length);
    out.write(c);
    out.write((byte) 0);
Tom Tromey committed
111 112
  }

113 114 115 116 117 118 119 120 121 122 123 124 125
  // Helper for finish and putNextEntry.
  private int write_entry (ZipEntry entry, boolean is_local)
    throws IOException
  {
    long offset = bytes_written;

    int bytes = put4 (is_local ? 0x04034b50 : 0x02014b50);
    if (! is_local)
      bytes += put_version ();
    bytes += put_version ();

    boolean crc_after = false;
    if (is_local
126 127
	&& (entry.getCrc() == -1 || entry.getCompressedSize() == -1
	    || entry.getSize() == -1))
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      crc_after = true;
    // For the bits field we always indicate `normal' compression,
    // even if that isn't true.
    bytes += put2 (crc_after ? (1 << 3) : 0);
    bytes += put2 (entry.method);

    bytes += put2(0);  // time - FIXME
    bytes += put2(0);  // date - FIXME

    if (crc_after)
      {
	// CRC, compressedSize, and Size are always 0 in this header.
	// The actual values are given after the entry.
	bytes += put4 (0);
	bytes += put4 (0);
	bytes += put4 (0);
      }
    else
      {
	bytes += put4 ((int) (entry.getCrc()));
	bytes += put4 ((int) (entry.getCompressedSize()));
	bytes += put4 ((int) (entry.getSize()));
      }

    byte[] name = entry.name.getBytes("8859_1");
    bytes += put2 (name.length);
    bytes += put2 (entry.extra == null ? 0 : entry.extra.length);

    byte[] comment = null;
    if (! is_local)
      {
	if (entry.getComment() == null)
	  bytes += put2 (0);
	else
	  {
	    comment = entry.getComment().getBytes("8859_1");
	    bytes += put2 (comment.length);
	  }

	// Disk number start.
	bytes += put2 (0);
	// Internal file attributes.
	bytes += put2 (0);
	// External file attributes.
	bytes += put2 (0);
	// Relative offset of local header.
	bytes += put2 ((int) offset);
      }

    out.write (name);
    out.write ((byte) 0);
    bytes += name.length + 1;
    if (entry.extra != null)
      {
	out.write(entry.extra);
	out.write((byte) 0);
	bytes += entry.extra.length + 1;
      }
    if (comment != null)
      {
	out.write(comment);
	out.write((byte) 0);
	bytes += comment.length + 1;
      }

    bytes_written += bytes;
    return bytes;
  }
196

Tom Tromey committed
197 198
  public void putNextEntry (ZipEntry entry) throws IOException
  {
199 200 201
    if (current != null)
      closeEntry ();

202 203
    if (entry.method < 0 )
      entry.method = method;
204 205 206 207 208 209 210 211 212 213 214 215 216 217
    if (entry.method == STORED)
      {
	if (entry.getSize() == -1 || entry.getCrc() == -1)
	  throw new ZipException ("required entry not set");
	// Just in case.
	entry.compressedSize = entry.getSize();
      }
    write_entry (entry, true);
    current = entry;
    int compr = (method == STORED) ? Deflater.NO_COMPRESSION : level;
    def.reset();
    def.setLevel(compr);
    filter = new CheckedOutputStream (new DeflaterOutputStream (out, def),
				      new CRC32 ());
Tom Tromey committed
218
  }
219

220
  public void setLevel (int level)
221
  {
222 223 224 225 226
    if (level != Deflater.DEFAULT_COMPRESSION
	&& (level < Deflater.NO_COMPRESSION
	    || level > Deflater.BEST_COMPRESSION))
      throw new IllegalArgumentException ();
    this.level = level;
227 228
  }

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  public void setMethod (int method)
  {
    if (method != DEFLATED && method != STORED)
      throw new IllegalArgumentException ();
    this.method = method;
  }

  public void setComment (String comment)
  {
    if (comment.length() > 65535)
      throw new IllegalArgumentException ();
    this.comment = comment;
  }

  public synchronized void write (byte[] buf, int off, int len)
    throws IOException
  {
    if (filter == null)
      throw new ZipException ("no open zip entry");
    filter.write(buf, off, len);
  }

  public ZipOutputStream (OutputStream out)
  {
    super (out);
    def = new Deflater (level, true);
  }

  private int put2 (int i) throws IOException
258 259 260
  {
    out.write (i);
    out.write (i >> 8);
261
    return 2;
262 263
  }

264
  private int put4 (int i) throws IOException
265 266 267 268 269
  {
    out.write (i);
    out.write (i >> 8);
    out.write (i >> 16);
    out.write (i >> 24);
270
    return 4;
271
  }
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

  private int put_version () throws IOException
  {
    // FIXME: for now we assume Unix, and we ignore the version
    // number.
    return put2 (3 << 8);
  }

  // The entry we are currently writing, or null if we've called
  // closeEntry.
  private ZipEntry current;
  // The chain of entries which have been written to this file.
  private ZipEntry chain;
  // The output stream to which data should be sent.
  private CheckedOutputStream filter;

  private int method = DEFLATED;
  private int level = Deflater.DEFAULT_COMPRESSION;
  private String comment = "";
  private long bytes_written;

  // The Deflater we use.
  private Deflater def;
Tom Tromey committed
295
}