2
|
1 |
/*
|
2438
|
2 |
* Copyright 1996-2009 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.util.zip;
|
|
27 |
|
|
28 |
import java.io.InputStream;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.EOFException;
|
|
31 |
import java.io.PushbackInputStream;
|
2438
|
32 |
import static java.util.zip.ZipConstants64.*;
|
2
|
33 |
|
|
34 |
/**
|
|
35 |
* This class implements an input stream filter for reading files in the
|
|
36 |
* ZIP file format. Includes support for both compressed and uncompressed
|
|
37 |
* entries.
|
|
38 |
*
|
|
39 |
* @author David Connelly
|
|
40 |
*/
|
|
41 |
public
|
|
42 |
class ZipInputStream extends InflaterInputStream implements ZipConstants {
|
|
43 |
private ZipEntry entry;
|
|
44 |
private int flag;
|
|
45 |
private CRC32 crc = new CRC32();
|
|
46 |
private long remaining;
|
|
47 |
private byte[] tmpbuf = new byte[512];
|
|
48 |
|
|
49 |
private static final int STORED = ZipEntry.STORED;
|
|
50 |
private static final int DEFLATED = ZipEntry.DEFLATED;
|
|
51 |
|
|
52 |
private boolean closed = false;
|
|
53 |
// this flag is set to true after EOF has reached for
|
|
54 |
// one entry
|
|
55 |
private boolean entryEOF = false;
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Check to make sure that this stream has not been closed
|
|
59 |
*/
|
|
60 |
private void ensureOpen() throws IOException {
|
|
61 |
if (closed) {
|
|
62 |
throw new IOException("Stream closed");
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Creates a new ZIP input stream.
|
|
68 |
* @param in the actual input stream
|
|
69 |
*/
|
|
70 |
public ZipInputStream(InputStream in) {
|
|
71 |
super(new PushbackInputStream(in, 512), new Inflater(true), 512);
|
|
72 |
usesDefaultInflater = true;
|
|
73 |
if(in == null) {
|
|
74 |
throw new NullPointerException("in is null");
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Reads the next ZIP file entry and positions the stream at the
|
|
80 |
* beginning of the entry data.
|
|
81 |
* @return the next ZIP file entry, or null if there are no more entries
|
|
82 |
* @exception ZipException if a ZIP file error has occurred
|
|
83 |
* @exception IOException if an I/O error has occurred
|
|
84 |
*/
|
|
85 |
public ZipEntry getNextEntry() throws IOException {
|
|
86 |
ensureOpen();
|
|
87 |
if (entry != null) {
|
|
88 |
closeEntry();
|
|
89 |
}
|
|
90 |
crc.reset();
|
|
91 |
inf.reset();
|
|
92 |
if ((entry = readLOC()) == null) {
|
|
93 |
return null;
|
|
94 |
}
|
|
95 |
if (entry.method == STORED) {
|
|
96 |
remaining = entry.size;
|
|
97 |
}
|
|
98 |
entryEOF = false;
|
|
99 |
return entry;
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Closes the current ZIP entry and positions the stream for reading the
|
|
104 |
* next entry.
|
|
105 |
* @exception ZipException if a ZIP file error has occurred
|
|
106 |
* @exception IOException if an I/O error has occurred
|
|
107 |
*/
|
|
108 |
public void closeEntry() throws IOException {
|
|
109 |
ensureOpen();
|
|
110 |
while (read(tmpbuf, 0, tmpbuf.length) != -1) ;
|
|
111 |
entryEOF = true;
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Returns 0 after EOF has reached for the current entry data,
|
|
116 |
* otherwise always return 1.
|
|
117 |
* <p>
|
|
118 |
* Programs should not count on this method to return the actual number
|
|
119 |
* of bytes that could be read without blocking.
|
|
120 |
*
|
|
121 |
* @return 1 before EOF and 0 after EOF has reached for current entry.
|
|
122 |
* @exception IOException if an I/O error occurs.
|
|
123 |
*
|
|
124 |
*/
|
|
125 |
public int available() throws IOException {
|
|
126 |
ensureOpen();
|
|
127 |
if (entryEOF) {
|
|
128 |
return 0;
|
|
129 |
} else {
|
|
130 |
return 1;
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Reads from the current ZIP entry into an array of bytes.
|
|
136 |
* If <code>len</code> is not zero, the method
|
|
137 |
* blocks until some input is available; otherwise, no
|
|
138 |
* bytes are read and <code>0</code> is returned.
|
|
139 |
* @param b the buffer into which the data is read
|
|
140 |
* @param off the start offset in the destination array <code>b</code>
|
|
141 |
* @param len the maximum number of bytes read
|
|
142 |
* @return the actual number of bytes read, or -1 if the end of the
|
|
143 |
* entry is reached
|
|
144 |
* @exception NullPointerException If <code>b</code> is <code>null</code>.
|
|
145 |
* @exception IndexOutOfBoundsException If <code>off</code> is negative,
|
|
146 |
* <code>len</code> is negative, or <code>len</code> is greater than
|
|
147 |
* <code>b.length - off</code>
|
|
148 |
* @exception ZipException if a ZIP file error has occurred
|
|
149 |
* @exception IOException if an I/O error has occurred
|
|
150 |
*/
|
|
151 |
public int read(byte[] b, int off, int len) throws IOException {
|
|
152 |
ensureOpen();
|
|
153 |
if (off < 0 || len < 0 || off > b.length - len) {
|
|
154 |
throw new IndexOutOfBoundsException();
|
|
155 |
} else if (len == 0) {
|
|
156 |
return 0;
|
|
157 |
}
|
|
158 |
|
|
159 |
if (entry == null) {
|
|
160 |
return -1;
|
|
161 |
}
|
|
162 |
switch (entry.method) {
|
|
163 |
case DEFLATED:
|
|
164 |
len = super.read(b, off, len);
|
|
165 |
if (len == -1) {
|
|
166 |
readEnd(entry);
|
|
167 |
entryEOF = true;
|
|
168 |
entry = null;
|
|
169 |
} else {
|
|
170 |
crc.update(b, off, len);
|
|
171 |
}
|
|
172 |
return len;
|
|
173 |
case STORED:
|
|
174 |
if (remaining <= 0) {
|
|
175 |
entryEOF = true;
|
|
176 |
entry = null;
|
|
177 |
return -1;
|
|
178 |
}
|
|
179 |
if (len > remaining) {
|
|
180 |
len = (int)remaining;
|
|
181 |
}
|
|
182 |
len = in.read(b, off, len);
|
|
183 |
if (len == -1) {
|
|
184 |
throw new ZipException("unexpected EOF");
|
|
185 |
}
|
|
186 |
crc.update(b, off, len);
|
|
187 |
remaining -= len;
|
|
188 |
if (remaining == 0 && entry.crc != crc.getValue()) {
|
|
189 |
throw new ZipException(
|
|
190 |
"invalid entry CRC (expected 0x" + Long.toHexString(entry.crc) +
|
|
191 |
" but got 0x" + Long.toHexString(crc.getValue()) + ")");
|
|
192 |
}
|
|
193 |
return len;
|
|
194 |
default:
|
|
195 |
throw new ZipException("invalid compression method");
|
|
196 |
}
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* Skips specified number of bytes in the current ZIP entry.
|
|
201 |
* @param n the number of bytes to skip
|
|
202 |
* @return the actual number of bytes skipped
|
|
203 |
* @exception ZipException if a ZIP file error has occurred
|
|
204 |
* @exception IOException if an I/O error has occurred
|
|
205 |
* @exception IllegalArgumentException if n < 0
|
|
206 |
*/
|
|
207 |
public long skip(long n) throws IOException {
|
|
208 |
if (n < 0) {
|
|
209 |
throw new IllegalArgumentException("negative skip length");
|
|
210 |
}
|
|
211 |
ensureOpen();
|
|
212 |
int max = (int)Math.min(n, Integer.MAX_VALUE);
|
|
213 |
int total = 0;
|
|
214 |
while (total < max) {
|
|
215 |
int len = max - total;
|
|
216 |
if (len > tmpbuf.length) {
|
|
217 |
len = tmpbuf.length;
|
|
218 |
}
|
|
219 |
len = read(tmpbuf, 0, len);
|
|
220 |
if (len == -1) {
|
|
221 |
entryEOF = true;
|
|
222 |
break;
|
|
223 |
}
|
|
224 |
total += len;
|
|
225 |
}
|
|
226 |
return total;
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Closes this input stream and releases any system resources associated
|
|
231 |
* with the stream.
|
|
232 |
* @exception IOException if an I/O error has occurred
|
|
233 |
*/
|
|
234 |
public void close() throws IOException {
|
|
235 |
if (!closed) {
|
|
236 |
super.close();
|
|
237 |
closed = true;
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|
|
241 |
private byte[] b = new byte[256];
|
|
242 |
|
|
243 |
/*
|
|
244 |
* Reads local file (LOC) header for next entry.
|
|
245 |
*/
|
|
246 |
private ZipEntry readLOC() throws IOException {
|
|
247 |
try {
|
|
248 |
readFully(tmpbuf, 0, LOCHDR);
|
|
249 |
} catch (EOFException e) {
|
|
250 |
return null;
|
|
251 |
}
|
|
252 |
if (get32(tmpbuf, 0) != LOCSIG) {
|
|
253 |
return null;
|
|
254 |
}
|
|
255 |
// get the entry name and create the ZipEntry first
|
|
256 |
int len = get16(tmpbuf, LOCNAM);
|
|
257 |
int blen = b.length;
|
|
258 |
if (len > blen) {
|
|
259 |
do
|
|
260 |
blen = blen * 2;
|
|
261 |
while (len > blen);
|
|
262 |
b = new byte[blen];
|
|
263 |
}
|
|
264 |
readFully(b, 0, len);
|
|
265 |
ZipEntry e = createZipEntry(getUTF8String(b, 0, len));
|
|
266 |
// now get the remaining fields for the entry
|
|
267 |
flag = get16(tmpbuf, LOCFLG);
|
|
268 |
if ((flag & 1) == 1) {
|
|
269 |
throw new ZipException("encrypted ZIP entry not supported");
|
|
270 |
}
|
|
271 |
e.method = get16(tmpbuf, LOCHOW);
|
|
272 |
e.time = get32(tmpbuf, LOCTIM);
|
|
273 |
if ((flag & 8) == 8) {
|
|
274 |
/* "Data Descriptor" present */
|
|
275 |
if (e.method != DEFLATED) {
|
|
276 |
throw new ZipException(
|
|
277 |
"only DEFLATED entries can have EXT descriptor");
|
|
278 |
}
|
|
279 |
} else {
|
|
280 |
e.crc = get32(tmpbuf, LOCCRC);
|
|
281 |
e.csize = get32(tmpbuf, LOCSIZ);
|
|
282 |
e.size = get32(tmpbuf, LOCLEN);
|
|
283 |
}
|
|
284 |
len = get16(tmpbuf, LOCEXT);
|
|
285 |
if (len > 0) {
|
|
286 |
byte[] bb = new byte[len];
|
|
287 |
readFully(bb, 0, len);
|
|
288 |
e.setExtra(bb);
|
2438
|
289 |
// extra fields are in "HeaderID(2)DataSize(2)Data... format
|
|
290 |
if (e.csize == ZIP64_MAGICVAL || e.size == ZIP64_MAGICVAL) {
|
|
291 |
int off = 0;
|
|
292 |
while (off + 4 < len) {
|
|
293 |
int sz = get16(bb, off + 2);
|
|
294 |
if (get16(bb, off) == ZIP64_EXTID) {
|
|
295 |
off += 4;
|
|
296 |
// LOC extra zip64 entry MUST include BOTH original and
|
|
297 |
// compressed file size fields
|
|
298 |
if (sz < 16 || (off + sz) > len ) {
|
|
299 |
// Invalid zip64 extra fields, simply skip. Even it's
|
|
300 |
// rare, it's possible the entry size happens to be
|
|
301 |
// the magic value and it "accidnetly" has some bytes
|
|
302 |
// in extra match the id.
|
|
303 |
return e;
|
|
304 |
}
|
|
305 |
e.size = get64(bb, off);
|
|
306 |
e.csize = get64(bb, off + 8);
|
|
307 |
break;
|
|
308 |
}
|
|
309 |
off += (sz + 4);
|
|
310 |
}
|
|
311 |
}
|
2
|
312 |
}
|
|
313 |
return e;
|
|
314 |
}
|
|
315 |
|
|
316 |
/*
|
|
317 |
* Fetches a UTF8-encoded String from the specified byte array.
|
|
318 |
*/
|
|
319 |
private static String getUTF8String(byte[] b, int off, int len) {
|
|
320 |
// First, count the number of characters in the sequence
|
|
321 |
int count = 0;
|
|
322 |
int max = off + len;
|
|
323 |
int i = off;
|
|
324 |
while (i < max) {
|
|
325 |
int c = b[i++] & 0xff;
|
|
326 |
switch (c >> 4) {
|
|
327 |
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
|
|
328 |
// 0xxxxxxx
|
|
329 |
count++;
|
|
330 |
break;
|
|
331 |
case 12: case 13:
|
|
332 |
// 110xxxxx 10xxxxxx
|
|
333 |
if ((b[i++] & 0xc0) != 0x80) {
|
|
334 |
throw new IllegalArgumentException();
|
|
335 |
}
|
|
336 |
count++;
|
|
337 |
break;
|
|
338 |
case 14:
|
|
339 |
// 1110xxxx 10xxxxxx 10xxxxxx
|
|
340 |
if (((b[i++] & 0xc0) != 0x80) ||
|
|
341 |
((b[i++] & 0xc0) != 0x80)) {
|
|
342 |
throw new IllegalArgumentException();
|
|
343 |
}
|
|
344 |
count++;
|
|
345 |
break;
|
|
346 |
default:
|
|
347 |
// 10xxxxxx, 1111xxxx
|
|
348 |
throw new IllegalArgumentException();
|
|
349 |
}
|
|
350 |
}
|
|
351 |
if (i != max) {
|
|
352 |
throw new IllegalArgumentException();
|
|
353 |
}
|
|
354 |
// Now decode the characters...
|
|
355 |
char[] cs = new char[count];
|
|
356 |
i = 0;
|
|
357 |
while (off < max) {
|
|
358 |
int c = b[off++] & 0xff;
|
|
359 |
switch (c >> 4) {
|
|
360 |
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
|
|
361 |
// 0xxxxxxx
|
|
362 |
cs[i++] = (char)c;
|
|
363 |
break;
|
|
364 |
case 12: case 13:
|
|
365 |
// 110xxxxx 10xxxxxx
|
|
366 |
cs[i++] = (char)(((c & 0x1f) << 6) | (b[off++] & 0x3f));
|
|
367 |
break;
|
|
368 |
case 14:
|
|
369 |
// 1110xxxx 10xxxxxx 10xxxxxx
|
|
370 |
int t = (b[off++] & 0x3f) << 6;
|
|
371 |
cs[i++] = (char)(((c & 0x0f) << 12) | t | (b[off++] & 0x3f));
|
|
372 |
break;
|
|
373 |
default:
|
|
374 |
// 10xxxxxx, 1111xxxx
|
|
375 |
throw new IllegalArgumentException();
|
|
376 |
}
|
|
377 |
}
|
|
378 |
return new String(cs, 0, count);
|
|
379 |
}
|
|
380 |
|
|
381 |
/**
|
|
382 |
* Creates a new <code>ZipEntry</code> object for the specified
|
|
383 |
* entry name.
|
|
384 |
*
|
|
385 |
* @param name the ZIP file entry name
|
|
386 |
* @return the ZipEntry just created
|
|
387 |
*/
|
|
388 |
protected ZipEntry createZipEntry(String name) {
|
|
389 |
return new ZipEntry(name);
|
|
390 |
}
|
|
391 |
|
|
392 |
/*
|
|
393 |
* Reads end of deflated entry as well as EXT descriptor if present.
|
|
394 |
*/
|
|
395 |
private void readEnd(ZipEntry e) throws IOException {
|
|
396 |
int n = inf.getRemaining();
|
|
397 |
if (n > 0) {
|
|
398 |
((PushbackInputStream)in).unread(buf, len - n, n);
|
|
399 |
}
|
|
400 |
if ((flag & 8) == 8) {
|
|
401 |
/* "Data Descriptor" present */
|
2438
|
402 |
if (inf.getBytesWritten() > ZIP64_MAGICVAL ||
|
|
403 |
inf.getBytesRead() > ZIP64_MAGICVAL) {
|
|
404 |
// ZIP64 format
|
|
405 |
readFully(tmpbuf, 0, ZIP64_EXTHDR);
|
|
406 |
long sig = get32(tmpbuf, 0);
|
|
407 |
if (sig != EXTSIG) { // no EXTSIG present
|
|
408 |
e.crc = sig;
|
|
409 |
e.csize = get64(tmpbuf, ZIP64_EXTSIZ - ZIP64_EXTCRC);
|
|
410 |
e.size = get64(tmpbuf, ZIP64_EXTLEN - ZIP64_EXTCRC);
|
|
411 |
((PushbackInputStream)in).unread(
|
|
412 |
tmpbuf, ZIP64_EXTHDR - ZIP64_EXTCRC - 1, ZIP64_EXTCRC);
|
|
413 |
} else {
|
|
414 |
e.crc = get32(tmpbuf, ZIP64_EXTCRC);
|
|
415 |
e.csize = get64(tmpbuf, ZIP64_EXTSIZ);
|
|
416 |
e.size = get64(tmpbuf, ZIP64_EXTLEN);
|
|
417 |
}
|
2
|
418 |
} else {
|
2438
|
419 |
readFully(tmpbuf, 0, EXTHDR);
|
|
420 |
long sig = get32(tmpbuf, 0);
|
|
421 |
if (sig != EXTSIG) { // no EXTSIG present
|
|
422 |
e.crc = sig;
|
|
423 |
e.csize = get32(tmpbuf, EXTSIZ - EXTCRC);
|
|
424 |
e.size = get32(tmpbuf, EXTLEN - EXTCRC);
|
|
425 |
((PushbackInputStream)in).unread(
|
|
426 |
tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC);
|
|
427 |
} else {
|
|
428 |
e.crc = get32(tmpbuf, EXTCRC);
|
|
429 |
e.csize = get32(tmpbuf, EXTSIZ);
|
|
430 |
e.size = get32(tmpbuf, EXTLEN);
|
|
431 |
}
|
2
|
432 |
}
|
|
433 |
}
|
|
434 |
if (e.size != inf.getBytesWritten()) {
|
|
435 |
throw new ZipException(
|
|
436 |
"invalid entry size (expected " + e.size +
|
|
437 |
" but got " + inf.getBytesWritten() + " bytes)");
|
|
438 |
}
|
|
439 |
if (e.csize != inf.getBytesRead()) {
|
|
440 |
throw new ZipException(
|
|
441 |
"invalid entry compressed size (expected " + e.csize +
|
|
442 |
" but got " + inf.getBytesRead() + " bytes)");
|
|
443 |
}
|
|
444 |
if (e.crc != crc.getValue()) {
|
|
445 |
throw new ZipException(
|
|
446 |
"invalid entry CRC (expected 0x" + Long.toHexString(e.crc) +
|
|
447 |
" but got 0x" + Long.toHexString(crc.getValue()) + ")");
|
|
448 |
}
|
|
449 |
}
|
|
450 |
|
|
451 |
/*
|
|
452 |
* Reads bytes, blocking until all bytes are read.
|
|
453 |
*/
|
|
454 |
private void readFully(byte[] b, int off, int len) throws IOException {
|
|
455 |
while (len > 0) {
|
|
456 |
int n = in.read(b, off, len);
|
|
457 |
if (n == -1) {
|
|
458 |
throw new EOFException();
|
|
459 |
}
|
|
460 |
off += n;
|
|
461 |
len -= n;
|
|
462 |
}
|
|
463 |
}
|
|
464 |
|
|
465 |
/*
|
|
466 |
* Fetches unsigned 16-bit value from byte array at specified offset.
|
|
467 |
* The bytes are assumed to be in Intel (little-endian) byte order.
|
|
468 |
*/
|
|
469 |
private static final int get16(byte b[], int off) {
|
|
470 |
return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
|
|
471 |
}
|
|
472 |
|
|
473 |
/*
|
|
474 |
* Fetches unsigned 32-bit value from byte array at specified offset.
|
|
475 |
* The bytes are assumed to be in Intel (little-endian) byte order.
|
|
476 |
*/
|
|
477 |
private static final long get32(byte b[], int off) {
|
2438
|
478 |
return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
|
|
479 |
}
|
|
480 |
|
|
481 |
/*
|
|
482 |
* Fetches signed 64-bit value from byte array at specified offset.
|
|
483 |
* The bytes are assumed to be in Intel (little-endian) byte order.
|
|
484 |
*/
|
|
485 |
private static final long get64(byte b[], int off) {
|
|
486 |
return get32(b, off) | (get32(b, off+4) << 32);
|
2
|
487 |
}
|
|
488 |
}
|