jaroslav@609: /* jaroslav@609: * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved. jaroslav@609: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@609: * jaroslav@609: * This code is free software; you can redistribute it and/or modify it jaroslav@609: * under the terms of the GNU General Public License version 2 only, as jaroslav@609: * published by the Free Software Foundation. Oracle designates this jaroslav@609: * particular file as subject to the "Classpath" exception as provided jaroslav@609: * by Oracle in the LICENSE file that accompanied this code. jaroslav@609: * jaroslav@609: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@609: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@609: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@609: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@609: * accompanied this code). jaroslav@609: * jaroslav@609: * You should have received a copy of the GNU General Public License version jaroslav@609: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@609: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@609: * jaroslav@609: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@609: * or visit www.oracle.com if you need additional information or have any jaroslav@609: * questions. jaroslav@609: */ jaroslav@609: jaroslav@694: package org.apidesign.bck2brwsr.emul.zip; jaroslav@609: jaroslav@694: import java.util.zip.*; jaroslav@609: import java.io.InputStream; jaroslav@609: import java.io.IOException; jaroslav@609: import java.io.EOFException; jaroslav@609: import java.io.PushbackInputStream; jaroslav@694: import static org.apidesign.bck2brwsr.emul.zip.ZipConstants64.*; jaroslav@694: import static java.util.zip.ZipInputStream.*; jaroslav@609: jaroslav@609: /** jaroslav@609: * This class implements an input stream filter for reading files in the jaroslav@609: * ZIP file format. Includes support for both compressed and uncompressed jaroslav@609: * entries. jaroslav@609: * jaroslav@609: * @author David Connelly jaroslav@609: */ jaroslav@609: public jaroslav@694: class ZipInputStream extends InflaterInputStream { jaroslav@609: private ZipEntry entry; jaroslav@609: private int flag; jaroslav@609: private CRC32 crc = new CRC32(); jaroslav@609: private long remaining; jaroslav@609: private byte[] tmpbuf = new byte[512]; jaroslav@609: jaroslav@609: private static final int STORED = ZipEntry.STORED; jaroslav@609: private static final int DEFLATED = ZipEntry.DEFLATED; jaroslav@609: jaroslav@609: private boolean closed = false; jaroslav@609: // this flag is set to true after EOF has reached for jaroslav@609: // one entry jaroslav@609: private boolean entryEOF = false; jaroslav@609: jaroslav@609: /** jaroslav@609: * Check to make sure that this stream has not been closed jaroslav@609: */ jaroslav@609: private void ensureOpen() throws IOException { jaroslav@609: if (closed) { jaroslav@609: throw new IOException("Stream closed"); jaroslav@609: } jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new ZIP input stream. jaroslav@609: * jaroslav@609: *
The UTF-8 {@link java.nio.charset.Charset charset} is used to jaroslav@609: * decode the entry names. jaroslav@609: * jaroslav@609: * @param in the actual input stream jaroslav@609: */ jaroslav@609: public ZipInputStream(InputStream in) { jaroslav@611: // this(in, "UTF-8"); jaroslav@611: super(new PushbackInputStream(in, 512), new Inflater(true), 512); jaroslav@694: //usesDefaultInflater = true; jaroslav@611: if(in == null) { jaroslav@611: throw new NullPointerException("in is null"); jaroslav@611: } jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new ZIP input stream. jaroslav@609: * jaroslav@609: * @param in the actual input stream jaroslav@609: * jaroslav@609: * @param charset jaroslav@609: * The {@linkplain java.nio.charset.Charset charset} to be jaroslav@609: * used to decode the ZIP entry name (ignored if the jaroslav@609: * language jaroslav@609: * encoding bit of the ZIP entry's general purpose bit jaroslav@609: * flag is set). jaroslav@609: * jaroslav@609: * @since 1.7 jaroslav@611: * jaroslav@609: public ZipInputStream(InputStream in, Charset charset) { jaroslav@609: super(new PushbackInputStream(in, 512), new Inflater(true), 512); jaroslav@609: usesDefaultInflater = true; jaroslav@609: if(in == null) { jaroslav@609: throw new NullPointerException("in is null"); jaroslav@609: } jaroslav@609: if (charset == null) jaroslav@609: throw new NullPointerException("charset is null"); jaroslav@609: this.zc = ZipCoder.get(charset); jaroslav@609: } jaroslav@611: */ jaroslav@609: jaroslav@609: /** jaroslav@609: * Reads the next ZIP file entry and positions the stream at the jaroslav@609: * beginning of the entry data. jaroslav@609: * @return the next ZIP file entry, or null if there are no more entries jaroslav@609: * @exception ZipException if a ZIP file error has occurred jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: */ jaroslav@609: public ZipEntry getNextEntry() throws IOException { jaroslav@609: ensureOpen(); jaroslav@609: if (entry != null) { jaroslav@609: closeEntry(); jaroslav@609: } jaroslav@609: crc.reset(); jaroslav@609: inf.reset(); jaroslav@609: if ((entry = readLOC()) == null) { jaroslav@609: return null; jaroslav@609: } jaroslav@694: if (entry.getMethod() == STORED) { jaroslav@694: remaining = entry.getSize(); jaroslav@609: } jaroslav@609: entryEOF = false; jaroslav@609: return entry; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Closes the current ZIP entry and positions the stream for reading the jaroslav@609: * next entry. jaroslav@609: * @exception ZipException if a ZIP file error has occurred jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: */ jaroslav@609: public void closeEntry() throws IOException { jaroslav@609: ensureOpen(); jaroslav@609: while (read(tmpbuf, 0, tmpbuf.length) != -1) ; jaroslav@609: entryEOF = true; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns 0 after EOF has reached for the current entry data, jaroslav@609: * otherwise always return 1. jaroslav@609: *
jaroslav@609: * Programs should not count on this method to return the actual number
jaroslav@609: * of bytes that could be read without blocking.
jaroslav@609: *
jaroslav@609: * @return 1 before EOF and 0 after EOF has reached for current entry.
jaroslav@609: * @exception IOException if an I/O error occurs.
jaroslav@609: *
jaroslav@609: */
jaroslav@609: public int available() throws IOException {
jaroslav@609: ensureOpen();
jaroslav@609: if (entryEOF) {
jaroslav@609: return 0;
jaroslav@609: } else {
jaroslav@609: return 1;
jaroslav@609: }
jaroslav@609: }
jaroslav@609:
jaroslav@609: /**
jaroslav@609: * Reads from the current ZIP entry into an array of bytes.
jaroslav@609: * If len
is not zero, the method
jaroslav@609: * blocks until some input is available; otherwise, no
jaroslav@609: * bytes are read and 0
is returned.
jaroslav@609: * @param b the buffer into which the data is read
jaroslav@609: * @param off the start offset in the destination array b
jaroslav@609: * @param len the maximum number of bytes read
jaroslav@609: * @return the actual number of bytes read, or -1 if the end of the
jaroslav@609: * entry is reached
jaroslav@609: * @exception NullPointerException if b
is null
.
jaroslav@609: * @exception IndexOutOfBoundsException if off
is negative,
jaroslav@609: * len
is negative, or len
is greater than
jaroslav@609: * b.length - off
jaroslav@609: * @exception ZipException if a ZIP file error has occurred
jaroslav@609: * @exception IOException if an I/O error has occurred
jaroslav@609: */
jaroslav@609: public int read(byte[] b, int off, int len) throws IOException {
jaroslav@609: ensureOpen();
jaroslav@609: if (off < 0 || len < 0 || off > b.length - len) {
jaroslav@609: throw new IndexOutOfBoundsException();
jaroslav@609: } else if (len == 0) {
jaroslav@609: return 0;
jaroslav@609: }
jaroslav@609:
jaroslav@609: if (entry == null) {
jaroslav@609: return -1;
jaroslav@609: }
jaroslav@694: switch (entry.getMethod()) {
jaroslav@609: case DEFLATED:
jaroslav@609: len = super.read(b, off, len);
jaroslav@609: if (len == -1) {
jaroslav@609: readEnd(entry);
jaroslav@609: entryEOF = true;
jaroslav@609: entry = null;
jaroslav@609: } else {
jaroslav@609: crc.update(b, off, len);
jaroslav@609: }
jaroslav@609: return len;
jaroslav@609: case STORED:
jaroslav@609: if (remaining <= 0) {
jaroslav@609: entryEOF = true;
jaroslav@609: entry = null;
jaroslav@609: return -1;
jaroslav@609: }
jaroslav@609: if (len > remaining) {
jaroslav@609: len = (int)remaining;
jaroslav@609: }
jaroslav@609: len = in.read(b, off, len);
jaroslav@609: if (len == -1) {
jaroslav@609: throw new ZipException("unexpected EOF");
jaroslav@609: }
jaroslav@609: crc.update(b, off, len);
jaroslav@609: remaining -= len;
jaroslav@694: if (remaining == 0 && entry.getCrc() != crc.getValue()) {
jaroslav@609: throw new ZipException(
jaroslav@694: "invalid entry CRC (expected 0x" + Long.toHexString(entry.getCrc()) +
jaroslav@609: " but got 0x" + Long.toHexString(crc.getValue()) + ")");
jaroslav@609: }
jaroslav@609: return len;
jaroslav@609: default:
jaroslav@609: throw new ZipException("invalid compression method");
jaroslav@609: }
jaroslav@609: }
jaroslav@609:
jaroslav@609: /**
jaroslav@609: * Skips specified number of bytes in the current ZIP entry.
jaroslav@609: * @param n the number of bytes to skip
jaroslav@609: * @return the actual number of bytes skipped
jaroslav@609: * @exception ZipException if a ZIP file error has occurred
jaroslav@609: * @exception IOException if an I/O error has occurred
jaroslav@609: * @exception IllegalArgumentException if n < 0
jaroslav@609: */
jaroslav@609: public long skip(long n) throws IOException {
jaroslav@609: if (n < 0) {
jaroslav@609: throw new IllegalArgumentException("negative skip length");
jaroslav@609: }
jaroslav@609: ensureOpen();
jaroslav@609: int max = (int)Math.min(n, Integer.MAX_VALUE);
jaroslav@609: int total = 0;
jaroslav@609: while (total < max) {
jaroslav@609: int len = max - total;
jaroslav@609: if (len > tmpbuf.length) {
jaroslav@609: len = tmpbuf.length;
jaroslav@609: }
jaroslav@609: len = read(tmpbuf, 0, len);
jaroslav@609: if (len == -1) {
jaroslav@609: entryEOF = true;
jaroslav@609: break;
jaroslav@609: }
jaroslav@609: total += len;
jaroslav@609: }
jaroslav@609: return total;
jaroslav@609: }
jaroslav@609:
jaroslav@609: /**
jaroslav@609: * Closes this input stream and releases any system resources associated
jaroslav@609: * with the stream.
jaroslav@609: * @exception IOException if an I/O error has occurred
jaroslav@609: */
jaroslav@609: public void close() throws IOException {
jaroslav@609: if (!closed) {
jaroslav@609: super.close();
jaroslav@609: closed = true;
jaroslav@609: }
jaroslav@609: }
jaroslav@609:
jaroslav@609: private byte[] b = new byte[256];
jaroslav@609:
jaroslav@609: /*
jaroslav@609: * Reads local file (LOC) header for next entry.
jaroslav@609: */
jaroslav@609: private ZipEntry readLOC() throws IOException {
jaroslav@609: try {
jaroslav@609: readFully(tmpbuf, 0, LOCHDR);
jaroslav@609: } catch (EOFException e) {
jaroslav@609: return null;
jaroslav@609: }
jaroslav@609: if (get32(tmpbuf, 0) != LOCSIG) {
jaroslav@609: return null;
jaroslav@609: }
jaroslav@609: // get flag first, we need check EFS.
jaroslav@609: flag = get16(tmpbuf, LOCFLG);
jaroslav@609: // get the entry name and create the ZipEntry first
jaroslav@609: int len = get16(tmpbuf, LOCNAM);
jaroslav@609: int blen = b.length;
jaroslav@609: if (len > blen) {
jaroslav@609: do
jaroslav@609: blen = blen * 2;
jaroslav@609: while (len > blen);
jaroslav@609: b = new byte[blen];
jaroslav@609: }
jaroslav@609: readFully(b, 0, len);
jaroslav@609: // Force to use UTF-8 if the EFS bit is ON, even the cs is NOT UTF-8
jaroslav@609: ZipEntry e = createZipEntry(((flag & EFS) != 0)
jaroslav@611: ? toStringUTF8(b, len)
jaroslav@611: : toString(b, len));
jaroslav@609: // now get the remaining fields for the entry
jaroslav@609: if ((flag & 1) == 1) {
jaroslav@609: throw new ZipException("encrypted ZIP entry not supported");
jaroslav@609: }
jaroslav@694: e.setMethod(get16(tmpbuf, LOCHOW));
jaroslav@694: e.setTime(get32(tmpbuf, LOCTIM));
jaroslav@609: if ((flag & 8) == 8) {
jaroslav@609: /* "Data Descriptor" present */
jaroslav@694: if (e.getMethod() != DEFLATED) {
jaroslav@609: throw new ZipException(
jaroslav@609: "only DEFLATED entries can have EXT descriptor");
jaroslav@609: }
jaroslav@609: } else {
jaroslav@694: e.setCrc(get32(tmpbuf, LOCCRC));
jaroslav@694: e.setCompressedSize(get32(tmpbuf, LOCSIZ));
jaroslav@694: e.setSize(get32(tmpbuf, LOCLEN));
jaroslav@609: }
jaroslav@609: len = get16(tmpbuf, LOCEXT);
jaroslav@609: if (len > 0) {
jaroslav@609: byte[] bb = new byte[len];
jaroslav@609: readFully(bb, 0, len);
jaroslav@609: e.setExtra(bb);
jaroslav@609: // extra fields are in "HeaderID(2)DataSize(2)Data... format
jaroslav@694: if (e.getCompressedSize() == ZIP64_MAGICVAL || e.getCompressedSize() == ZIP64_MAGICVAL) {
jaroslav@609: int off = 0;
jaroslav@609: while (off + 4 < len) {
jaroslav@609: int sz = get16(bb, off + 2);
jaroslav@609: if (get16(bb, off) == ZIP64_EXTID) {
jaroslav@609: off += 4;
jaroslav@609: // LOC extra zip64 entry MUST include BOTH original and
jaroslav@609: // compressed file size fields
jaroslav@609: if (sz < 16 || (off + sz) > len ) {
jaroslav@609: // Invalid zip64 extra fields, simply skip. Even it's
jaroslav@609: // rare, it's possible the entry size happens to be
jaroslav@609: // the magic value and it "accidnetly" has some bytes
jaroslav@609: // in extra match the id.
jaroslav@609: return e;
jaroslav@609: }
jaroslav@694: e.setSize(get64(bb, off));
jaroslav@694: e.setCompressedSize(get64(bb, off + 8));
jaroslav@609: break;
jaroslav@609: }
jaroslav@609: off += (sz + 4);
jaroslav@609: }
jaroslav@609: }
jaroslav@609: }
jaroslav@609: return e;
jaroslav@609: }
jaroslav@609:
jaroslav@609: /**
jaroslav@609: * Creates a new ZipEntry
object for the specified
jaroslav@609: * entry name.
jaroslav@609: *
jaroslav@609: * @param name the ZIP file entry name
jaroslav@609: * @return the ZipEntry just created
jaroslav@609: */
jaroslav@609: protected ZipEntry createZipEntry(String name) {
jaroslav@609: return new ZipEntry(name);
jaroslav@609: }
jaroslav@609:
jaroslav@609: /*
jaroslav@609: * Reads end of deflated entry as well as EXT descriptor if present.
jaroslav@609: */
jaroslav@609: private void readEnd(ZipEntry e) throws IOException {
jaroslav@609: int n = inf.getRemaining();
jaroslav@609: if (n > 0) {
jaroslav@609: ((PushbackInputStream)in).unread(buf, len - n, n);
jaroslav@609: }
jaroslav@609: if ((flag & 8) == 8) {
jaroslav@609: /* "Data Descriptor" present */
jaroslav@609: if (inf.getBytesWritten() > ZIP64_MAGICVAL ||
jaroslav@609: inf.getBytesRead() > ZIP64_MAGICVAL) {
jaroslav@609: // ZIP64 format
jaroslav@609: readFully(tmpbuf, 0, ZIP64_EXTHDR);
jaroslav@609: long sig = get32(tmpbuf, 0);
jaroslav@609: if (sig != EXTSIG) { // no EXTSIG present
jaroslav@694: e.setCrc(sig);
jaroslav@694: e.setCompressedSize(get64(tmpbuf, ZIP64_EXTSIZ - ZIP64_EXTCRC));
jaroslav@694: e.setSize(get64(tmpbuf, ZIP64_EXTLEN - ZIP64_EXTCRC));
jaroslav@609: ((PushbackInputStream)in).unread(
jaroslav@609: tmpbuf, ZIP64_EXTHDR - ZIP64_EXTCRC - 1, ZIP64_EXTCRC);
jaroslav@609: } else {
jaroslav@694: e.setCrc(get32(tmpbuf, ZIP64_EXTCRC));
jaroslav@694: e.setCompressedSize(get64(tmpbuf, ZIP64_EXTSIZ));
jaroslav@694: e.setSize(get64(tmpbuf, ZIP64_EXTLEN));
jaroslav@609: }
jaroslav@609: } else {
jaroslav@609: readFully(tmpbuf, 0, EXTHDR);
jaroslav@609: long sig = get32(tmpbuf, 0);
jaroslav@609: if (sig != EXTSIG) { // no EXTSIG present
jaroslav@694: e.setCrc(sig);
jaroslav@694: e.setCompressedSize(get32(tmpbuf, EXTSIZ - EXTCRC));
jaroslav@694: e.setSize(get32(tmpbuf, EXTLEN - EXTCRC));
jaroslav@609: ((PushbackInputStream)in).unread(
jaroslav@609: tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC);
jaroslav@609: } else {
jaroslav@694: e.setCrc(get32(tmpbuf, EXTCRC));
jaroslav@694: e.setCompressedSize(get32(tmpbuf, EXTSIZ));
jaroslav@694: e.setSize(get32(tmpbuf, EXTLEN));
jaroslav@609: }
jaroslav@609: }
jaroslav@609: }
jaroslav@694: if (e.getSize() != inf.getBytesWritten()) {
jaroslav@609: throw new ZipException(
jaroslav@694: "invalid entry size (expected " + e.getSize() +
jaroslav@609: " but got " + inf.getBytesWritten() + " bytes)");
jaroslav@609: }
jaroslav@694: if (e.getCompressedSize() != inf.getBytesRead()) {
jaroslav@609: throw new ZipException(
jaroslav@694: "invalid entry compressed size (expected " + e.getCompressedSize() +
jaroslav@609: " but got " + inf.getBytesRead() + " bytes)");
jaroslav@609: }
jaroslav@694: if (e.getCrc() != crc.getValue()) {
jaroslav@609: throw new ZipException(
jaroslav@694: "invalid entry CRC (expected 0x" + Long.toHexString(e.getCrc()) +
jaroslav@609: " but got 0x" + Long.toHexString(crc.getValue()) + ")");
jaroslav@609: }
jaroslav@609: }
jaroslav@609:
jaroslav@609: /*
jaroslav@609: * Reads bytes, blocking until all bytes are read.
jaroslav@609: */
jaroslav@609: private void readFully(byte[] b, int off, int len) throws IOException {
jaroslav@609: while (len > 0) {
jaroslav@609: int n = in.read(b, off, len);
jaroslav@609: if (n == -1) {
jaroslav@609: throw new EOFException();
jaroslav@609: }
jaroslav@609: off += n;
jaroslav@609: len -= n;
jaroslav@609: }
jaroslav@609: }
jaroslav@609:
jaroslav@609: /*
jaroslav@609: * Fetches unsigned 16-bit value from byte array at specified offset.
jaroslav@609: * The bytes are assumed to be in Intel (little-endian) byte order.
jaroslav@609: */
jaroslav@609: private static final int get16(byte b[], int off) {
jaroslav@609: return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
jaroslav@609: }
jaroslav@609:
jaroslav@609: /*
jaroslav@609: * Fetches unsigned 32-bit value from byte array at specified offset.
jaroslav@609: * The bytes are assumed to be in Intel (little-endian) byte order.
jaroslav@609: */
jaroslav@609: private static final long get32(byte b[], int off) {
jaroslav@609: return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
jaroslav@609: }
jaroslav@609:
jaroslav@609: /*
jaroslav@609: * Fetches signed 64-bit value from byte array at specified offset.
jaroslav@609: * The bytes are assumed to be in Intel (little-endian) byte order.
jaroslav@609: */
jaroslav@609: private static final long get64(byte b[], int off) {
jaroslav@609: return get32(b, off) | (get32(b, off+4) << 32);
jaroslav@609: }
jaroslav@611:
jaroslav@611: private static String toStringUTF8(byte[] arr, int len) {
jaroslav@611: return new String(arr, 0, len);
jaroslav@611: }
jaroslav@611:
jaroslav@611: private static String toString(byte[] b, int len) {
jaroslav@611: return new String(b, 0, len);
jaroslav@611: }
jaroslav@609: }