2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package java.util.zip;
21 import java.io.EOFException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.PushbackInputStream;
25 import java.util.jar.Attributes;
26 import java.util.jar.JarEntry;
28 import org.apache.harmony.archive.internal.nls.Messages;
29 import org.apache.harmony.luni.util.Util;
32 * This class provides an implementation of {@code FilterInputStream} that
33 * uncompresses data from a <i>ZIP-archive</i> input stream.
35 * A <i>ZIP-archive</i> is a collection of compressed (or uncompressed) files -
36 * the so called ZIP entries. Therefore when reading from a {@code
37 * ZipInputStream} first the entry's attributes will be retrieved with {@code
38 * getNextEntry} before its data is read.
41 * While {@code InflaterInputStream} can read a compressed <i>ZIP-archive</i>
42 * entry, this extension can read uncompressed entries as well.
45 * Use {@code ZipFile} if you can access the archive as a file directly.
52 public class ZipInputStream extends InflaterInputStream implements ZipConstants {
53 static final int DEFLATED = 8;
55 static final int STORED = 0;
57 static final int ZIPDataDescriptorFlag = 8;
59 static final int ZIPLocalHeaderVersionNeeded = 20;
61 private boolean zipClosed = false;
63 private boolean entriesEnd = false;
65 private boolean hasDD = false;
67 private int entryIn = 0;
69 private int inRead, lastRead = 0;
71 ZipEntry currentEntry;
73 private final byte[] hdrBuf = new byte[LOCHDR - LOCVER];
75 private final CRC32 crc = new CRC32();
77 private byte[] nameBuf = new byte[256];
79 private char[] charBuf = new char[256];
82 * Constructs a new {@code ZipInputStream} from the specified input stream.
85 * the input stream to representing a ZIP archive.
88 public ZipInputStream(InputStream stream) {
89 super(new PushbackInputStream(stream, BUF_SIZE), new Inflater(true));
91 throw new NullPointerException();
96 * Closes this {@code ZipInputStream}.
99 * if an {@code IOException} occurs.
103 public void close() throws IOException {
104 if (zipClosed != true) {
105 closeEntry(); // Close the current entry
112 * Closes the current ZIP entry and positions to read the next entry.
114 * @throws IOException
115 * if an {@code IOException} occurs.
118 public void closeEntry() throws IOException {
120 throw new IOException(Messages.getString("archive.1E")); //$NON-NLS-1$
122 if (currentEntry == null) {
125 if (currentEntry instanceof java.util.jar.JarEntry) {
126 Attributes temp = ((JarEntry) currentEntry).getAttributes();
127 if (temp != null && temp.containsKey("hidden")) { //$NON-NLS-1$
131 // Ensure all entry bytes are read
132 skip(Long.MAX_VALUE);
134 if (currentEntry.compressionMethod == DEFLATED) {
135 inB = inf.getTotalIn();
136 out = inf.getTotalOut();
142 // Pushback any required bytes
143 if ((diff = entryIn - inB) != 0) {
144 ((PushbackInputStream) in).unread(buf, len - diff, diff);
148 in.read(hdrBuf, 0, EXTHDR);
149 if (getLong(hdrBuf, 0) != EXTSIG) {
150 throw new ZipException(Messages.getString("archive.1F")); //$NON-NLS-1$
152 currentEntry.crc = getLong(hdrBuf, EXTCRC);
153 currentEntry.compressedSize = getLong(hdrBuf, EXTSIZ);
154 currentEntry.size = getLong(hdrBuf, EXTLEN);
156 if (currentEntry.crc != crc.getValue()) {
157 throw new ZipException(Messages.getString("archive.20")); //$NON-NLS-1$
159 if (currentEntry.compressedSize != inB || currentEntry.size != out) {
160 throw new ZipException(Messages.getString("archive.21")); //$NON-NLS-1$
164 lastRead = inRead = entryIn = len = 0;
170 * Reads the next entry from this {@code ZipInputStream}.
172 * @return the next {@code ZipEntry} contained in the input stream.
173 * @throws IOException
174 * if the stream is not positioned at the beginning of an entry
175 * or if an other {@code IOException} occurs.
179 public ZipEntry getNextEntry() throws IOException {
180 if (currentEntry != null) {
187 int x = 0, count = 0;
189 count += x = in.read(hdrBuf, count, 4 - count);
194 long hdr = getLong(hdrBuf, 0);
203 // Read the local header
205 while (count != (LOCHDR - LOCVER)) {
206 count += x = in.read(hdrBuf, count, (LOCHDR - LOCVER) - count);
208 throw new EOFException();
211 int version = getShort(hdrBuf, 0) & 0xff;
212 if (version > ZIPLocalHeaderVersionNeeded) {
213 throw new ZipException(Messages.getString("archive.22")); //$NON-NLS-1$
215 int flags = getShort(hdrBuf, LOCFLG - LOCVER);
216 hasDD = ((flags & ZIPDataDescriptorFlag) == ZIPDataDescriptorFlag);
217 int cetime = getShort(hdrBuf, LOCTIM - LOCVER);
218 int cemodDate = getShort(hdrBuf, LOCTIM - LOCVER + 2);
219 int cecompressionMethod = getShort(hdrBuf, LOCHOW - LOCVER);
220 long cecrc = 0, cecompressedSize = 0, cesize = -1;
222 cecrc = getLong(hdrBuf, LOCCRC - LOCVER);
223 cecompressedSize = getLong(hdrBuf, LOCSIZ - LOCVER);
224 cesize = getLong(hdrBuf, LOCLEN - LOCVER);
226 int flen = getShort(hdrBuf, LOCNAM - LOCVER);
228 throw new ZipException(Messages.getString("archive.23")); //$NON-NLS-1$
230 int elen = getShort(hdrBuf, LOCEXT - LOCVER);
233 if (flen > nameBuf.length) {
234 nameBuf = new byte[flen];
235 charBuf = new char[flen];
237 while (count != flen) {
238 count += x = in.read(nameBuf, count, flen - count);
240 throw new EOFException();
243 currentEntry = createZipEntry(Util.convertUTF8WithBuf(nameBuf, charBuf,
245 currentEntry.time = cetime;
246 currentEntry.modDate = cemodDate;
247 currentEntry.setMethod(cecompressionMethod);
249 currentEntry.setCrc(cecrc);
250 currentEntry.setSize(cesize);
251 currentEntry.setCompressedSize(cecompressedSize);
255 byte[] e = new byte[elen];
256 while (count != elen) {
257 count += x = in.read(e, count, elen - count);
259 throw new EOFException();
262 currentEntry.setExtra(e);
267 /* Read 4 bytes from the buffer and store it as an int */
270 public int read(byte[] buffer, int start, int length) throws IOException {
272 throw new IOException(Messages.getString("archive.1E")); //$NON-NLS-1$
274 if (inf.finished() || currentEntry == null) {
277 // avoid int overflow, check null buffer
278 if (start <= buffer.length && length >= 0 && start >= 0
279 && buffer.length - start >= length) {
280 if (currentEntry.compressionMethod == STORED) {
281 int csize = (int) currentEntry.size;
282 if (inRead >= csize) {
285 if (lastRead >= len) {
287 if ((len = in.read(buf)) == -1) {
292 // BEGIN android-changed
293 int toRead = length > (len - lastRead) ? len - lastRead : length;
294 // END android-changed
295 if ((csize - inRead) < toRead) {
296 toRead = csize - inRead;
298 System.arraycopy(buf, lastRead, buffer, start, toRead);
301 crc.update(buffer, start, toRead);
304 if (inf.needsInput()) {
312 read = inf.inflate(buffer, start, length);
313 } catch (DataFormatException e) {
314 throw new ZipException(e.getMessage());
316 if (read == 0 && inf.finished()) {
319 crc.update(buffer, start, read);
322 throw new ArrayIndexOutOfBoundsException();
326 * Skips up to the specified number of bytes in the current ZIP entry.
329 * the number of bytes to skip.
330 * @return the number of bytes skipped.
331 * @throws IOException
332 * if an {@code IOException} occurs.
336 public long skip(long value) throws IOException {
339 byte[] b = new byte[1024];
340 while (skipped != value) {
341 long rem = value - skipped;
342 int x = read(b, 0, (int) (b.length > rem ? rem : b.length));
350 throw new IllegalArgumentException();
354 * Returns 0 if the {@code EOF} has been reached, otherwise returns 1.
356 * @return 0 after {@code EOF} of current entry, 1 otherwise.
357 * @throws IOException
358 * if an IOException occurs.
362 public int available() throws IOException {
364 throw new IOException(Messages.getString("archive.1E")); //$NON-NLS-1$
366 if (currentEntry == null) {
369 if (currentEntry.compressionMethod == STORED) {
370 if (inRead >= currentEntry.size) {
374 if (inf.finished()) {
382 * creates a {@link ZipEntry } with the given name.
385 * the name of the entry.
386 * @return the created {@code ZipEntry}.
389 protected ZipEntry createZipEntry(String name) {
390 return new ZipEntry(name);
393 private int getShort(byte[] buffer, int off) {
394 return (buffer[off] & 0xFF) | ((buffer[off + 1] & 0xFF) << 8);
397 private long getLong(byte[] buffer, int off) {
399 l |= (buffer[off] & 0xFF);
400 l |= (buffer[off + 1] & 0xFF) << 8;
401 l |= (buffer[off + 2] & 0xFF) << 16;
402 l |= ((long) (buffer[off + 3] & 0xFF)) << 24;