1 |
/* java.util.zip.ZipFile |
/* java.util.zip.ZipFile |
2 |
Copyright (C) 2001 Free Software Foundation, Inc. |
Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
38 |
package java.util.zip; |
package java.util.zip; |
39 |
|
|
40 |
|
import java.io.ByteArrayInputStream; |
41 |
|
import java.io.DataInput; |
42 |
|
import java.io.DataInputStream; |
43 |
import java.io.File; |
import java.io.File; |
44 |
import java.io.InputStream; |
import java.io.InputStream; |
45 |
import java.io.IOException; |
import java.io.IOException; |
137 |
* @exception IOException if a i/o error occured. |
* @exception IOException if a i/o error occured. |
138 |
* @exception EOFException if the file ends prematurely |
* @exception EOFException if the file ends prematurely |
139 |
*/ |
*/ |
140 |
private final int readLeShort() throws IOException { |
private final int readLeShort(DataInput di) throws IOException { |
141 |
return raf.readUnsignedByte() | raf.readUnsignedByte() << 8; |
byte[] b = new byte[2]; |
142 |
|
di.readFully(b); |
143 |
|
return (b[0] & 0xff) | (b[1] & 0xff) << 8; |
144 |
} |
} |
145 |
|
|
146 |
/** |
/** |
148 |
* @exception IOException if a i/o error occured. |
* @exception IOException if a i/o error occured. |
149 |
* @exception EOFException if the file ends prematurely |
* @exception EOFException if the file ends prematurely |
150 |
*/ |
*/ |
151 |
private final int readLeInt() throws IOException { |
private final int readLeInt(DataInput di) throws IOException { |
152 |
return readLeShort() | readLeShort() << 16; |
byte[] b = new byte[4]; |
153 |
|
di.readFully(b); |
154 |
|
return ((b[0] & 0xff) | (b[1] & 0xff) << 8) |
155 |
|
| ((b[2] & 0xff) | (b[3] & 0xff) << 8) << 16; |
156 |
} |
} |
157 |
|
|
158 |
/** |
/** |
173 |
{ |
{ |
174 |
if (pos < 0) |
if (pos < 0) |
175 |
throw new ZipException |
throw new ZipException |
176 |
("central directory not found, probably not a zip file"); |
("central directory not found, probably not a zip file: " + name); |
177 |
raf.seek(pos--); |
raf.seek(pos--); |
178 |
} |
} |
179 |
while (readLeInt() != ENDSIG); |
while (readLeInt(raf) != ENDSIG); |
180 |
if (raf.skipBytes(ENDTOT - ENDNRD) != ENDTOT - ENDNRD) |
if (raf.skipBytes(ENDTOT - ENDNRD) != ENDTOT - ENDNRD) |
181 |
throw new EOFException(); |
throw new EOFException(name); |
182 |
int count = readLeShort(); |
int count = readLeShort(raf); |
183 |
if (raf.skipBytes(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ) |
if (raf.skipBytes(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ) |
184 |
throw new EOFException(); |
throw new EOFException(name); |
185 |
int centralOffset = readLeInt(); |
int centralOffset = readLeInt(raf); |
186 |
|
|
187 |
entries = new ZipEntry[count]; |
entries = new ZipEntry[count]; |
188 |
raf.seek(centralOffset); |
raf.seek(centralOffset); |
189 |
|
byte[] ebs = new byte[24]; |
190 |
|
ByteArrayInputStream ebais = new ByteArrayInputStream(ebs); |
191 |
|
DataInputStream edip = new DataInputStream(ebais); |
192 |
for (int i = 0; i < count; i++) |
for (int i = 0; i < count; i++) |
193 |
{ |
{ |
194 |
if (readLeInt() != CENSIG) |
if (readLeInt(raf) != CENSIG) |
195 |
throw new ZipException("Wrong Central Directory signature"); |
throw new ZipException("Wrong Central Directory signature: " + name); |
196 |
if (raf.skipBytes(CENHOW - CENVEM) != CENHOW - CENVEM) |
if (raf.skipBytes(CENHOW - CENVEM) != CENHOW - CENVEM) |
197 |
throw new EOFException(); |
throw new EOFException(name); |
198 |
int method = readLeShort(); |
|
199 |
int dostime = readLeInt(); |
raf.readFully(ebs); |
200 |
int crc = readLeInt(); |
ebais.reset(); |
201 |
int csize = readLeInt(); |
int method = readLeShort(edip); |
202 |
int size = readLeInt(); |
int dostime = readLeInt(edip); |
203 |
int nameLen = readLeShort(); |
int crc = readLeInt(edip); |
204 |
int extraLen = readLeShort(); |
int csize = readLeInt(edip); |
205 |
int commentLen = readLeShort(); |
int size = readLeInt(edip); |
206 |
|
int nameLen = readLeShort(edip); |
207 |
|
int extraLen = readLeShort(edip); |
208 |
|
int commentLen = readLeShort(edip); |
209 |
|
|
210 |
if (raf.skipBytes(CENOFF - CENDSK) != CENOFF - CENDSK) |
if (raf.skipBytes(CENOFF - CENDSK) != CENOFF - CENDSK) |
211 |
throw new EOFException(); |
throw new EOFException(name); |
212 |
int offset = readLeInt(); |
int offset = readLeInt(raf); |
213 |
|
|
214 |
byte[] buffer = new byte[Math.max(nameLen, commentLen)]; |
byte[] buffer = new byte[Math.max(nameLen, commentLen)]; |
215 |
|
|
260 |
public Enumeration entries() |
public Enumeration entries() |
261 |
{ |
{ |
262 |
if (entries == null) |
if (entries == null) |
263 |
throw new IllegalStateException("ZipFile has closed"); |
throw new IllegalStateException("ZipFile has closed: " + name); |
264 |
return new ZipEntryEnumeration(entries); |
return new ZipEntryEnumeration(entries); |
265 |
} |
} |
266 |
|
|
281 |
public ZipEntry getEntry(String name) |
public ZipEntry getEntry(String name) |
282 |
{ |
{ |
283 |
if (entries == null) |
if (entries == null) |
284 |
throw new IllegalStateException("ZipFile has closed"); |
throw new IllegalStateException("ZipFile has closed: " + name); |
285 |
int index = getEntryIndex(name); |
int index = getEntryIndex(name); |
286 |
return index >= 0 ? (ZipEntry) entries[index].clone() : null; |
return index >= 0 ? (ZipEntry) entries[index].clone() : null; |
287 |
} |
} |
299 |
synchronized (raf) |
synchronized (raf) |
300 |
{ |
{ |
301 |
raf.seek(entry.offset); |
raf.seek(entry.offset); |
302 |
if (readLeInt() != LOCSIG) |
if (readLeInt(raf) != LOCSIG) |
303 |
throw new ZipException("Wrong Local header signature"); |
throw new ZipException("Wrong Local header signature: " + name); |
304 |
|
|
305 |
/* skip version and flags */ |
/* skip version and flags */ |
306 |
if (raf.skipBytes(LOCHOW - LOCVER) != LOCHOW - LOCVER) |
if (raf.skipBytes(LOCHOW - LOCVER) != LOCHOW - LOCVER) |
307 |
throw new EOFException(); |
throw new EOFException(name); |
308 |
|
|
309 |
if (entry.getMethod() != readLeShort()) |
if (entry.getMethod() != readLeShort(raf)) |
310 |
throw new ZipException("Compression method mismatch"); |
throw new ZipException("Compression method mismatch: " + name); |
311 |
|
|
312 |
/* Skip time, crc, size and csize */ |
/* Skip time, crc, size and csize */ |
313 |
if (raf.skipBytes(LOCNAM - LOCTIM) != LOCNAM - LOCTIM) |
if (raf.skipBytes(LOCNAM - LOCTIM) != LOCNAM - LOCTIM) |
314 |
throw new EOFException(); |
throw new EOFException(name); |
315 |
|
|
316 |
if (entry.getName().length() != readLeShort()) |
if (entry.getName().length() != readLeShort(raf)) |
317 |
throw new ZipException("file name length mismatch"); |
throw new ZipException("file name length mismatch: " + name); |
318 |
|
|
319 |
int extraLen = entry.getName().length() + readLeShort(); |
int extraLen = entry.getName().length() + readLeShort(raf); |
320 |
return entry.offset + LOCHDR + extraLen; |
return entry.offset + LOCHDR + extraLen; |
321 |
} |
} |
322 |
} |
} |