2002-06-15 20:31:13 +02:00
|
|
|
/* java.util.zip.ZipFile
|
2003-01-02 22:01:24 +01:00
|
|
|
Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
|
2000-11-17 22:42:28 +01:00
|
|
|
|
|
|
|
This file is part of GNU Classpath.
|
|
|
|
|
|
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2002-06-15 20:31:13 +02:00
|
|
|
|
2000-11-17 22:42:28 +01:00
|
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
|
|
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA.
|
|
|
|
|
2002-01-22 23:40:42 +01:00
|
|
|
Linking this library statically or dynamically with other modules is
|
|
|
|
making a combined work based on this library. Thus, the terms and
|
|
|
|
conditions of the GNU General Public License cover the whole
|
|
|
|
combination.
|
|
|
|
|
|
|
|
As a special exception, the copyright holders of this library give you
|
|
|
|
permission to link this library with independent modules to produce an
|
|
|
|
executable, regardless of the license terms of these independent
|
|
|
|
modules, and to copy and distribute the resulting executable under
|
|
|
|
terms of your choice, provided that you also meet, for each linked
|
|
|
|
independent module, the terms and conditions of the license of that
|
|
|
|
module. An independent module is a module which is not derived from
|
|
|
|
or based on this library. If you modify this library, you may extend
|
|
|
|
this exception to your version of the library, but you are not
|
|
|
|
obligated to do so. If you do not wish to do so, delete this
|
|
|
|
exception statement from your version. */
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
package java.util.zip;
|
2002-10-31 22:56:32 +01:00
|
|
|
|
2003-01-02 22:01:24 +01:00
|
|
|
import java.io.BufferedInputStream;
|
2002-10-31 22:56:32 +01:00
|
|
|
import java.io.DataInput;
|
2002-06-15 20:31:13 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.EOFException;
|
|
|
|
import java.io.RandomAccessFile;
|
|
|
|
import java.util.Enumeration;
|
2003-01-02 22:01:24 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2002-06-15 20:31:13 +02:00
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents a Zip archive. You can ask for the contained
|
|
|
|
* entries, or get an input stream for a file entry. The entry is
|
|
|
|
* automatically decompressed.
|
|
|
|
*
|
|
|
|
* This class is thread safe: You can open input streams for arbitrary
|
|
|
|
* entries in different threads.
|
|
|
|
*
|
|
|
|
* @author Jochen Hoenicke
|
2003-01-02 22:01:24 +01:00
|
|
|
* @author Artur Biesiadowski
|
1999-05-18 17:33:03 +02:00
|
|
|
*/
|
1999-04-07 16:42:40 +02:00
|
|
|
public class ZipFile implements ZipConstants
|
|
|
|
{
|
2000-08-28 00:26:27 +02:00
|
|
|
|
2002-12-03 23:06:31 +01:00
|
|
|
/**
|
|
|
|
* Mode flag to open a zip file for reading.
|
2002-06-15 20:31:13 +02:00
|
|
|
*/
|
|
|
|
public static final int OPEN_READ = 0x1;
|
|
|
|
|
2002-12-03 23:06:31 +01:00
|
|
|
/**
|
|
|
|
* Mode flag to delete a zip file after reading.
|
2002-06-15 20:31:13 +02:00
|
|
|
*/
|
|
|
|
public static final int OPEN_DELETE = 0x4;
|
|
|
|
|
2002-12-03 23:06:31 +01:00
|
|
|
// Name of this zip file.
|
|
|
|
private final String name;
|
|
|
|
|
|
|
|
// File from which zip entries are read.
|
|
|
|
private final RandomAccessFile raf;
|
|
|
|
|
|
|
|
// The entries of this zip file when initialized and not yet closed.
|
2003-01-02 22:01:24 +01:00
|
|
|
private HashMap entries;
|
2002-12-03 23:06:31 +01:00
|
|
|
|
|
|
|
private boolean closed = false;
|
2002-06-15 20:31:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a Zip file with the given name for reading.
|
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
* @exception ZipException if the file doesn't contain a valid zip
|
|
|
|
* archive.
|
|
|
|
*/
|
|
|
|
public ZipFile(String name) throws ZipException, IOException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
this.raf = new RandomAccessFile(name, "r");
|
|
|
|
this.name = name;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
|
|
|
* Opens a Zip file reading the given File.
|
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
* @exception ZipException if the file doesn't contain a valid zip
|
|
|
|
* archive.
|
|
|
|
*/
|
|
|
|
public ZipFile(File file) throws ZipException, IOException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
this.raf = new RandomAccessFile(file, "r");
|
2003-11-26 22:55:27 +01:00
|
|
|
this.name = file.getPath();
|
2000-08-28 00:26:27 +02:00
|
|
|
}
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
|
|
|
* Opens a Zip file reading the given File in the given mode.
|
|
|
|
*
|
2002-10-31 22:56:32 +01:00
|
|
|
* If the OPEN_DELETE mode is specified, the zip file will be deleted at
|
|
|
|
* some time moment after it is opened. It will be deleted before the zip
|
|
|
|
* file is closed or the Virtual Machine exits.
|
2002-06-15 20:31:13 +02:00
|
|
|
*
|
|
|
|
* The contents of the zip file will be accessible until it is closed.
|
|
|
|
*
|
|
|
|
* The OPEN_DELETE mode is currently unimplemented in this library
|
|
|
|
*
|
|
|
|
* @since JDK1.3
|
|
|
|
* @param mode Must be one of OPEN_READ or OPEN_READ | OPEN_DELETE
|
|
|
|
*
|
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
* @exception ZipException if the file doesn't contain a valid zip
|
|
|
|
* archive.
|
|
|
|
*/
|
|
|
|
public ZipFile(File file, int mode) throws ZipException, IOException
|
2000-08-28 00:26:27 +02:00
|
|
|
{
|
|
|
|
if ((mode & OPEN_DELETE) != 0)
|
|
|
|
{
|
2002-10-31 22:56:32 +01:00
|
|
|
throw new IllegalArgumentException
|
|
|
|
("OPEN_DELETE mode not supported yet in java.util.zip.ZipFile");
|
2000-08-28 00:26:27 +02:00
|
|
|
}
|
2002-06-15 20:31:13 +02:00
|
|
|
this.raf = new RandomAccessFile(file, "r");
|
2003-11-26 22:55:27 +01:00
|
|
|
this.name = file.getPath();
|
2002-06-15 20:31:13 +02:00
|
|
|
}
|
2000-08-28 00:26:27 +02:00
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
2003-01-02 22:01:24 +01:00
|
|
|
* Read an unsigned short in little endian byte order from the given
|
|
|
|
* DataInput stream using the given byte buffer.
|
|
|
|
*
|
|
|
|
* @param di DataInput stream to read from.
|
|
|
|
* @param b the byte buffer to read in (must be at least 2 bytes long).
|
|
|
|
* @return The value read.
|
|
|
|
*
|
2002-06-15 20:31:13 +02:00
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
* @exception EOFException if the file ends prematurely
|
|
|
|
*/
|
2003-01-02 22:01:24 +01:00
|
|
|
private final int readLeShort(DataInput di, byte[] b) throws IOException
|
2002-10-31 22:56:32 +01:00
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
di.readFully(b, 0, 2);
|
2002-10-31 22:56:32 +01:00
|
|
|
return (b[0] & 0xff) | (b[1] & 0xff) << 8;
|
1999-05-06 02:15:47 +02:00
|
|
|
}
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
2003-01-02 22:01:24 +01:00
|
|
|
* Read an int in little endian byte order from the given
|
|
|
|
* DataInput stream using the given byte buffer.
|
|
|
|
*
|
|
|
|
* @param di DataInput stream to read from.
|
|
|
|
* @param b the byte buffer to read in (must be at least 4 bytes long).
|
|
|
|
* @return The value read.
|
|
|
|
*
|
2002-06-15 20:31:13 +02:00
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
* @exception EOFException if the file ends prematurely
|
|
|
|
*/
|
2003-01-02 22:01:24 +01:00
|
|
|
private final int readLeInt(DataInput di, byte[] b) throws IOException
|
2002-10-31 22:56:32 +01:00
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
di.readFully(b, 0, 4);
|
2002-10-31 22:56:32 +01:00
|
|
|
return ((b[0] & 0xff) | (b[1] & 0xff) << 8)
|
|
|
|
| ((b[2] & 0xff) | (b[3] & 0xff) << 8) << 16;
|
2002-06-15 20:31:13 +02:00
|
|
|
}
|
|
|
|
|
2003-01-02 22:01:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read an unsigned short in little endian byte order from the given
|
|
|
|
* byte buffer at the given offset.
|
|
|
|
*
|
|
|
|
* @param b the byte array to read from.
|
|
|
|
* @param off the offset to read from.
|
|
|
|
* @return The value read.
|
|
|
|
*/
|
|
|
|
private final int readLeShort(byte[] b, int off)
|
|
|
|
{
|
|
|
|
return (b[off] & 0xff) | (b[off+1] & 0xff) << 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read an int in little endian byte order from the given
|
|
|
|
* byte buffer at the given offset.
|
|
|
|
*
|
|
|
|
* @param b the byte array to read from.
|
|
|
|
* @param off the offset to read from.
|
|
|
|
* @return The value read.
|
|
|
|
*/
|
|
|
|
private final int readLeInt(byte[] b, int off)
|
|
|
|
{
|
|
|
|
return ((b[off] & 0xff) | (b[off+1] & 0xff) << 8)
|
|
|
|
| ((b[off+2] & 0xff) | (b[off+3] & 0xff) << 8) << 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
|
|
|
* Read the central directory of a zip file and fill the entries
|
2003-01-02 22:01:24 +01:00
|
|
|
* array. This is called exactly once when first needed. It is called
|
|
|
|
* while holding the lock on <code>raf</code>.
|
|
|
|
*
|
2002-06-15 20:31:13 +02:00
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
* @exception ZipException if the central directory is malformed
|
|
|
|
*/
|
|
|
|
private void readEntries() throws ZipException, IOException
|
1999-05-06 02:15:47 +02:00
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
/* Search for the End Of Central Directory. When a zip comment is
|
|
|
|
* present the directory may start earlier.
|
|
|
|
* FIXME: This searches the whole file in a very slow manner if the
|
|
|
|
* file isn't a zip file.
|
|
|
|
*/
|
|
|
|
long pos = raf.length() - ENDHDR;
|
2003-01-02 22:01:24 +01:00
|
|
|
byte[] ebs = new byte[CENHDR];
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (pos < 0)
|
|
|
|
throw new ZipException
|
2002-10-31 22:56:32 +01:00
|
|
|
("central directory not found, probably not a zip file: " + name);
|
2002-06-15 20:31:13 +02:00
|
|
|
raf.seek(pos--);
|
|
|
|
}
|
2003-01-02 22:01:24 +01:00
|
|
|
while (readLeInt(raf, ebs) != ENDSIG);
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
if (raf.skipBytes(ENDTOT - ENDNRD) != ENDTOT - ENDNRD)
|
2002-10-31 22:56:32 +01:00
|
|
|
throw new EOFException(name);
|
2003-01-02 22:01:24 +01:00
|
|
|
int count = readLeShort(raf, ebs);
|
2002-06-15 20:31:13 +02:00
|
|
|
if (raf.skipBytes(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ)
|
2002-10-31 22:56:32 +01:00
|
|
|
throw new EOFException(name);
|
2003-01-02 22:01:24 +01:00
|
|
|
int centralOffset = readLeInt(raf, ebs);
|
2002-06-15 20:31:13 +02:00
|
|
|
|
2003-01-02 22:01:24 +01:00
|
|
|
entries = new HashMap(count+count/2);
|
2002-06-15 20:31:13 +02:00
|
|
|
raf.seek(centralOffset);
|
2003-01-02 22:01:24 +01:00
|
|
|
|
|
|
|
byte[] buffer = new byte[16];
|
2002-06-15 20:31:13 +02:00
|
|
|
for (int i = 0; i < count; i++)
|
1999-05-06 02:15:47 +02:00
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
raf.readFully(ebs);
|
|
|
|
if (readLeInt(ebs, 0) != CENSIG)
|
2002-10-31 22:56:32 +01:00
|
|
|
throw new ZipException("Wrong Central Directory signature: " + name);
|
2003-01-02 22:01:24 +01:00
|
|
|
|
|
|
|
int method = readLeShort(ebs, CENHOW);
|
|
|
|
int dostime = readLeInt(ebs, CENTIM);
|
|
|
|
int crc = readLeInt(ebs, CENCRC);
|
|
|
|
int csize = readLeInt(ebs, CENSIZ);
|
|
|
|
int size = readLeInt(ebs, CENLEN);
|
|
|
|
int nameLen = readLeShort(ebs, CENNAM);
|
|
|
|
int extraLen = readLeShort(ebs, CENEXT);
|
|
|
|
int commentLen = readLeShort(ebs, CENCOM);
|
|
|
|
|
|
|
|
int offset = readLeInt(ebs, CENOFF);
|
|
|
|
|
|
|
|
int needBuffer = Math.max(nameLen, commentLen);
|
|
|
|
if (buffer.length < needBuffer)
|
|
|
|
buffer = new byte[needBuffer];
|
2002-06-15 20:31:13 +02:00
|
|
|
|
|
|
|
raf.readFully(buffer, 0, nameLen);
|
2003-01-02 22:01:24 +01:00
|
|
|
String name = new String(buffer, 0, 0, nameLen);
|
2002-06-15 20:31:13 +02:00
|
|
|
|
|
|
|
ZipEntry entry = new ZipEntry(name);
|
|
|
|
entry.setMethod(method);
|
|
|
|
entry.setCrc(crc & 0xffffffffL);
|
|
|
|
entry.setSize(size & 0xffffffffL);
|
|
|
|
entry.setCompressedSize(csize & 0xffffffffL);
|
|
|
|
entry.setDOSTime(dostime);
|
|
|
|
if (extraLen > 0)
|
1999-05-06 02:15:47 +02:00
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
byte[] extra = new byte[extraLen];
|
|
|
|
raf.readFully(extra);
|
|
|
|
entry.setExtra(extra);
|
1999-05-06 02:15:47 +02:00
|
|
|
}
|
2002-06-15 20:31:13 +02:00
|
|
|
if (commentLen > 0)
|
1999-05-06 02:15:47 +02:00
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
raf.readFully(buffer, 0, commentLen);
|
|
|
|
entry.setComment(new String(buffer, 0, commentLen));
|
1999-05-06 02:15:47 +02:00
|
|
|
}
|
2002-06-15 20:31:13 +02:00
|
|
|
entry.offset = offset;
|
2002-12-03 23:06:31 +01:00
|
|
|
entries.put(name, entry);
|
1999-05-06 02:15:47 +02:00
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
|
|
|
* Closes the ZipFile. This also closes all input streams given by
|
|
|
|
* this class. After this is called, no further method should be
|
|
|
|
* called.
|
2003-01-02 22:01:24 +01:00
|
|
|
*
|
2002-06-15 20:31:13 +02:00
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
*/
|
1999-04-07 16:42:40 +02:00
|
|
|
public void close() throws IOException
|
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
synchronized (raf)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-12-03 23:06:31 +01:00
|
|
|
closed = true;
|
|
|
|
entries = null;
|
2002-06-15 20:31:13 +02:00
|
|
|
raf.close();
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-21 13:35:49 +01:00
|
|
|
/**
|
|
|
|
* Calls the <code>close()</code> method when this ZipFile has not yet
|
|
|
|
* been explicitly closed.
|
|
|
|
*/
|
|
|
|
protected void finalize() throws IOException
|
|
|
|
{
|
MemoryImageSource.java, [...]: New versions from classpath.
2003-07-14 Michael Koch <konqueror@gmx.de>
* java/awt/image/MemoryImageSource.java,
java/beans/PropertyEditorManager.java,
javax/naming/CompoundName.java,
javax/naming/spi/NamingManager.java,
javax/swing/AbstractButton.java,
javax/swing/ButtonModel.java,
javax/swing/SwingUtilities.java,
javax/swing/UIManager.java,
javax/swing/colorchooser/DefaultColorSelectionModel.java,
javax/swing/event/AncestorEvent.java,
javax/swing/event/InternalFrameEvent.java,
java/util/zip/ZipFile.java:
New versions from classpath.
From-SVN: r69321
2003-07-14 07:33:30 +02:00
|
|
|
if (!closed && raf != null) close();
|
2003-02-21 13:35:49 +01:00
|
|
|
}
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
|
|
|
* Returns an enumeration of all Zip entries in this Zip file.
|
|
|
|
*/
|
|
|
|
public Enumeration entries()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-12-03 23:06:31 +01:00
|
|
|
try
|
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
return new ZipEntryEnumeration(getEntries().values().iterator());
|
2002-12-03 23:06:31 +01:00
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2002-12-03 23:06:31 +01:00
|
|
|
/**
|
|
|
|
* Checks that the ZipFile is still open and reads entries when necessary.
|
|
|
|
*
|
|
|
|
* @exception IllegalStateException when the ZipFile has already been closed.
|
|
|
|
* @exception IOEexception when the entries could not be read.
|
|
|
|
*/
|
2003-01-02 22:01:24 +01:00
|
|
|
private HashMap getEntries() throws IOException
|
BitMaskExtent.java, [...]: Removed Latin-1 copyright symbols.
* gnu/gcj/awt/BitMaskExtent.java, gnu/gcj/awt/Buffers.java,
gnu/gcj/awt/ComponentDataBlitOp.java,
gnu/gcj/awt/GLightweightPeer.java, java/awt/Graphics2D.java,
java/awt/RenderingHints.java, java/awt/color/ColorSpace.java,
java/awt/color/ICC_ColorSpace.java,
java/awt/color/ICC_Profile.java,
java/awt/image/BufferedImage.java, java/awt/image/ColorModel.java,
java/awt/image/ComponentColorModel.java,
java/awt/image/ComponentSampleModel.java,
java/awt/image/DataBuffer.java,
java/awt/image/DataBufferByte.java,
java/awt/image/DataBufferInt.java,
java/awt/image/DataBufferUShort.java,
java/awt/image/DirectColorModel.java,
java/awt/image/IndexColorModel.java,
java/awt/image/PackedColorModel.java, java/awt/image/Raster.java,
java/awt/image/RasterOp.java, java/awt/image/SampleModel.java,
java/awt/image/SinglePixelPackedSampleModel.java,
java/awt/image/WritableRaster.java, java/util/zip/ZipFile.java:
Removed Latin-1 copyright symbols.
* java/util/zip/ZipFile.java: Indentation fixes.
From-SVN: r36027
2000-08-29 05:23:57 +02:00
|
|
|
{
|
2002-12-03 23:06:31 +01:00
|
|
|
synchronized(raf)
|
|
|
|
{
|
|
|
|
if (closed)
|
|
|
|
throw new IllegalStateException("ZipFile has closed: " + name);
|
|
|
|
|
|
|
|
if (entries == null)
|
|
|
|
readEntries();
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
BitMaskExtent.java, [...]: Removed Latin-1 copyright symbols.
* gnu/gcj/awt/BitMaskExtent.java, gnu/gcj/awt/Buffers.java,
gnu/gcj/awt/ComponentDataBlitOp.java,
gnu/gcj/awt/GLightweightPeer.java, java/awt/Graphics2D.java,
java/awt/RenderingHints.java, java/awt/color/ColorSpace.java,
java/awt/color/ICC_ColorSpace.java,
java/awt/color/ICC_Profile.java,
java/awt/image/BufferedImage.java, java/awt/image/ColorModel.java,
java/awt/image/ComponentColorModel.java,
java/awt/image/ComponentSampleModel.java,
java/awt/image/DataBuffer.java,
java/awt/image/DataBufferByte.java,
java/awt/image/DataBufferInt.java,
java/awt/image/DataBufferUShort.java,
java/awt/image/DirectColorModel.java,
java/awt/image/IndexColorModel.java,
java/awt/image/PackedColorModel.java, java/awt/image/Raster.java,
java/awt/image/RasterOp.java, java/awt/image/SampleModel.java,
java/awt/image/SinglePixelPackedSampleModel.java,
java/awt/image/WritableRaster.java, java/util/zip/ZipFile.java:
Removed Latin-1 copyright symbols.
* java/util/zip/ZipFile.java: Indentation fixes.
From-SVN: r36027
2000-08-29 05:23:57 +02:00
|
|
|
}
|
1999-05-06 02:15:47 +02:00
|
|
|
|
2000-11-17 22:42:28 +01:00
|
|
|
/**
|
2002-06-15 20:31:13 +02:00
|
|
|
* Searches for a zip entry in this archive with the given name.
|
2003-01-02 22:01:24 +01:00
|
|
|
*
|
2002-06-15 20:31:13 +02:00
|
|
|
* @param the name. May contain directory components separated by
|
|
|
|
* slashes ('/').
|
|
|
|
* @return the zip entry, or null if no entry with that name exists.
|
2003-01-02 22:01:24 +01:00
|
|
|
*/
|
2002-06-15 20:31:13 +02:00
|
|
|
public ZipEntry getEntry(String name)
|
BitMaskExtent.java, [...]: Removed Latin-1 copyright symbols.
* gnu/gcj/awt/BitMaskExtent.java, gnu/gcj/awt/Buffers.java,
gnu/gcj/awt/ComponentDataBlitOp.java,
gnu/gcj/awt/GLightweightPeer.java, java/awt/Graphics2D.java,
java/awt/RenderingHints.java, java/awt/color/ColorSpace.java,
java/awt/color/ICC_ColorSpace.java,
java/awt/color/ICC_Profile.java,
java/awt/image/BufferedImage.java, java/awt/image/ColorModel.java,
java/awt/image/ComponentColorModel.java,
java/awt/image/ComponentSampleModel.java,
java/awt/image/DataBuffer.java,
java/awt/image/DataBufferByte.java,
java/awt/image/DataBufferInt.java,
java/awt/image/DataBufferUShort.java,
java/awt/image/DirectColorModel.java,
java/awt/image/IndexColorModel.java,
java/awt/image/PackedColorModel.java, java/awt/image/Raster.java,
java/awt/image/RasterOp.java, java/awt/image/SampleModel.java,
java/awt/image/SinglePixelPackedSampleModel.java,
java/awt/image/WritableRaster.java, java/util/zip/ZipFile.java:
Removed Latin-1 copyright symbols.
* java/util/zip/ZipFile.java: Indentation fixes.
From-SVN: r36027
2000-08-29 05:23:57 +02:00
|
|
|
{
|
2002-12-03 23:06:31 +01:00
|
|
|
try
|
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
HashMap entries = getEntries();
|
2002-12-03 23:06:31 +01:00
|
|
|
ZipEntry entry = (ZipEntry) entries.get(name);
|
|
|
|
return entry != null ? (ZipEntry) entry.clone() : null;
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2000-08-20 23:51:19 +02:00
|
|
|
}
|
|
|
|
|
2003-01-02 22:01:24 +01:00
|
|
|
|
|
|
|
//access should be protected by synchronized(raf)
|
|
|
|
private byte[] locBuf = new byte[LOCHDR];
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
|
|
|
* Checks, if the local header of the entry at index i matches the
|
|
|
|
* central directory, and returns the offset to the data.
|
2003-01-02 22:01:24 +01:00
|
|
|
*
|
|
|
|
* @param entry to check.
|
2002-06-15 20:31:13 +02:00
|
|
|
* @return the start offset of the (compressed) data.
|
2003-01-02 22:01:24 +01:00
|
|
|
*
|
2002-06-15 20:31:13 +02:00
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
* @exception ZipException if the local header doesn't match the
|
|
|
|
* central directory header
|
|
|
|
*/
|
|
|
|
private long checkLocalHeader(ZipEntry entry) throws IOException
|
BitMaskExtent.java, [...]: Removed Latin-1 copyright symbols.
* gnu/gcj/awt/BitMaskExtent.java, gnu/gcj/awt/Buffers.java,
gnu/gcj/awt/ComponentDataBlitOp.java,
gnu/gcj/awt/GLightweightPeer.java, java/awt/Graphics2D.java,
java/awt/RenderingHints.java, java/awt/color/ColorSpace.java,
java/awt/color/ICC_ColorSpace.java,
java/awt/color/ICC_Profile.java,
java/awt/image/BufferedImage.java, java/awt/image/ColorModel.java,
java/awt/image/ComponentColorModel.java,
java/awt/image/ComponentSampleModel.java,
java/awt/image/DataBuffer.java,
java/awt/image/DataBufferByte.java,
java/awt/image/DataBufferInt.java,
java/awt/image/DataBufferUShort.java,
java/awt/image/DirectColorModel.java,
java/awt/image/IndexColorModel.java,
java/awt/image/PackedColorModel.java, java/awt/image/Raster.java,
java/awt/image/RasterOp.java, java/awt/image/SampleModel.java,
java/awt/image/SinglePixelPackedSampleModel.java,
java/awt/image/WritableRaster.java, java/util/zip/ZipFile.java:
Removed Latin-1 copyright symbols.
* java/util/zip/ZipFile.java: Indentation fixes.
From-SVN: r36027
2000-08-29 05:23:57 +02:00
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
synchronized (raf)
|
|
|
|
{
|
|
|
|
raf.seek(entry.offset);
|
2003-01-02 22:01:24 +01:00
|
|
|
raf.readFully(locBuf);
|
|
|
|
|
|
|
|
if (readLeInt(locBuf, 0) != LOCSIG)
|
2002-10-31 22:56:32 +01:00
|
|
|
throw new ZipException("Wrong Local header signature: " + name);
|
2002-06-15 20:31:13 +02:00
|
|
|
|
2003-01-02 22:01:24 +01:00
|
|
|
if (entry.getMethod() != readLeShort(locBuf, LOCHOW))
|
2002-10-31 22:56:32 +01:00
|
|
|
throw new ZipException("Compression method mismatch: " + name);
|
2002-06-15 20:31:13 +02:00
|
|
|
|
2003-01-02 22:01:24 +01:00
|
|
|
if (entry.getName().length() != readLeShort(locBuf, LOCNAM))
|
2002-10-31 22:56:32 +01:00
|
|
|
throw new ZipException("file name length mismatch: " + name);
|
2002-06-15 20:31:13 +02:00
|
|
|
|
2003-01-02 22:01:24 +01:00
|
|
|
int extraLen = entry.getName().length() + readLeShort(locBuf, LOCEXT);
|
2002-06-15 20:31:13 +02:00
|
|
|
return entry.offset + LOCHDR + extraLen;
|
|
|
|
}
|
2000-08-28 00:26:27 +02:00
|
|
|
}
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
|
|
|
* Creates an input stream reading the given zip entry as
|
|
|
|
* uncompressed data. Normally zip entry should be an entry
|
|
|
|
* returned by getEntry() or entries().
|
2003-01-02 22:01:24 +01:00
|
|
|
*
|
|
|
|
* @param entry the entry to create an InputStream for.
|
2002-06-15 20:31:13 +02:00
|
|
|
* @return the input stream.
|
2003-01-02 22:01:24 +01:00
|
|
|
*
|
2002-06-15 20:31:13 +02:00
|
|
|
* @exception IOException if a i/o error occured.
|
|
|
|
* @exception ZipException if the Zip archive is malformed.
|
|
|
|
*/
|
|
|
|
public InputStream getInputStream(ZipEntry entry) throws IOException
|
1999-05-06 02:15:47 +02:00
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
HashMap entries = getEntries();
|
2002-12-03 23:06:31 +01:00
|
|
|
String name = entry.getName();
|
|
|
|
ZipEntry zipEntry = (ZipEntry) entries.get(name);
|
|
|
|
if (zipEntry == null)
|
|
|
|
throw new NoSuchElementException(name);
|
|
|
|
|
|
|
|
long start = checkLocalHeader(zipEntry);
|
|
|
|
int method = zipEntry.getMethod();
|
2003-01-02 22:01:24 +01:00
|
|
|
InputStream is = new BufferedInputStream(new PartialInputStream
|
|
|
|
(raf, start, zipEntry.getCompressedSize()));
|
2002-06-15 20:31:13 +02:00
|
|
|
switch (method)
|
|
|
|
{
|
|
|
|
case ZipOutputStream.STORED:
|
|
|
|
return is;
|
|
|
|
case ZipOutputStream.DEFLATED:
|
|
|
|
return new InflaterInputStream(is, new Inflater(true));
|
|
|
|
default:
|
|
|
|
throw new ZipException("Unknown compression method " + method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-11-26 22:55:27 +01:00
|
|
|
* Returns the (path) name of this zip file.
|
2002-06-15 20:31:13 +02:00
|
|
|
*/
|
|
|
|
public String getName()
|
1999-05-06 02:15:47 +02:00
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
return name;
|
1999-05-06 02:15:47 +02:00
|
|
|
}
|
1999-05-18 17:33:03 +02:00
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of entries in this zip file.
|
|
|
|
*/
|
|
|
|
public int size()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-06-15 20:31:13 +02:00
|
|
|
try
|
|
|
|
{
|
2002-12-03 23:06:31 +01:00
|
|
|
return getEntries().size();
|
2002-06-15 20:31:13 +02:00
|
|
|
}
|
2002-12-03 23:06:31 +01:00
|
|
|
catch (IOException ioe)
|
2002-06-15 20:31:13 +02:00
|
|
|
{
|
2002-12-03 23:06:31 +01:00
|
|
|
return 0;
|
2002-06-15 20:31:13 +02:00
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
2002-06-15 20:31:13 +02:00
|
|
|
|
|
|
|
private static class ZipEntryEnumeration implements Enumeration
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
private final Iterator elements;
|
2002-06-15 20:31:13 +02:00
|
|
|
|
2003-01-02 22:01:24 +01:00
|
|
|
public ZipEntryEnumeration(Iterator elements)
|
2002-06-15 20:31:13 +02:00
|
|
|
{
|
2002-12-03 23:06:31 +01:00
|
|
|
this.elements = elements;
|
2002-06-15 20:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasMoreElements()
|
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
return elements.hasNext();
|
2002-06-15 20:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Object nextElement()
|
|
|
|
{
|
2002-12-03 23:06:31 +01:00
|
|
|
/* We return a clone, just to be safe that the user doesn't
|
|
|
|
* change the entry.
|
|
|
|
*/
|
2003-01-02 22:01:24 +01:00
|
|
|
return ((ZipEntry)elements.next()).clone();
|
2002-06-15 20:31:13 +02:00
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2002-06-15 20:31:13 +02:00
|
|
|
private static class PartialInputStream extends InputStream
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2003-01-02 22:01:24 +01:00
|
|
|
private final RandomAccessFile raf;
|
2002-06-15 20:31:13 +02:00
|
|
|
long filepos, end;
|
|
|
|
|
|
|
|
public PartialInputStream(RandomAccessFile raf, long start, long len)
|
|
|
|
{
|
|
|
|
this.raf = raf;
|
|
|
|
filepos = start;
|
|
|
|
end = start + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int available()
|
|
|
|
{
|
|
|
|
long amount = end - filepos;
|
|
|
|
if (amount > Integer.MAX_VALUE)
|
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
return (int) amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int read() throws IOException
|
|
|
|
{
|
|
|
|
if (filepos == end)
|
|
|
|
return -1;
|
|
|
|
synchronized (raf)
|
|
|
|
{
|
|
|
|
raf.seek(filepos++);
|
|
|
|
return raf.read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int read(byte[] b, int off, int len) throws IOException
|
|
|
|
{
|
|
|
|
if (len > end - filepos)
|
|
|
|
{
|
|
|
|
len = (int) (end - filepos);
|
|
|
|
if (len == 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
synchronized (raf)
|
|
|
|
{
|
|
|
|
raf.seek(filepos);
|
|
|
|
int count = raf.read(b, off, len);
|
|
|
|
if (count > 0)
|
|
|
|
filepos += len;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long skip(long amount)
|
|
|
|
{
|
|
|
|
if (amount < 0)
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
if (amount > end - filepos)
|
|
|
|
amount = end - filepos;
|
|
|
|
filepos += amount;
|
|
|
|
return amount;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
}
|