1 /*
2 * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4 *
5 *
6 *
7 *
8 *
9 *
10 *
11 *
12 *
13 *
14 *
15 *
16 *
17 *
18 *
19 *
20 *
21 *
22 *
23 *
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;
32 import java.nio.charset.Charset;
33 import java.nio.charset.StandardCharsets;
34 import static java.util.zip.ZipConstants64.*;
35 import static java.util.zip.ZipUtils.*;
36
37 /**
38 * This class implements an input stream filter for reading files in the
39 * ZIP file format. Includes support for both compressed and uncompressed
40 * entries.
41 *
42 * @author David Connelly
43 */
44 public
45 class ZipInputStream extends InflaterInputStream implements ZipConstants {
46 private ZipEntry entry;
47 private int flag;
48 private CRC32 crc = new CRC32();
49 private long remaining;
50 private byte[] tmpbuf = new byte[512];
51
52 private static final int STORED = ZipEntry.STORED;
53 private static final int DEFLATED = ZipEntry.DEFLATED;
54
55 private boolean closed = false;
56 // this flag is set to true after EOF has reached for
57 // one entry
58 private boolean entryEOF = false;
59
60 private ZipCoder zc;
61
62 /**
63 * Check to make sure that this stream has not been closed
64 */
65 private void ensureOpen() throws IOException {
66 if (closed) {
67 throw new IOException("Stream closed");
68 }
69 }
70
71 /**
72 * Creates a new ZIP input stream.
73 *
74 * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
75 * decode the entry names.
76 *
77 * @param in the actual input stream
78 */
79 public ZipInputStream(InputStream in) {
80 this(in, StandardCharsets.UTF_8);
81 }
82
83 /**
84 * Creates a new ZIP input stream.
85 *
86 * @param in the actual input stream
87 *
88 * @param charset
89 * The {@linkplain java.nio.charset.Charset charset} to be
90 * used to decode the ZIP entry name (ignored if the
91 * <a href="package-summary.html#lang_encoding"> language
92 * encoding bit</a> of the ZIP entry's general purpose bit
93 * flag is set).
94 *
95 * @since 1.7
96 */
97 public ZipInputStream(InputStream in, Charset charset) {
98 super(new PushbackInputStream(in, 512), new Inflater(true), 512);
99 usesDefaultInflater = true;
100 if(in == null) {
101 throw new NullPointerException("in is null");
102 }
103 if (charset == null)
104 throw new NullPointerException("charset is null");
105 this.zc = ZipCoder.get(charset);
106 }
107
108 /**
109 * Reads the next ZIP file entry and positions the stream at the
110 * beginning of the entry data.
111 * @return the next ZIP file entry, or null if there are no more entries
112 * @exception ZipException if a ZIP file error has occurred
113 * @exception IOException if an I/O error has occurred
114 */
115 public ZipEntry getNextEntry() throws IOException {
116 ensureOpen();
117 if (entry != null) {
118 closeEntry();
119 }
120 crc.reset();
121 inf.reset();
122 if ((entry = readLOC()) == null) {
123 return null;
124 }
125 if (entry.method == STORED) {
126 remaining = entry.size;
127 }
128 entryEOF = false;
129 return entry;
130 }
131
132 /**
133 * Closes the current ZIP entry and positions the stream for reading the
134 * next entry.
135 * @exception ZipException if a ZIP file error has occurred
136 * @exception IOException if an I/O error has occurred
137 */
138 public void closeEntry() throws IOException {
139 ensureOpen();
140 while (read(tmpbuf, 0, tmpbuf.length) != -1) ;
141 entryEOF = true;
142 }
143
144 /**
145 * Returns 0 after EOF has reached for the current entry data,
146 * otherwise always return 1.
147 * <p>
148 * Programs should not count on this method to return the actual number
149 * of bytes that could be read without blocking.
150 *
151 * @return 1 before EOF and 0 after EOF has reached for current entry.
152 * @exception IOException if an I/O error occurs.
153 *
154 */
155 public int available() throws IOException {
156 ensureOpen();
157 if (entryEOF) {
158 return 0;
159 } else {
160 return 1;
161 }
162 }
163
164 /**
165 * Reads from the current ZIP entry into an array of bytes.
166 * If <code>len</code> is not zero, the method
167 * blocks until some input is available; otherwise, no
168 * bytes are read and <code>0</code> is returned.
169 * @param b the buffer into which the data is read
170 * @param off the start offset in the destination array <code>b</code>
171 * @param len the maximum number of bytes read
172 * @return the actual number of bytes read, or -1 if the end of the
173 * entry is reached
174 * @exception NullPointerException if <code>b</code> is <code>null</code>.
175 * @exception IndexOutOfBoundsException if <code>off</code> is negative,
176 * <code>len</code> is negative, or <code>len</code> is greater than
177 * <code>b.length - off</code>
178 * @exception ZipException if a ZIP file error has occurred
179 * @exception IOException if an I/O error has occurred
180 */
181 public int read(byte[] b, int off, int len) throws IOException {
182 ensureOpen();
183 if (off < 0 || len < 0 || off > b.length - len) {
184 throw new IndexOutOfBoundsException();
185 } else if (len == 0) {
186 return 0;
187 }
188
189 if (entry == null) {
190 return -1;
191 }
192 switch (entry.method) {
193 case DEFLATED:
194 len = super.read(b, off, len);
195 if (len == -1) {
196 readEnd(entry);
197 entryEOF = true;
198 entry = null;
199 } else {
200 crc.update(b, off, len);
201 }
202 return len;
203 case STORED:
204 if (remaining <= 0) {
205 entryEOF = true;
206 entry = null;
207 return -1;
208 }
209 if (len > remaining) {
210 len = (int)remaining;
211 }
212 len = in.read(b, off, len);
213 if (len == -1) {
214 throw new ZipException("unexpected EOF");
215 }
216 crc.update(b, off, len);
217 remaining -= len;
218 if (remaining == 0 && entry.crc != crc.getValue()) {
219 throw new ZipException(
220 "invalid entry CRC (expected 0x" + Long.toHexString(entry.crc) +
221 " but got 0x" + Long.toHexString(crc.getValue()) + ")");
222 }
223 return len;
224 default:
225 throw new ZipException("invalid compression method");
226 }
227 }
228
229 /**
230 * Skips specified number of bytes in the current ZIP entry.
231 * @param n the number of bytes to skip
232 * @return the actual number of bytes skipped
233 * @exception ZipException if a ZIP file error has occurred
234 * @exception IOException if an I/O error has occurred
235 * @exception IllegalArgumentException if {@code n < 0}
236 */
237 public long skip(long n) throws IOException {
238 if (n < 0) {
239 throw new IllegalArgumentException("negative skip length");
240 }
241 ensureOpen();
242 int max = (int)Math.min(n, Integer.MAX_VALUE);
243 int total = 0;
244 while (total < max) {
245 int len = max - total;
246 if (len > tmpbuf.length) {
247 len = tmpbuf.length;
248 }
249 len = read(tmpbuf, 0, len);
250 if (len == -1) {
251 entryEOF = true;
252 break;
253 }
254 total += len;
255 }
256 return total;
257 }
258
259 /**
260 * Closes this input stream and releases any system resources associated
261 * with the stream.
262 * @exception IOException if an I/O error has occurred
263 */
264 public void close() throws IOException {
265 if (!closed) {
266 super.close();
267 closed = true;
268 }
269 }
270
271 private byte[] b = new byte[256];
272
273 /*
274 * Reads local file (LOC) header for next entry.
275 */
276 private ZipEntry readLOC() throws IOException {
277 try {
278 readFully(tmpbuf, 0, LOCHDR);
279 } catch (EOFException e) {
280 return null;
281 }
282 if (get32(tmpbuf, 0) != LOCSIG) {
283 return null;
284 }
285 // get flag first, we need check EFS.
286 flag = get16(tmpbuf, LOCFLG);
287 // get the entry name and create the ZipEntry first
288 int len = get16(tmpbuf, LOCNAM);
289 int blen = b.length;
290 if (len > blen) {
291 do {
292 blen = blen * 2;
293 } while (len > blen);
294 b = new byte[blen];
295 }
296 readFully(b, 0, len);
297 // Force to use UTF-8 if the EFS bit is ON, even the cs is NOT UTF-8
298 ZipEntry e = createZipEntry(((flag & EFS) != 0)
299 ? zc.toStringUTF8(b, len)
300 : zc.toString(b, len));
301 // now get the remaining fields for the entry
302 if ((flag & 1) == 1) {
303 throw new ZipException("encrypted ZIP entry not supported");
304 }
305 e.method = get16(tmpbuf, LOCHOW);
306 e.xdostime = get32(tmpbuf, LOCTIM);
307 if ((flag & 8) == 8) {
308 /* "Data Descriptor" present */
309 if (e.method != DEFLATED) {
310 throw new ZipException(
311 "only DEFLATED entries can have EXT descriptor");
312 }
313 } else {
314 e.crc = get32(tmpbuf, LOCCRC);
315 e.csize = get32(tmpbuf, LOCSIZ);
316 e.size = get32(tmpbuf, LOCLEN);
317 }
318 len = get16(tmpbuf, LOCEXT);
319 if (len > 0) {
320 byte[] extra = new byte[len];
321 readFully(extra, 0, len);
322 e.setExtra0(extra,
323 e.csize == ZIP64_MAGICVAL || e.size == ZIP64_MAGICVAL);
324 }
325 return e;
326 }
327
328 /**
329 * Creates a new <code>ZipEntry</code> object for the specified
330 * entry name.
331 *
332 * @param name the ZIP file entry name
333 * @return the ZipEntry just created
334 */
335 protected ZipEntry createZipEntry(String name) {
336 return new ZipEntry(name);
337 }
338
339 /*
340 * Reads end of deflated entry as well as EXT descriptor if present.
341 */
342 private void readEnd(ZipEntry e) throws IOException {
343 int n = inf.getRemaining();
344 if (n > 0) {
345 ((PushbackInputStream)in).unread(buf, len - n, n);
346 }
347 if ((flag & 8) == 8) {
348 /* "Data Descriptor" present */
349 if (inf.getBytesWritten() > ZIP64_MAGICVAL ||
350 inf.getBytesRead() > ZIP64_MAGICVAL) {
351 // ZIP64 format
352 readFully(tmpbuf, 0, ZIP64_EXTHDR);
353 long sig = get32(tmpbuf, 0);
354 if (sig != EXTSIG) { // no EXTSIG present
355 e.crc = sig;
356 e.csize = get64(tmpbuf, ZIP64_EXTSIZ - ZIP64_EXTCRC);
357 e.size = get64(tmpbuf, ZIP64_EXTLEN - ZIP64_EXTCRC);
358 ((PushbackInputStream)in).unread(
359 tmpbuf, ZIP64_EXTHDR - ZIP64_EXTCRC - 1, ZIP64_EXTCRC);
360 } else {
361 e.crc = get32(tmpbuf, ZIP64_EXTCRC);
362 e.csize = get64(tmpbuf, ZIP64_EXTSIZ);
363 e.size = get64(tmpbuf, ZIP64_EXTLEN);
364 }
365 } else {
366 readFully(tmpbuf, 0, EXTHDR);
367 long sig = get32(tmpbuf, 0);
368 if (sig != EXTSIG) { // no EXTSIG present
369 e.crc = sig;
370 e.csize = get32(tmpbuf, EXTSIZ - EXTCRC);
371 e.size = get32(tmpbuf, EXTLEN - EXTCRC);
372 ((PushbackInputStream)in).unread(
373 tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC);
374 } else {
375 e.crc = get32(tmpbuf, EXTCRC);
376 e.csize = get32(tmpbuf, EXTSIZ);
377 e.size = get32(tmpbuf, EXTLEN);
378 }
379 }
380 }
381 if (e.size != inf.getBytesWritten()) {
382 throw new ZipException(
383 "invalid entry size (expected " + e.size +
384 " but got " + inf.getBytesWritten() + " bytes)");
385 }
386 if (e.csize != inf.getBytesRead()) {
387 throw new ZipException(
388 "invalid entry compressed size (expected " + e.csize +
389 " but got " + inf.getBytesRead() + " bytes)");
390 }
391 if (e.crc != crc.getValue()) {
392 throw new ZipException(
393 "invalid entry CRC (expected 0x" + Long.toHexString(e.crc) +
394 " but got 0x" + Long.toHexString(crc.getValue()) + ")");
395 }
396 }
397
398 /*
399 * Reads bytes, blocking until all bytes are read.
400 */
401 private void readFully(byte[] b, int off, int len) throws IOException {
402 while (len > 0) {
403 int n = in.read(b, off, len);
404 if (n == -1) {
405 throw new EOFException();
406 }
407 off += n;
408 len -= n;
409 }
410 }
411
412 }
413