【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了CNZipInputStream.java相关资料,希望对各位网友有所帮助!
/*
* @(#)CNZipInputStream.java 1.33 03/02/07
*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.util.zip;
import java.io.InputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.PushbackInputStream;
/**
* This class implements an input stream filter for reading files in the
* ZIP file format. Includes support for both compressed and uncompressed
* entries.
*
* @author David Connelly
* @version 1.33, 02/07/03
*/
public
class CNZipInputStream extends InflaterInputStream implements ZipConstants {
private ZipEntry entry;
private CRC32 crc = new CRC32();
private long remaining;
private byte[] tmpbuf = new byte[512];
private static final int STORED = ZipEntry.STORED;
private static final int DEFLATED = ZipEntry.DEFLATED;
private String encoding="UTF-8";
private boolean closed = false;
// this flag is set to true after EOF has reached for
// one entry
private boolean entryEOF = false;
/**
* Check to make sure that this stream has not been closed
*/
private void ensureOpen() throws IOException {
if (closed) {
throw new IOException("Stream closed");
}
}
/**
* Creates a new ZIP input stream.
* @param in the actual input stream
*/
public CNZipInputStream(InputStream in) {
super(new PushbackInputStream(in, 512), new Inflater(true), 512);
usesDefaultInflater = true;
if(in == null) {
throw new NullPointerException("in is null");
}
}
public CNZipInputStream(InputStream in,String encoding) {
super(new PushbackInputStream(in,512),new Inflater(true),512);
usesDefaultInflater = true;
if(in == null) {
throw new NullPointerException("in is null");
}
this.encoding=encoding;
}
/**
* Reads the next ZIP file entry and positions stream at the beginning
* of the entry data.
* @return the ZipEntry just read
* @exception ZipException if a ZIP file error has occurred
* @exception IOException if an I/O error has occurred
*/
public ZipEntry getNextEntry() throws IOException {
ensureOpen();
if (entry != null) {
closeEntry();
}
crc.reset();
inf.reset();
if ((entry = readLOC()) == null) {
return null;
}
if (entry.method == STORED) {
remaining = entry.size;
}
entryEOF = false;
return entry;
}
/**
* Closes the current ZIP entry and positions the stream for reading the
* next entry.
* @exception ZipException if a ZIP file error has occurred
* @exception IOException if an I/O error has occurred
*/
public void closeEntry() throws IOException {
ensureOpen();
while (read(tmpbuf, 0, tmpbuf.length) != -1) ;
entryEOF = true;
}
/**
* Returns 0 after EOF has reached for the current entry data,
* otherwise always return 1.
* <p>
* Programs should not count on this method to return the actual number
* of bytes that could be read without blocking.
*
* @return 1 before EOF and 0 after EOF has reached for current entry.
* @exception IOException if an I/O error occurs.
*
*/
public int available() throws IOException {
ensureOpen();
if (entryEOF) {
return 0;
} else {
return 1;
}
}
/**
* Reads from the current ZIP entry into an array of bytes. Blocks until
* some input is available.
* @param b the buffer into which the data is read
* @param off the start offset of the data
* @param len the maximum number of bytes read
* @return the actual number of bytes read, or -1 if the end of the
* entry is reached
* @exception ZipException if a ZIP file error has occurred
* @exception IOException if an I/O error has occurred
*/
public int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if (off < 0 || len < 0 || off > b.length - len) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
if (entry == null) {
return -1;
}
switch (entry.method) {
case DEFLATED:
len = super.read(b, off, len);
if (len == -1) {
readEnd(entry);
entryEOF = true;
entry = null;
} else {
crc.update(b, off, len);
}
return len;
case STORED:
if (remaining <= 0) {
entryEOF = true;
entry = null;
return -1;
}
if (len > remaining) {
len = (int)remaining;
}
len = in.read(b, off, len);
if (len == -1) {
throw new ZipException("unexpected EOF");
}
crc.update(b, off, len);
remaining -= len;
return len;
default:
throw new InternalError("invalid compression method");
}
}
/**
* Skips specified number of bytes in the current ZIP entry.
* @param n the number of bytes to skip
* @return the actual number of bytes skipped
* @exception ZipException if a ZIP file error has occurred
* @exception IOException if an I/O error has occurred
* @exception IllegalArgumentException if n < 0
*/
public long skip(long n) throws IOException {
if (n < 0) {
throw new IllegalArgumentException("negative skip length");
}
ensureOpen();
int max = (int)Math.min(n, Integer.MAX_VALUE);
int total = 0;
while (total < max) {
int len = max - total;
if (len > tmpbuf.length) {
len = tmpbuf.length;
}
len = read(tmpbuf, 0, len);
if (len == -1) {
entryEOF = true;
break;
}
total += len;
}
return total;
}
/**
* Closes the ZIP input stream.
* @exception IOException if an I/O error has occurred
*/
public void close() throws IOException {
if (!closed) {
super.close();
closed = true;
}
}
private byte[] b = new byte[256];
/*
* Reads local file (LOC) header for next entry.
*/
private ZipEntry readLOC() throws IOException {
try {
readFully(tmpbuf, 0, LOCHDR);
} catch (EOFException e) {
return null;
}
if (get32(tmpbuf, 0) != LOCSIG) {
return null;
}
// get the entry name and create the ZipEntry first
int len = get16(tmpbuf, LOCNAM);
if (len == 0) {
throw new ZipException("missing entry name");
}
int blen = b.length;
if (len > blen) {
do
blen = blen * 2;
while (len > blen);
b = new byte[blen];
}
readFully(b, 0, len);
ZipEntry e=null;
try
{
if (this.encoding.toUpperCase().equals("UTF-8"))
e=createZipEntry(getUTF8String(b, 0, len));
else
e=createZipEntry(new String(b,0,len,this.encoding));
}
catch(Exception byteE)
{
e=createZipEntry(getUTF8String(b, 0, len));
}
// now get the remaining fields for the entry
e.version = get16(tmpbuf, LOCVER);
e.flag = get16(tmpbuf, LOCFLG);
if ((e.flag & 1) == 1) {
throw new ZipException("encrypted ZIP entry not supported");
}
e.method = get16(tmpbuf, LOCHOW);
e.time = get32(tmpbuf, LOCTIM);
if ((e.flag & 8) == 8) {
/* EXT descriptor present */
if (e.method != DEFLATED) {
throw new ZipException(
"only DEFLATED entries can have EXT descriptor");
}
} else {
e.crc = get32(tmpbuf, LOCCRC);
e.csize = get32(tmpbuf, LOCSIZ);
e.size = get32(tmpbuf, LOCLEN);
}
len = get16(tmpbuf, LOCEXT);
if (len > 0) {
byte[] bb = new byte[len];
readFully(bb, 0, len);
e.extra = bb;
}
return e;
}
/*
* Fetches a UTF8-encoded String from the specified byte array.
*/
private static String getUTF8String(byte[] b, int off, int len) {
// First, count the number of characters in the sequence
int count = 0;
int max = off + len;
int i = off;
while (i < max) {
int c = b[i++] & 0xff;
swit
上一篇:
CNZipDemo.java
下一篇:
法律专业开题报告范文