2000-05-12 01:05:03 +08:00
|
|
|
/* Copyright (C) 1999, 2000 Free Software Foundation
|
1999-05-06 08:15:47 +08:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
package java.util.zip;
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Per Bothner
|
|
|
|
* @date May 1999.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Written using on-line Java Platform 1.2 API Specification, as well
|
|
|
|
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
|
|
|
* Status: Quite incomplete, but can read uncompressed .zip archives.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// JDK1.2 has "protected ZipEntry createZipEntry(String)" but is very
|
|
|
|
// vague about what the method does. FIXME.
|
|
|
|
// We do not calculate the CRC and compare it with the specified value;
|
|
|
|
// we probably should. FIXME.
|
|
|
|
|
|
|
|
|
1999-06-23 20:24:59 +08:00
|
|
|
public class ZipInputStream extends InflaterInputStream implements ZipConstants
|
1999-05-06 08:15:47 +08:00
|
|
|
{
|
|
|
|
public ZipInputStream (InputStream in)
|
|
|
|
{
|
1999-05-23 02:08:46 +08:00
|
|
|
super (in, new Inflater (true));
|
1999-05-06 08:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public ZipEntry getNextEntry () throws IOException
|
|
|
|
{
|
2000-11-18 04:44:03 +08:00
|
|
|
if (closed)
|
|
|
|
throw new IOException ("stream closed");
|
1999-05-06 08:15:47 +08:00
|
|
|
if (current != null)
|
1999-06-23 20:24:59 +08:00
|
|
|
closeEntry();
|
1999-05-06 08:15:47 +08:00
|
|
|
if (in.read() != 'P'
|
|
|
|
|| in.read() != 'K')
|
|
|
|
return null;
|
|
|
|
int code = in.read();
|
|
|
|
while (code == '\001')
|
|
|
|
{
|
|
|
|
code = in.read();
|
|
|
|
if (code != '\002')
|
|
|
|
return null;
|
|
|
|
in.skip(16);
|
|
|
|
int size = read4();
|
|
|
|
in.skip(4);
|
|
|
|
int fname_length = readu2();
|
|
|
|
int extra_length = readu2();
|
|
|
|
int fcomment_length = readu2();
|
2000-05-12 01:05:03 +08:00
|
|
|
// `12' is the number of bytes between the comment length
|
|
|
|
// field and the end of the fixed part of the header:
|
|
|
|
// 2 bytes for `disk number start'
|
|
|
|
// 2 bytes for `internal file attributes'
|
|
|
|
// 4 bytes for `external file attributes'
|
|
|
|
// 4 bytes for `relative offset of local header'
|
|
|
|
in.skip(12 + fname_length + extra_length + fcomment_length);
|
1999-05-06 08:15:47 +08:00
|
|
|
if (in.read() != 'P' || in.read() != 'K')
|
|
|
|
return null;
|
|
|
|
code = in.read();
|
|
|
|
}
|
|
|
|
if (code == '\005')
|
|
|
|
{
|
|
|
|
if (in.read() != '\006')
|
|
|
|
return null;
|
|
|
|
in.skip(16);
|
|
|
|
int comment_size = readu2();
|
|
|
|
in.skip(comment_size);
|
|
|
|
if (in.read() != 'P' || in.read() != 'K')
|
|
|
|
return null;
|
|
|
|
code = in.read();
|
|
|
|
}
|
|
|
|
if (code != '\003'
|
|
|
|
|| in.read() != '\004')
|
|
|
|
return null;
|
|
|
|
int ex_version = readu2();
|
|
|
|
current_flags = readu2();
|
|
|
|
int method = readu2();
|
|
|
|
int modtime = readu2();
|
|
|
|
int moddate = readu2();
|
|
|
|
int crc = read4();
|
|
|
|
int compressedSize = read4();
|
|
|
|
int uncompressedSize = read4();
|
|
|
|
int filenameLength = readu2();
|
|
|
|
int extraLength = readu2();
|
|
|
|
byte[] bname = new byte[filenameLength];
|
|
|
|
readFully(bname);
|
2000-08-21 05:51:19 +08:00
|
|
|
ZipEntry entry = createZipEntry(new String(bname, "8859_1"));
|
1999-05-06 08:15:47 +08:00
|
|
|
if (extraLength > 0)
|
|
|
|
{
|
|
|
|
byte[] bextra = new byte[extraLength];
|
|
|
|
readFully(bextra);
|
|
|
|
entry.extra = bextra;
|
|
|
|
}
|
|
|
|
entry.compressedSize = compressedSize;
|
|
|
|
entry.size = uncompressedSize;
|
|
|
|
entry.crc = (long) crc & 0xffffffffL;
|
|
|
|
entry.method = method;
|
|
|
|
entry.time = ZipEntry.timeFromDOS(moddate, modtime);
|
|
|
|
current = entry;
|
|
|
|
avail = uncompressedSize;
|
1999-05-23 02:08:46 +08:00
|
|
|
compressed_bytes = compressedSize;
|
1999-05-06 08:15:47 +08:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
1999-05-23 02:08:46 +08:00
|
|
|
// We override fill to let us control how much data gets read from
|
|
|
|
// the underlying input stream. This lets us avoid having to push
|
|
|
|
// back data.
|
|
|
|
protected void fill () throws IOException
|
|
|
|
{
|
2000-11-18 04:44:03 +08:00
|
|
|
if (closed)
|
|
|
|
throw new IOException ("stream closed");
|
1999-05-23 02:08:46 +08:00
|
|
|
int count = buf.length;
|
|
|
|
if (count > compressed_bytes)
|
|
|
|
count = compressed_bytes;
|
|
|
|
len = in.read(buf, 0, count);
|
|
|
|
if (len != -1)
|
|
|
|
{
|
|
|
|
compressed_bytes -= len;
|
|
|
|
inf.setInput(buf, 0, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Attributes.java, [...]: Imported from Classpath.
Sat Aug 19 11:00:53 2000 Anthony Green <green@redhat.com>
* java/util/jar/Attributes.java, java/util/jar/JarEntry.java,
java/util/jar/JarException.java, java/util/jar/JarFile.java,
java/util/jar/JarInputStream.java,
java/util/jar/JarOutputStream.java, java/util/jar/Manifest.java,
java/util/Set.java, java/util/Map.java, java/util/Bucket.java,
java/util/AbstractSet.java, java/util/BasicMapEntry.java,
java/security/cert/CRL.java, java/security/cert/CRLException.java,
java/security/cert/Certificate.java,
java/security/cert/CertificateEncodingException.java,
java/security/cert/CertificateException.java,
java/security/cert/CertificateExpiredException.java,
java/security/cert/CertificateFactory.java,
java/security/cert/CertificateFactorySpi.java,
java/security/cert/CertificateNotYetValidException.java,
java/security/cert/CertificateParsingException.java,
java/security/cert/X509CRL.java,
java/security/cert/X509CRLEntry.java,
java/security/cert/X509Certificate.java,
java/security/cert/X509Extension.java: Imported from Classpath.
* java/util/Hashtable.java: Imported from Classpath.
* java/util/zip/ZipInputStream.java: Create stub for
createZipEntry.
* gcj/javaprims.h: Updated class list.
* Makefile.in, gcj/Makefile.in: Rebuilt.
* Makefile.am (ordinary_java_source_files): Add these new classes.
From-SVN: r35809
2000-08-20 02:19:42 +08:00
|
|
|
protected ZipEntry createZipEntry (String name)
|
|
|
|
{
|
2000-08-20 03:54:36 +08:00
|
|
|
return new ZipEntry (name);
|
Attributes.java, [...]: Imported from Classpath.
Sat Aug 19 11:00:53 2000 Anthony Green <green@redhat.com>
* java/util/jar/Attributes.java, java/util/jar/JarEntry.java,
java/util/jar/JarException.java, java/util/jar/JarFile.java,
java/util/jar/JarInputStream.java,
java/util/jar/JarOutputStream.java, java/util/jar/Manifest.java,
java/util/Set.java, java/util/Map.java, java/util/Bucket.java,
java/util/AbstractSet.java, java/util/BasicMapEntry.java,
java/security/cert/CRL.java, java/security/cert/CRLException.java,
java/security/cert/Certificate.java,
java/security/cert/CertificateEncodingException.java,
java/security/cert/CertificateException.java,
java/security/cert/CertificateExpiredException.java,
java/security/cert/CertificateFactory.java,
java/security/cert/CertificateFactorySpi.java,
java/security/cert/CertificateNotYetValidException.java,
java/security/cert/CertificateParsingException.java,
java/security/cert/X509CRL.java,
java/security/cert/X509CRLEntry.java,
java/security/cert/X509Certificate.java,
java/security/cert/X509Extension.java: Imported from Classpath.
* java/util/Hashtable.java: Imported from Classpath.
* java/util/zip/ZipInputStream.java: Create stub for
createZipEntry.
* gcj/javaprims.h: Updated class list.
* Makefile.in, gcj/Makefile.in: Rebuilt.
* Makefile.am (ordinary_java_source_files): Add these new classes.
From-SVN: r35809
2000-08-20 02:19:42 +08:00
|
|
|
}
|
2000-11-18 04:44:03 +08:00
|
|
|
|
1999-05-06 08:15:47 +08:00
|
|
|
public int read (byte[] b, int off, int len) throws IOException
|
|
|
|
{
|
2000-11-18 04:44:03 +08:00
|
|
|
if (closed)
|
|
|
|
throw new IOException ("stream closed");
|
1999-05-06 08:15:47 +08:00
|
|
|
if (len > avail)
|
|
|
|
len = avail;
|
1999-05-23 02:08:46 +08:00
|
|
|
int count;
|
|
|
|
if (current.method == Deflater.DEFLATED)
|
|
|
|
count = super.read(b, off, len);
|
|
|
|
else
|
|
|
|
count = in.read(b, off, len);
|
|
|
|
if (count == -1 || avail == 0)
|
|
|
|
{
|
|
|
|
inf.reset();
|
|
|
|
count = -1;
|
|
|
|
}
|
|
|
|
else
|
1999-05-06 08:15:47 +08:00
|
|
|
avail -= count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long skip (long n) throws IOException
|
|
|
|
{
|
2000-11-18 04:44:03 +08:00
|
|
|
if (closed)
|
|
|
|
throw new IOException ("stream closed");
|
1999-05-06 08:15:47 +08:00
|
|
|
if (n > avail)
|
|
|
|
n = avail;
|
1999-05-23 02:08:46 +08:00
|
|
|
long count;
|
|
|
|
if (current.method == Deflater.DEFLATED)
|
|
|
|
count = super.skip(n);
|
|
|
|
else
|
|
|
|
count = in.skip(n);
|
1999-05-06 08:15:47 +08:00
|
|
|
avail = avail - (int) count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2000-11-18 04:44:03 +08:00
|
|
|
public int available()
|
|
|
|
{
|
|
|
|
return closed ? 0 : 1;
|
2000-08-21 05:51:19 +08:00
|
|
|
}
|
|
|
|
|
1999-05-06 08:15:47 +08:00
|
|
|
private void readFully (byte[] b) throws IOException
|
|
|
|
{
|
|
|
|
int off = 0;
|
|
|
|
int len = b.length;
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
int count = in.read(b, off, len);
|
|
|
|
if (count <= 0)
|
|
|
|
throw new EOFException(".zip archive ended prematurely");
|
|
|
|
off += count;
|
|
|
|
len -= count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int readu2 () throws IOException
|
|
|
|
{
|
|
|
|
int byte0 = in.read();
|
|
|
|
int byte1 = in.read();
|
|
|
|
if (byte0 < 0 || byte1 < 0)
|
|
|
|
throw new EOFException(".zip archive ended prematurely");
|
|
|
|
return ((byte1 & 0xFF) << 8) | (byte0 & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int read4 () throws IOException
|
|
|
|
{
|
|
|
|
int byte0 = in.read();
|
|
|
|
int byte1 = in.read();
|
|
|
|
int byte2 = in.read();
|
|
|
|
int byte3 = in.read();
|
|
|
|
if (byte3 < 0)
|
|
|
|
throw new EOFException(".zip archive ended prematurely");
|
|
|
|
return ((byte3 & 0xFF) << 24) + ((byte2 & 0xFF) << 16)
|
|
|
|
+ ((byte1 & 0xFF) << 8) + (byte0 & 0xFF);
|
|
|
|
}
|
|
|
|
|
1999-06-23 20:24:59 +08:00
|
|
|
public void closeEntry () throws IOException
|
1999-05-06 08:15:47 +08:00
|
|
|
{
|
|
|
|
if (current != null)
|
|
|
|
{
|
|
|
|
if (avail > 0)
|
|
|
|
skip (avail);
|
|
|
|
if ((current_flags & 8) != 0)
|
|
|
|
{
|
|
|
|
int sig = read4();
|
|
|
|
if (sig != 0x04034b50)
|
1999-05-18 23:33:03 +08:00
|
|
|
throw new ZipException("bad/missing magic number at end of .zip entry");
|
1999-05-06 08:15:47 +08:00
|
|
|
int crc = read4();
|
|
|
|
int compressedSize = read4();
|
|
|
|
int uncompressedSize = read4();
|
|
|
|
if (current.compressedSize != compressedSize
|
|
|
|
|| current.size != uncompressedSize
|
|
|
|
|| current.crc != crc)
|
1999-05-18 23:33:03 +08:00
|
|
|
throw new ZipException("bad data descriptor at end of .zip entry");
|
1999-05-06 08:15:47 +08:00
|
|
|
}
|
|
|
|
current = null;
|
|
|
|
avail = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close () throws IOException
|
|
|
|
{
|
|
|
|
current = null;
|
2000-08-21 05:51:19 +08:00
|
|
|
closed = true;
|
1999-05-06 08:15:47 +08:00
|
|
|
super.close();
|
|
|
|
}
|
1999-05-23 02:08:46 +08:00
|
|
|
|
|
|
|
private ZipEntry current;
|
|
|
|
private int current_flags;
|
|
|
|
// Number of uncompressed bytes to be read.
|
|
|
|
private int avail;
|
|
|
|
// Number of bytes we can read from underlying stream.
|
|
|
|
private int compressed_bytes;
|
2000-08-21 05:51:19 +08:00
|
|
|
// Is this ZipInputStream closed? Set by the close() method.
|
|
|
|
private boolean closed = false;
|
1999-05-06 08:15:47 +08:00
|
|
|
}
|