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)
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
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
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
;
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;
64 private boolean entryAtEOF
;
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
89 System
.arraycopy(buf
, len
- avail
, out
, offset
, length
);
94 private void readFully(byte[] out
) throws IOException
100 int count
= readBuf(out
, off
, len
);
102 throw new EOFException();
108 private final int readLeByte() throws IOException
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
142 throw new IOException("Stream closed.");
146 int header
= readLeInt();
147 if (header
== CENSIG
)
149 /* Central Header reached. */
153 if (header
!= LOCSIG
)
154 throw new ZipException("Wrong Local header signature: "
155 + Integer
.toHexString(header
));
158 flags
= readLeShort();
159 method
= readLeShort();
160 int dostime
= readLeInt();
161 int crc
= 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
];
173 String name
= new String(buffer
);
175 entry
= createZipEntry(name
);
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
);
187 byte[] extra
= new byte[extraLen
];
189 entry
.setExtra(extra
);
192 if (method
== ZipOutputStream
.DEFLATED
&& avail
> 0)
194 System
.arraycopy(buf
, len
- avail
, buf
, 0, avail
);
197 inf
.setInput(buf
, 0, len
);
202 private void readDataDescr() throws IOException
204 if (readLeInt() != EXTSIG
)
205 throw new ZipException("Data descriptor signature not found");
206 entry
.setCrc(readLeInt() & 0xffffffffL
);
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
219 throw new IOException("Stream closed.");
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 */
234 csize
-= inf
.getTotalIn();
235 avail
= inf
.getRemaining();
238 if (avail
> csize
&& csize
>= 0)
246 long skipped
= in
.skip(csize
& 0xffffffffL
);
248 throw new ZipException("zip archive ends early.");
255 if (method
== ZipOutputStream
.DEFLATED
)
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)
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
292 throw new IOException("Stream closed.");
295 boolean finished
= false;
298 case ZipOutputStream
.DEFLATED
:
299 len
= super.read(b
, off
, len
);
303 throw new ZipException("Inflater not finished!?");
304 avail
= inf
.getRemaining();
305 if ((flags
& 8) != 0)
308 if (inf
.getTotalIn() != csize
309 || inf
.getTotalOut() != size
)
310 throw new ZipException("size mismatch: "+csize
+";"+size
+" <-> "+inf
.getTotalIn()+";"+inf
.getTotalOut());
316 case ZipOutputStream
.STORED
:
318 if (len
> csize
&& csize
>= 0)
321 len
= readBuf(b
, off
, len
);
331 throw new ZipException("EOF in stored block");
336 crc
.update(b
, off
, len
);
340 if ((crc
.getValue() & 0xffffffffL
) != entry
.getCrc())
341 throw new ZipException("CRC mismatch");
350 * Closes the zip file.
351 * @exception IOException if a i/o error occured.
353 public void close() throws IOException
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
);