FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / util / zip / ZipInputStream.java
blob2fba9f556dccfe85fbb79981805e662f3ea79e93
1 /* java.util.zip.ZipInputStream
2 Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package java.util.zip;
39 import java.io.EOFException;
40 import java.io.InputStream;
41 import java.io.IOException;
42 import java.util.Enumeration;
44 /**
45 * This is a FilterInputStream that reads the files in an zip archive
46 * one after another. It has a special method to get the zip entry of
47 * the next file. The zip entry contains information about the file name
48 * size, compressed size, CRC, etc.
50 * It includes support for STORED and DEFLATED entries.
52 * @author Jochen Hoenicke
54 public class ZipInputStream extends InflaterInputStream implements ZipConstants
56 private CRC32 crc = new CRC32();
57 private ZipEntry entry = null;
59 private int csize;
60 private int size;
61 private int method;
62 private int flags;
63 private int avail;
64 private boolean entryAtEOF;
66 /**
67 * Creates a new Zip input stream, reading a zip archive.
69 public ZipInputStream(InputStream in)
71 super(in, new Inflater(true));
74 private void fillBuf() throws IOException
76 avail = len = in.read(buf, 0, buf.length);
79 private int readBuf(byte[] out, int offset, int length) throws IOException
81 if (avail <= 0)
83 fillBuf();
84 if (avail <= 0)
85 return -1;
87 if (length > avail)
88 length = avail;
89 System.arraycopy(buf, len - avail, out, offset, length);
90 avail -= length;
91 return length;
94 private void readFully(byte[] out) throws IOException
96 int off = 0;
97 int len = out.length;
98 while (len > 0)
100 int count = readBuf(out, off, len);
101 if (count == -1)
102 throw new EOFException();
103 off += count;
104 len -= count;
108 private final int readLeByte() throws IOException
110 if (avail <= 0)
112 fillBuf();
113 if (avail <= 0)
114 throw new ZipException("EOF in header");
116 return buf[len - avail--] & 0xff;
120 * Read an unsigned short in little endian byte order.
122 private final int readLeShort() throws IOException
124 return readLeByte() | (readLeByte() << 8);
128 * Read an int in little endian byte order.
130 private final int readLeInt() throws IOException
132 return readLeShort() | (readLeShort() << 16);
136 * Open the next entry from the zip archive, and return its description.
137 * If the previous entry wasn't closed, this method will close it.
139 public ZipEntry getNextEntry() throws IOException
141 if (crc == null)
142 throw new IOException("Stream closed.");
143 if (entry != null)
144 closeEntry();
146 int header = readLeInt();
147 if (header == CENSIG)
149 /* Central Header reached. */
150 close();
151 return null;
153 if (header != LOCSIG)
154 throw new ZipException("Wrong Local header signature: "
155 + Integer.toHexString(header));
156 /* skip version */
157 readLeShort();
158 flags = readLeShort();
159 method = readLeShort();
160 int dostime = readLeInt();
161 int crc = readLeInt();
162 csize = readLeInt();
163 size = readLeInt();
164 int nameLen = readLeShort();
165 int extraLen = readLeShort();
167 if (method == ZipOutputStream.STORED && csize != size)
168 throw new ZipException("Stored, but compressed != uncompressed");
171 byte[] buffer = new byte[nameLen];
172 readFully(buffer);
173 String name = new String(buffer);
175 entry = createZipEntry(name);
176 entryAtEOF = false;
177 entry.setMethod(method);
178 if ((flags & 8) == 0)
180 entry.setCrc(crc & 0xffffffffL);
181 entry.setSize(size & 0xffffffffL);
182 entry.setCompressedSize(csize & 0xffffffffL);
184 entry.setDOSTime(dostime);
185 if (extraLen > 0)
187 byte[] extra = new byte[extraLen];
188 readFully(extra);
189 entry.setExtra(extra);
192 if (method == ZipOutputStream.DEFLATED && avail > 0)
194 System.arraycopy(buf, len - avail, buf, 0, avail);
195 len = avail;
196 avail = 0;
197 inf.setInput(buf, 0, len);
199 return entry;
202 private void readDataDescr() throws IOException
204 if (readLeInt() != EXTSIG)
205 throw new ZipException("Data descriptor signature not found");
206 entry.setCrc(readLeInt() & 0xffffffffL);
207 csize = readLeInt();
208 size = readLeInt();
209 entry.setSize(size & 0xffffffffL);
210 entry.setCompressedSize(csize & 0xffffffffL);
214 * Closes the current zip entry and moves to the next one.
216 public void closeEntry() throws IOException
218 if (crc == null)
219 throw new IOException("Stream closed.");
220 if (entry == null)
221 return;
223 if (method == ZipOutputStream.DEFLATED)
225 if ((flags & 8) != 0)
227 /* We don't know how much we must skip, read until end. */
228 byte[] tmp = new byte[2048];
229 while (read(tmp) > 0)
231 /* read will close this entry */
232 return;
234 csize -= inf.getTotalIn();
235 avail = inf.getRemaining();
238 if (avail > csize && csize >= 0)
239 avail -= csize;
240 else
242 csize -= avail;
243 avail = 0;
244 while (csize != 0)
246 long skipped = in.skip(csize & 0xffffffffL);
247 if (skipped <= 0)
248 throw new ZipException("zip archive ends early.");
249 csize -= skipped;
253 size = 0;
254 crc.reset();
255 if (method == ZipOutputStream.DEFLATED)
256 inf.reset();
257 entry = null;
258 entryAtEOF = true;
261 public int available() throws IOException
263 return entryAtEOF ? 0 : 1;
267 * Reads a byte from the current zip entry.
268 * @return the byte or -1 on EOF.
269 * @exception IOException if a i/o error occured.
270 * @exception ZipException if the deflated stream is corrupted.
272 public int read() throws IOException
274 byte[] b = new byte[1];
275 if (read(b, 0, 1) <= 0)
276 return -1;
277 return b[0] & 0xff;
281 * Reads a block of bytes from the current zip entry.
282 * @return the number of bytes read (may be smaller, even before
283 * EOF), or -1 on EOF.
284 * @exception IOException if a i/o error occured.
285 * @exception ZipException if the deflated stream is corrupted.
287 public int read(byte[] b, int off, int len) throws IOException
289 if (len == 0)
290 return 0;
291 if (crc == null)
292 throw new IOException("Stream closed.");
293 if (entry == null)
294 return -1;
295 boolean finished = false;
296 switch (method)
298 case ZipOutputStream.DEFLATED:
299 len = super.read(b, off, len);
300 if (len < 0)
302 if (!inf.finished())
303 throw new ZipException("Inflater not finished!?");
304 avail = inf.getRemaining();
305 if ((flags & 8) != 0)
306 readDataDescr();
308 if (inf.getTotalIn() != csize
309 || inf.getTotalOut() != size)
310 throw new ZipException("size mismatch: "+csize+";"+size+" <-> "+inf.getTotalIn()+";"+inf.getTotalOut());
311 inf.reset();
312 finished = true;
314 break;
316 case ZipOutputStream.STORED:
318 if (len > csize && csize >= 0)
319 len = csize;
321 len = readBuf(b, off, len);
322 if (len > 0)
324 csize -= len;
325 size -= len;
328 if (csize == 0)
329 finished = true;
330 else if (len < 0)
331 throw new ZipException("EOF in stored block");
332 break;
335 if (len > 0)
336 crc.update(b, off, len);
338 if (finished)
340 if ((crc.getValue() & 0xffffffffL) != entry.getCrc())
341 throw new ZipException("CRC mismatch");
342 crc.reset();
343 entry = null;
344 entryAtEOF = true;
346 return len;
350 * Closes the zip file.
351 * @exception IOException if a i/o error occured.
353 public void close() throws IOException
355 super.close();
356 crc = null;
357 entry = null;
358 entryAtEOF = true;
362 * Creates a new zip entry for the given name. This is equivalent
363 * to new ZipEntry(name).
364 * @param name the name of the zip entry.
366 protected ZipEntry createZipEntry(String name)
368 return new ZipEntry(name);