1 | /* java.util.zip.ZipInputStream
|
---|
2 | Copyright (C) 2001, 2002, 2003 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 | import java.io.EOFException;
|
---|
40 | import java.io.InputStream;
|
---|
41 | import java.io.IOException;
|
---|
42 | import java.util.Enumeration;
|
---|
43 |
|
---|
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.
|
---|
49 | *
|
---|
50 | * It includes support for STORED and DEFLATED entries.
|
---|
51 | *
|
---|
52 | * @author Jochen Hoenicke
|
---|
53 | */
|
---|
54 | public class ZipInputStream extends InflaterInputStream implements ZipConstants
|
---|
55 | {
|
---|
56 | private CRC32 crc = new CRC32();
|
---|
57 | private ZipEntry entry = null;
|
---|
58 |
|
---|
59 | private int csize;
|
---|
60 | private int size;
|
---|
61 | private int method;
|
---|
62 | private int flags;
|
---|
63 | private int avail;
|
---|
64 | private boolean entryAtEOF;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Creates a new Zip input stream, reading a zip archive.
|
---|
68 | */
|
---|
69 | public ZipInputStream(InputStream in)
|
---|
70 | {
|
---|
71 | super(in, new Inflater(true));
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void fillBuf() throws IOException
|
---|
75 | {
|
---|
76 | avail = len = in.read(buf, 0, buf.length);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private int readBuf(byte[] out, int offset, int length) throws IOException
|
---|
80 | {
|
---|
81 | if (avail <= 0)
|
---|
82 | {
|
---|
83 | fillBuf();
|
---|
84 | if (avail <= 0)
|
---|
85 | return -1;
|
---|
86 | }
|
---|
87 | if (length > avail)
|
---|
88 | length = avail;
|
---|
89 | System.arraycopy(buf, len - avail, out, offset, length);
|
---|
90 | avail -= length;
|
---|
91 | return length;
|
---|
92 | }
|
---|
93 |
|
---|
94 | private void readFully(byte[] out) throws IOException
|
---|
95 | {
|
---|
96 | int off = 0;
|
---|
97 | int len = out.length;
|
---|
98 | while (len > 0)
|
---|
99 | {
|
---|
100 | int count = readBuf(out, off, len);
|
---|
101 | if (count == -1)
|
---|
102 | throw new EOFException();
|
---|
103 | off += count;
|
---|
104 | len -= count;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | private final int readLeByte() throws IOException
|
---|
109 | {
|
---|
110 | if (avail <= 0)
|
---|
111 | {
|
---|
112 | fillBuf();
|
---|
113 | if (avail <= 0)
|
---|
114 | throw new ZipException("EOF in header");
|
---|
115 | }
|
---|
116 | return buf[len - avail--] & 0xff;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Read an unsigned short in little endian byte order.
|
---|
121 | */
|
---|
122 | private final int readLeShort() throws IOException
|
---|
123 | {
|
---|
124 | return readLeByte() | (readLeByte() << 8);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Read an int in little endian byte order.
|
---|
129 | */
|
---|
130 | private final int readLeInt() throws IOException
|
---|
131 | {
|
---|
132 | return readLeShort() | (readLeShort() << 16);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
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.
|
---|
138 | */
|
---|
139 | public ZipEntry getNextEntry() throws IOException
|
---|
140 | {
|
---|
141 | if (crc == null)
|
---|
142 | throw new IOException("Stream closed.");
|
---|
143 | if (entry != null)
|
---|
144 | closeEntry();
|
---|
145 |
|
---|
146 | int header = readLeInt();
|
---|
147 | if (header == CENSIG)
|
---|
148 | {
|
---|
149 | /* Central Header reached. */
|
---|
150 | close();
|
---|
151 | return null;
|
---|
152 | }
|
---|
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();
|
---|
166 |
|
---|
167 | if (method == ZipOutputStream.STORED && csize != size)
|
---|
168 | throw new ZipException("Stored, but compressed != uncompressed");
|
---|
169 |
|
---|
170 |
|
---|
171 | byte[] buffer = new byte[nameLen];
|
---|
172 | readFully(buffer);
|
---|
173 | String name = new String(buffer);
|
---|
174 |
|
---|
175 | entry = createZipEntry(name);
|
---|
176 | entryAtEOF = false;
|
---|
177 | entry.setMethod(method);
|
---|
178 | if ((flags & 8) == 0)
|
---|
179 | {
|
---|
180 | entry.setCrc(crc & 0xffffffffL);
|
---|
181 | entry.setSize(size & 0xffffffffL);
|
---|
182 | entry.setCompressedSize(csize & 0xffffffffL);
|
---|
183 | }
|
---|
184 | entry.setDOSTime(dostime);
|
---|
185 | if (extraLen > 0)
|
---|
186 | {
|
---|
187 | byte[] extra = new byte[extraLen];
|
---|
188 | readFully(extra);
|
---|
189 | entry.setExtra(extra);
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (method == ZipOutputStream.DEFLATED && avail > 0)
|
---|
193 | {
|
---|
194 | System.arraycopy(buf, len - avail, buf, 0, avail);
|
---|
195 | len = avail;
|
---|
196 | avail = 0;
|
---|
197 | inf.setInput(buf, 0, len);
|
---|
198 | }
|
---|
199 | return entry;
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void readDataDescr() throws IOException
|
---|
203 | {
|
---|
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);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Closes the current zip entry and moves to the next one.
|
---|
215 | */
|
---|
216 | public void closeEntry() throws IOException
|
---|
217 | {
|
---|
218 | if (crc == null)
|
---|
219 | throw new IOException("Stream closed.");
|
---|
220 | if (entry == null)
|
---|
221 | return;
|
---|
222 |
|
---|
223 | if (method == ZipOutputStream.DEFLATED)
|
---|
224 | {
|
---|
225 | if ((flags & 8) != 0)
|
---|
226 | {
|
---|
227 | /* We don't know how much we must skip, read until end. */
|
---|
228 | byte[] tmp = new byte[2048];
|
---|
229 | while (read(tmp) > 0)
|
---|
230 | ;
|
---|
231 | /* read will close this entry */
|
---|
232 | return;
|
---|
233 | }
|
---|
234 | csize -= inf.getTotalIn();
|
---|
235 | avail = inf.getRemaining();
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (avail > csize && csize >= 0)
|
---|
239 | avail -= csize;
|
---|
240 | else
|
---|
241 | {
|
---|
242 | csize -= avail;
|
---|
243 | avail = 0;
|
---|
244 | while (csize != 0)
|
---|
245 | {
|
---|
246 | long skipped = in.skip(csize & 0xffffffffL);
|
---|
247 | if (skipped <= 0)
|
---|
248 | throw new ZipException("zip archive ends early.");
|
---|
249 | csize -= skipped;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | size = 0;
|
---|
254 | crc.reset();
|
---|
255 | if (method == ZipOutputStream.DEFLATED)
|
---|
256 | inf.reset();
|
---|
257 | entry = null;
|
---|
258 | entryAtEOF = true;
|
---|
259 | }
|
---|
260 |
|
---|
261 | public int available() throws IOException
|
---|
262 | {
|
---|
263 | return entryAtEOF ? 0 : 1;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
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.
|
---|
271 | */
|
---|
272 | public int read() throws IOException
|
---|
273 | {
|
---|
274 | byte[] b = new byte[1];
|
---|
275 | if (read(b, 0, 1) <= 0)
|
---|
276 | return -1;
|
---|
277 | return b[0] & 0xff;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
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.
|
---|
286 | */
|
---|
287 | public int read(byte[] b, int off, int len) throws IOException
|
---|
288 | {
|
---|
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)
|
---|
297 | {
|
---|
298 | case ZipOutputStream.DEFLATED:
|
---|
299 | len = super.read(b, off, len);
|
---|
300 | if (len < 0)
|
---|
301 | {
|
---|
302 | if (!inf.finished())
|
---|
303 | throw new ZipException("Inflater not finished!?");
|
---|
304 | avail = inf.getRemaining();
|
---|
305 | if ((flags & 8) != 0)
|
---|
306 | readDataDescr();
|
---|
307 |
|
---|
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;
|
---|
313 | }
|
---|
314 | break;
|
---|
315 |
|
---|
316 | case ZipOutputStream.STORED:
|
---|
317 |
|
---|
318 | if (len > csize && csize >= 0)
|
---|
319 | len = csize;
|
---|
320 |
|
---|
321 | len = readBuf(b, off, len);
|
---|
322 | if (len > 0)
|
---|
323 | {
|
---|
324 | csize -= len;
|
---|
325 | size -= len;
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (csize == 0)
|
---|
329 | finished = true;
|
---|
330 | else if (len < 0)
|
---|
331 | throw new ZipException("EOF in stored block");
|
---|
332 | break;
|
---|
333 | }
|
---|
334 |
|
---|
335 | if (len > 0)
|
---|
336 | crc.update(b, off, len);
|
---|
337 |
|
---|
338 | if (finished)
|
---|
339 | {
|
---|
340 | if ((crc.getValue() & 0xffffffffL) != entry.getCrc())
|
---|
341 | throw new ZipException("CRC mismatch");
|
---|
342 | crc.reset();
|
---|
343 | entry = null;
|
---|
344 | entryAtEOF = true;
|
---|
345 | }
|
---|
346 | return len;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Closes the zip file.
|
---|
351 | * @exception IOException if a i/o error occured.
|
---|
352 | */
|
---|
353 | public void close() throws IOException
|
---|
354 | {
|
---|
355 | super.close();
|
---|
356 | crc = null;
|
---|
357 | entry = null;
|
---|
358 | entryAtEOF = true;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
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.
|
---|
365 | */
|
---|
366 | protected ZipEntry createZipEntry(String name)
|
---|
367 | {
|
---|
368 | return new ZipEntry(name);
|
---|
369 | }
|
---|
370 | }
|
---|