| 1 | /* ZipOutputStream.java - Create a file in zip format
|
|---|
| 2 | Copyright (C) 1999, 2000 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 | package java.util.zip;
|
|---|
| 39 |
|
|---|
| 40 | import java.io.*;
|
|---|
| 41 |
|
|---|
| 42 | /* Written using on-line Java Platform 1.2 API Specification
|
|---|
| 43 | * and JCL book.
|
|---|
| 44 | * Believed complete and correct.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | public class ZipOutputStream extends DeflaterOutputStream
|
|---|
| 48 | implements ZipConstants
|
|---|
| 49 | {
|
|---|
| 50 | public static final int STORED = 0;
|
|---|
| 51 | public static final int DEFLATED = 8;
|
|---|
| 52 |
|
|---|
| 53 | public void close () throws IOException
|
|---|
| 54 | {
|
|---|
| 55 | finish ();
|
|---|
| 56 | out.close();
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | public void closeEntry () throws IOException
|
|---|
| 60 | {
|
|---|
| 61 | int compressed_size;
|
|---|
| 62 | if (current.method == STORED)
|
|---|
| 63 | {
|
|---|
| 64 | compressed_size = uncompressed_size;
|
|---|
| 65 | }
|
|---|
| 66 | else
|
|---|
| 67 | {
|
|---|
| 68 | super.finish();
|
|---|
| 69 | compressed_size = def.getTotalOut();
|
|---|
| 70 | }
|
|---|
| 71 | long crc = sum.getValue();
|
|---|
| 72 |
|
|---|
| 73 | bytes_written += compressed_size;
|
|---|
| 74 |
|
|---|
| 75 | if (current.getCrc() == -1 || current.getCompressedSize() == -1
|
|---|
| 76 | || current.getSize() == -1)
|
|---|
| 77 | {
|
|---|
| 78 | current.setCrc(crc);
|
|---|
| 79 | current.compressedSize = compressed_size;
|
|---|
| 80 | current.setSize(uncompressed_size);
|
|---|
| 81 | put4 (0x08074b50);
|
|---|
| 82 | put4 ((int) (current.getCrc()));
|
|---|
| 83 | put4 ((int) (current.getCompressedSize()));
|
|---|
| 84 | put4 ((int) (current.getSize()));
|
|---|
| 85 | bytes_written += 16;
|
|---|
| 86 | }
|
|---|
| 87 | else if (current.getCrc() != crc
|
|---|
| 88 | || current.getCompressedSize() != compressed_size
|
|---|
| 89 | || current.getSize() != uncompressed_size)
|
|---|
| 90 | throw new ZipException ("zip entry field incorrect");
|
|---|
| 91 |
|
|---|
| 92 | current.next = chain;
|
|---|
| 93 | chain = current;
|
|---|
| 94 | current = null;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | public void write (int bval) throws IOException
|
|---|
| 98 | {
|
|---|
| 99 | if (current.method == STORED)
|
|---|
| 100 | {
|
|---|
| 101 | out.write(bval);
|
|---|
| 102 | }
|
|---|
| 103 | else
|
|---|
| 104 | super.write(bval);
|
|---|
| 105 | sum.update(bval);
|
|---|
| 106 | uncompressed_size += 1;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | public void write (byte[] buf, int off, int len) throws IOException
|
|---|
| 110 | {
|
|---|
| 111 | if (current.method == STORED)
|
|---|
| 112 | out.write(buf, off, len);
|
|---|
| 113 | else
|
|---|
| 114 | super.write(buf, off, len);
|
|---|
| 115 | sum.update(buf, off, len);
|
|---|
| 116 | uncompressed_size += len;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | public void finish () throws IOException
|
|---|
| 120 | {
|
|---|
| 121 | if (current != null)
|
|---|
| 122 | closeEntry ();
|
|---|
| 123 |
|
|---|
| 124 | // Write the central directory.
|
|---|
| 125 | long offset = bytes_written;
|
|---|
| 126 | int count = 0;
|
|---|
| 127 | int bytes = 0;
|
|---|
| 128 | while (chain != null)
|
|---|
| 129 | {
|
|---|
| 130 | bytes += write_entry (chain, false);
|
|---|
| 131 | ++count;
|
|---|
| 132 | chain = chain.next;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | // Write the end of the central directory record.
|
|---|
| 136 | put4 (0x06054b50);
|
|---|
| 137 | // Disk number.
|
|---|
| 138 | put2 (0);
|
|---|
| 139 | // Another disk number.
|
|---|
| 140 | put2 (0);
|
|---|
| 141 | put2 (count);
|
|---|
| 142 | put2 (count);
|
|---|
| 143 | put4 (bytes);
|
|---|
| 144 | put4 ((int) offset);
|
|---|
| 145 |
|
|---|
| 146 | byte[] c = comment.getBytes("8859_1");
|
|---|
| 147 | put2 (c.length);
|
|---|
| 148 | out.write(c);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | // Helper for finish and putNextEntry.
|
|---|
| 152 | private int write_entry (ZipEntry entry, boolean is_local)
|
|---|
| 153 | throws IOException
|
|---|
| 154 | {
|
|---|
| 155 | int bytes = put4 (is_local ? 0x04034b50 : 0x02014b50);
|
|---|
| 156 | if (! is_local)
|
|---|
| 157 | bytes += put_version ();
|
|---|
| 158 | bytes += put_version ();
|
|---|
| 159 |
|
|---|
| 160 | boolean crc_after = false;
|
|---|
| 161 | if (is_local
|
|---|
| 162 | && (entry.getCrc() == -1 || entry.getCompressedSize() == -1
|
|---|
| 163 | || entry.getSize() == -1))
|
|---|
| 164 | crc_after = true;
|
|---|
| 165 | // For the bits field we always indicate `normal' compression,
|
|---|
| 166 | // even if that isn't true.
|
|---|
| 167 | bytes += put2 (crc_after ? (1 << 3) : 0);
|
|---|
| 168 | bytes += put2 (entry.method);
|
|---|
| 169 |
|
|---|
| 170 | bytes += put2(0); // time - FIXME
|
|---|
| 171 | bytes += put2(0); // date - FIXME
|
|---|
| 172 |
|
|---|
| 173 | if (crc_after)
|
|---|
| 174 | {
|
|---|
| 175 | // CRC, compressedSize, and Size are always 0 in this header.
|
|---|
| 176 | // The actual values are given after the entry.
|
|---|
| 177 | bytes += put4 (0);
|
|---|
| 178 | bytes += put4 (0);
|
|---|
| 179 | bytes += put4 (0);
|
|---|
| 180 | }
|
|---|
| 181 | else
|
|---|
| 182 | {
|
|---|
| 183 | bytes += put4 ((int) (entry.getCrc()));
|
|---|
| 184 | bytes += put4 ((int) (entry.getCompressedSize()));
|
|---|
| 185 | bytes += put4 ((int) (entry.getSize()));
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | byte[] name = entry.name.getBytes("8859_1");
|
|---|
| 189 | bytes += put2 (name.length);
|
|---|
| 190 | bytes += put2 (entry.extra == null ? 0 : entry.extra.length);
|
|---|
| 191 |
|
|---|
| 192 | byte[] comment = null;
|
|---|
| 193 | if (! is_local)
|
|---|
| 194 | {
|
|---|
| 195 | if (entry.getComment() == null)
|
|---|
| 196 | bytes += put2 (0);
|
|---|
| 197 | else
|
|---|
| 198 | {
|
|---|
| 199 | comment = entry.getComment().getBytes("8859_1");
|
|---|
| 200 | bytes += put2 (comment.length);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | // Disk number start.
|
|---|
| 204 | bytes += put2 (0);
|
|---|
| 205 | // Internal file attributes.
|
|---|
| 206 | bytes += put2 (0);
|
|---|
| 207 | // External file attributes.
|
|---|
| 208 | bytes += put4 (0);
|
|---|
| 209 | // Relative offset of local header.
|
|---|
| 210 | bytes += put4 ((int) entry.relativeOffset);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | out.write (name);
|
|---|
| 214 | bytes += name.length;
|
|---|
| 215 | if (entry.extra != null)
|
|---|
| 216 | {
|
|---|
| 217 | out.write(entry.extra);
|
|---|
| 218 | bytes += entry.extra.length;
|
|---|
| 219 | }
|
|---|
| 220 | if (comment != null)
|
|---|
| 221 | {
|
|---|
| 222 | out.write(comment);
|
|---|
| 223 | bytes += comment.length;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | bytes_written += bytes;
|
|---|
| 227 | return bytes;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | public void putNextEntry (ZipEntry entry) throws IOException
|
|---|
| 231 | {
|
|---|
| 232 | if (current != null)
|
|---|
| 233 | closeEntry ();
|
|---|
| 234 |
|
|---|
| 235 | if (entry.method < 0 )
|
|---|
| 236 | entry.method = method;
|
|---|
| 237 | if (entry.method == STORED)
|
|---|
| 238 | {
|
|---|
| 239 | if (entry.getSize() == -1 || entry.getCrc() == -1)
|
|---|
| 240 | throw new ZipException ("required entry not set");
|
|---|
| 241 | // Just in case.
|
|---|
| 242 | entry.compressedSize = entry.getSize();
|
|---|
| 243 | }
|
|---|
| 244 | entry.relativeOffset = bytes_written;
|
|---|
| 245 | write_entry (entry, true);
|
|---|
| 246 | current = entry;
|
|---|
| 247 | int compr = (method == STORED) ? Deflater.NO_COMPRESSION : level;
|
|---|
| 248 | def.reset();
|
|---|
| 249 | def.setLevel(compr);
|
|---|
| 250 | sum.reset();
|
|---|
| 251 | uncompressed_size = 0;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | public void setLevel (int level)
|
|---|
| 255 | {
|
|---|
| 256 | if (level != Deflater.DEFAULT_COMPRESSION
|
|---|
| 257 | && (level < Deflater.NO_COMPRESSION
|
|---|
| 258 | || level > Deflater.BEST_COMPRESSION))
|
|---|
| 259 | throw new IllegalArgumentException ();
|
|---|
| 260 | this.level = level;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | public void setMethod (int method)
|
|---|
| 264 | {
|
|---|
| 265 | if (method != DEFLATED && method != STORED)
|
|---|
| 266 | throw new IllegalArgumentException ();
|
|---|
| 267 | this.method = method;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | public void setComment (String comment)
|
|---|
| 271 | {
|
|---|
| 272 | if (comment.length() > 65535)
|
|---|
| 273 | throw new IllegalArgumentException ();
|
|---|
| 274 | this.comment = comment;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | public ZipOutputStream (OutputStream out)
|
|---|
| 278 | {
|
|---|
| 279 | super (out, new Deflater (Deflater.DEFAULT_COMPRESSION, true), 8192);
|
|---|
| 280 | sum = new CRC32 ();
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | private int put2 (int i) throws IOException
|
|---|
| 284 | {
|
|---|
| 285 | out.write (i);
|
|---|
| 286 | out.write (i >> 8);
|
|---|
| 287 | return 2;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | private int put4 (int i) throws IOException
|
|---|
| 291 | {
|
|---|
| 292 | out.write (i);
|
|---|
| 293 | out.write (i >> 8);
|
|---|
| 294 | out.write (i >> 16);
|
|---|
| 295 | out.write (i >> 24);
|
|---|
| 296 | return 4;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | private int put_version () throws IOException
|
|---|
| 300 | {
|
|---|
| 301 | // FIXME: for now we assume Unix, and we ignore the version
|
|---|
| 302 | // number.
|
|---|
| 303 | return put2 (3 << 8);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | // The entry we are currently writing, or null if we've called
|
|---|
| 307 | // closeEntry.
|
|---|
| 308 | private ZipEntry current;
|
|---|
| 309 | // The chain of entries which have been written to this file.
|
|---|
| 310 | private ZipEntry chain;
|
|---|
| 311 |
|
|---|
| 312 | private int method = DEFLATED;
|
|---|
| 313 | private int level = Deflater.DEFAULT_COMPRESSION;
|
|---|
| 314 | private String comment = "";
|
|---|
| 315 | private long bytes_written;
|
|---|
| 316 |
|
|---|
| 317 | private int uncompressed_size;
|
|---|
| 318 |
|
|---|
| 319 | /** The checksum object. */
|
|---|
| 320 | private Checksum sum;
|
|---|
| 321 | }
|
|---|