1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// ZipFile.java - Read contents of a ZIP file.
/* Copyright (C) 1999, 2000 Free Software Foundation
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.*;
/* Written using on-line Java Platform 1.2 API Specification
* and JCL book.
* Believed complete and correct.
*/
public class ZipFile implements ZipConstants
{
public static final int OPEN_READ = 1;
public static final int OPEN_DELETE = 4;
public ZipFile (String fname) throws IOException
{
this(new File(fname));
}
public ZipFile (File f) throws IOException
{
this(f, OPEN_READ);
}
public ZipFile (File f, int mode) throws IOException
{
if (mode != OPEN_READ && mode != (OPEN_READ | OPEN_DELETE))
throw new IllegalArgumentException
("mode can only be OPEN_READ or OPEN_READ | OPEN_DELETE");
if ((mode & OPEN_DELETE) != 0)
{
delete_on_close = f;
f.deleteOnExit();
}
else
{
delete_on_close = null;
}
file = new RandomAccessFile(f, "r");
name = f.getName();
readDirectory ();
}
void readDirectory () throws IOException
{
long size = file.length ();
if (size < ZipConstants.END_CENTRAL_DIR_SIZE)
throw new ZipException ("zipfile too short");
// We do not handle a "zipfile comment", which the appnote says can
// be at the end of a .zip file. We could handle this by seeking
// to the beginning and reading forwards.
file.seek(size - ZipConstants.END_CENTRAL_DIR_SIZE);
if (file.read() != 'P'
|| file.read() != 'K'
|| file.read() != '\005'
|| file.read() != '\006')
throw new ZipException("not a valid zipfile");
file.skipBytes(6);
numEntries = readu2();
int dir_size = read4 (); // Read "size of the central directory".
file.seek(size - (dir_size + ZipConstants.END_CENTRAL_DIR_SIZE));
ZipEntry last = null;
for (int i = 0; i < numEntries; i++)
{
file.skipBytes(10);
int method = readu2();
int modtime = readu2();
int moddate = readu2();
int crc = read4();
int compressedSize = read4();
int uncompressedSize = read4();
int filenameLength = readu2();
int extraLength = readu2();
int commentLength = readu2();
int diskNumberStart = readu2();
int intAttributes = readu2();
int extAttributes = read4();
int relativeOffset = read4();
byte[] bname = new byte[filenameLength];
file.readFully(bname);
ZipEntry entry = new ZipEntry(new String(bname, "8859_1"));
if (extraLength > 0)
{
byte[] bextra = new byte[extraLength];
file.readFully(bextra);
entry.extra = bextra;
}
if (commentLength > 0)
{
byte[] bcomment = new byte[commentLength];
file.readFully(bcomment);
entry.comment = new String(bcomment, "8859_1");
}
entry.compressedSize = compressedSize;
entry.size = uncompressedSize;
entry.crc = (long) crc & 0xffffffffL;
entry.method = method;
entry.relativeOffset = relativeOffset;
entry.time = ZipEntry.timeFromDOS(moddate, modtime);
if (last == null)
entries = entry;
else
last.next = entry;
last = entry;
}
}
public java.util.Enumeration entries()
{
return new ZipEnumeration(this);
}
public void close() throws IOException
{
file.close();
entries = null;
numEntries = 0;
if (delete_on_close != null)
delete_on_close.delete();
}
public ZipEntry getEntry(String name)
{
for (ZipEntry entry = entries; entry != null; entry = entry.next)
{
if (name.equals(entry.getName()))
return entry;
}
return null;
}
public InputStream getInputStream(ZipEntry ze) throws IOException
{
byte[] buffer = new byte[(int) ze.getCompressedSize()];
/* Read the size of the extra field, and skip to the start of the
data. */
file.seek (ze.relativeOffset + ZipConstants.LOCAL_FILE_HEADER_SIZE - 2);
int extraFieldLength = readu2();
file.skipBytes (ze.getName().length() + extraFieldLength);
file.readFully(buffer);
InputStream is = new ByteArrayInputStream (buffer);
if (ze.getMethod() == ZipEntry.DEFLATED)
// Data in zipfile entries does not have a zlib header, so construct
// an Inflater with the `nowrapper' option.
is = new InflaterInputStream (is, new Inflater (true), 512);
return is;
}
public String getName ()
{
return name;
}
public int size ()
{
if (entries == null)
throw new IllegalStateException("ZipFile already closed");
else
return numEntries;
}
protected void finalize () throws IOException
{
close();
}
private int readu2 () throws IOException
{
int byte0 = file.read();
int byte1 = file.read();
if (byte0 < 0 || byte1 < 0)
throw new ZipException (".zip archive ended prematurely");
return ((byte1 & 0xFF) << 8) | (byte0 & 0xFF);
}
private int read4 () throws IOException
{
int byte0 = file.read();
int byte1 = file.read();
int byte2 = file.read();
int byte3 = file.read();
if (byte3 < 0)
throw new ZipException (".zip archive ended prematurely");
return ((byte3 & 0xFF) << 24) + ((byte2 & 0xFF) << 16)
+ ((byte1 & 0xFF) << 8) + (byte0 & 0xFF);
}
ZipEntry entries;
int numEntries;
RandomAccessFile file;
String name;
/** File to delete on close or null. */
File delete_on_close;
}
final class ZipEnumeration implements java.util.Enumeration
{
ZipEntry entry;
ZipEnumeration (ZipFile zfile)
{
entry = zfile.entries;
}
public boolean hasMoreElements ()
{
return entry != null;
}
public Object nextElement ()
{
ZipEntry cur = entry;
if (cur == null)
throw new java.util.NoSuchElementException();
entry = cur.next;
return cur;
}
}
|