jaroslav@609
|
1 |
/*
|
jaroslav@609
|
2 |
* Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@609
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@609
|
4 |
*
|
jaroslav@609
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@609
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@609
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@609
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@609
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@609
|
10 |
*
|
jaroslav@609
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@609
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@609
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@609
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@609
|
15 |
* accompanied this code).
|
jaroslav@609
|
16 |
*
|
jaroslav@609
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@609
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@609
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@609
|
20 |
*
|
jaroslav@609
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@609
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@609
|
23 |
* questions.
|
jaroslav@609
|
24 |
*/
|
jaroslav@609
|
25 |
|
jaroslav@694
|
26 |
package org.apidesign.bck2brwsr.emul.zip;
|
jaroslav@609
|
27 |
|
jaroslav@694
|
28 |
import java.util.zip.*;
|
jaroslav@609
|
29 |
import java.io.InputStream;
|
jaroslav@609
|
30 |
import java.io.IOException;
|
jaroslav@609
|
31 |
import java.io.EOFException;
|
jaroslav@609
|
32 |
import java.io.PushbackInputStream;
|
jaroslav@694
|
33 |
import static org.apidesign.bck2brwsr.emul.zip.ZipConstants64.*;
|
jaroslav@694
|
34 |
import static java.util.zip.ZipInputStream.*;
|
jaroslav@609
|
35 |
|
jaroslav@609
|
36 |
/**
|
jaroslav@609
|
37 |
* This class implements an input stream filter for reading files in the
|
jaroslav@609
|
38 |
* ZIP file format. Includes support for both compressed and uncompressed
|
jaroslav@609
|
39 |
* entries.
|
jaroslav@609
|
40 |
*
|
jaroslav@609
|
41 |
* @author David Connelly
|
jaroslav@609
|
42 |
*/
|
jaroslav@609
|
43 |
public
|
jaroslav@694
|
44 |
class ZipInputStream extends InflaterInputStream {
|
jaroslav@609
|
45 |
private ZipEntry entry;
|
jaroslav@609
|
46 |
private int flag;
|
jaroslav@609
|
47 |
private CRC32 crc = new CRC32();
|
jaroslav@609
|
48 |
private long remaining;
|
jaroslav@609
|
49 |
private byte[] tmpbuf = new byte[512];
|
jaroslav@609
|
50 |
|
jaroslav@609
|
51 |
private static final int STORED = ZipEntry.STORED;
|
jaroslav@609
|
52 |
private static final int DEFLATED = ZipEntry.DEFLATED;
|
jaroslav@609
|
53 |
|
jaroslav@609
|
54 |
private boolean closed = false;
|
jaroslav@609
|
55 |
// this flag is set to true after EOF has reached for
|
jaroslav@609
|
56 |
// one entry
|
jaroslav@609
|
57 |
private boolean entryEOF = false;
|
jaroslav@609
|
58 |
|
jaroslav@609
|
59 |
/**
|
jaroslav@609
|
60 |
* Check to make sure that this stream has not been closed
|
jaroslav@609
|
61 |
*/
|
jaroslav@609
|
62 |
private void ensureOpen() throws IOException {
|
jaroslav@609
|
63 |
if (closed) {
|
jaroslav@609
|
64 |
throw new IOException("Stream closed");
|
jaroslav@609
|
65 |
}
|
jaroslav@609
|
66 |
}
|
jaroslav@609
|
67 |
|
jaroslav@609
|
68 |
/**
|
jaroslav@609
|
69 |
* Creates a new ZIP input stream.
|
jaroslav@609
|
70 |
*
|
jaroslav@609
|
71 |
* <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
|
jaroslav@609
|
72 |
* decode the entry names.
|
jaroslav@609
|
73 |
*
|
jaroslav@609
|
74 |
* @param in the actual input stream
|
jaroslav@609
|
75 |
*/
|
jaroslav@609
|
76 |
public ZipInputStream(InputStream in) {
|
jaroslav@611
|
77 |
// this(in, "UTF-8");
|
jaroslav@611
|
78 |
super(new PushbackInputStream(in, 512), new Inflater(true), 512);
|
jaroslav@694
|
79 |
//usesDefaultInflater = true;
|
jaroslav@611
|
80 |
if(in == null) {
|
jaroslav@611
|
81 |
throw new NullPointerException("in is null");
|
jaroslav@611
|
82 |
}
|
jaroslav@609
|
83 |
}
|
jaroslav@609
|
84 |
|
jaroslav@609
|
85 |
/**
|
jaroslav@609
|
86 |
* Creates a new ZIP input stream.
|
jaroslav@609
|
87 |
*
|
jaroslav@609
|
88 |
* @param in the actual input stream
|
jaroslav@609
|
89 |
*
|
jaroslav@609
|
90 |
* @param charset
|
jaroslav@609
|
91 |
* The {@linkplain java.nio.charset.Charset charset} to be
|
jaroslav@609
|
92 |
* used to decode the ZIP entry name (ignored if the
|
jaroslav@609
|
93 |
* <a href="package-summary.html#lang_encoding"> language
|
jaroslav@609
|
94 |
* encoding bit</a> of the ZIP entry's general purpose bit
|
jaroslav@609
|
95 |
* flag is set).
|
jaroslav@609
|
96 |
*
|
jaroslav@609
|
97 |
* @since 1.7
|
jaroslav@611
|
98 |
*
|
jaroslav@609
|
99 |
public ZipInputStream(InputStream in, Charset charset) {
|
jaroslav@609
|
100 |
super(new PushbackInputStream(in, 512), new Inflater(true), 512);
|
jaroslav@609
|
101 |
usesDefaultInflater = true;
|
jaroslav@609
|
102 |
if(in == null) {
|
jaroslav@609
|
103 |
throw new NullPointerException("in is null");
|
jaroslav@609
|
104 |
}
|
jaroslav@609
|
105 |
if (charset == null)
|
jaroslav@609
|
106 |
throw new NullPointerException("charset is null");
|
jaroslav@609
|
107 |
this.zc = ZipCoder.get(charset);
|
jaroslav@609
|
108 |
}
|
jaroslav@611
|
109 |
*/
|
jaroslav@609
|
110 |
|
jaroslav@609
|
111 |
/**
|
jaroslav@609
|
112 |
* Reads the next ZIP file entry and positions the stream at the
|
jaroslav@609
|
113 |
* beginning of the entry data.
|
jaroslav@609
|
114 |
* @return the next ZIP file entry, or null if there are no more entries
|
jaroslav@609
|
115 |
* @exception ZipException if a ZIP file error has occurred
|
jaroslav@609
|
116 |
* @exception IOException if an I/O error has occurred
|
jaroslav@609
|
117 |
*/
|
jaroslav@609
|
118 |
public ZipEntry getNextEntry() throws IOException {
|
jaroslav@609
|
119 |
ensureOpen();
|
jaroslav@609
|
120 |
if (entry != null) {
|
jaroslav@609
|
121 |
closeEntry();
|
jaroslav@609
|
122 |
}
|
jaroslav@609
|
123 |
crc.reset();
|
jaroslav@609
|
124 |
inf.reset();
|
jaroslav@609
|
125 |
if ((entry = readLOC()) == null) {
|
jaroslav@609
|
126 |
return null;
|
jaroslav@609
|
127 |
}
|
jaroslav@694
|
128 |
if (entry.getMethod() == STORED) {
|
jaroslav@694
|
129 |
remaining = entry.getSize();
|
jaroslav@609
|
130 |
}
|
jaroslav@609
|
131 |
entryEOF = false;
|
jaroslav@609
|
132 |
return entry;
|
jaroslav@609
|
133 |
}
|
jaroslav@609
|
134 |
|
jaroslav@609
|
135 |
/**
|
jaroslav@609
|
136 |
* Closes the current ZIP entry and positions the stream for reading the
|
jaroslav@609
|
137 |
* next entry.
|
jaroslav@609
|
138 |
* @exception ZipException if a ZIP file error has occurred
|
jaroslav@609
|
139 |
* @exception IOException if an I/O error has occurred
|
jaroslav@609
|
140 |
*/
|
jaroslav@609
|
141 |
public void closeEntry() throws IOException {
|
jaroslav@609
|
142 |
ensureOpen();
|
jaroslav@609
|
143 |
while (read(tmpbuf, 0, tmpbuf.length) != -1) ;
|
jaroslav@609
|
144 |
entryEOF = true;
|
jaroslav@609
|
145 |
}
|
jaroslav@609
|
146 |
|
jaroslav@609
|
147 |
/**
|
jaroslav@609
|
148 |
* Returns 0 after EOF has reached for the current entry data,
|
jaroslav@609
|
149 |
* otherwise always return 1.
|
jaroslav@609
|
150 |
* <p>
|
jaroslav@609
|
151 |
* Programs should not count on this method to return the actual number
|
jaroslav@609
|
152 |
* of bytes that could be read without blocking.
|
jaroslav@609
|
153 |
*
|
jaroslav@609
|
154 |
* @return 1 before EOF and 0 after EOF has reached for current entry.
|
jaroslav@609
|
155 |
* @exception IOException if an I/O error occurs.
|
jaroslav@609
|
156 |
*
|
jaroslav@609
|
157 |
*/
|
jaroslav@609
|
158 |
public int available() throws IOException {
|
jaroslav@609
|
159 |
ensureOpen();
|
jaroslav@609
|
160 |
if (entryEOF) {
|
jaroslav@609
|
161 |
return 0;
|
jaroslav@609
|
162 |
} else {
|
jaroslav@609
|
163 |
return 1;
|
jaroslav@609
|
164 |
}
|
jaroslav@609
|
165 |
}
|
jaroslav@609
|
166 |
|
jaroslav@609
|
167 |
/**
|
jaroslav@609
|
168 |
* Reads from the current ZIP entry into an array of bytes.
|
jaroslav@609
|
169 |
* If <code>len</code> is not zero, the method
|
jaroslav@609
|
170 |
* blocks until some input is available; otherwise, no
|
jaroslav@609
|
171 |
* bytes are read and <code>0</code> is returned.
|
jaroslav@609
|
172 |
* @param b the buffer into which the data is read
|
jaroslav@609
|
173 |
* @param off the start offset in the destination array <code>b</code>
|
jaroslav@609
|
174 |
* @param len the maximum number of bytes read
|
jaroslav@609
|
175 |
* @return the actual number of bytes read, or -1 if the end of the
|
jaroslav@609
|
176 |
* entry is reached
|
jaroslav@609
|
177 |
* @exception NullPointerException if <code>b</code> is <code>null</code>.
|
jaroslav@609
|
178 |
* @exception IndexOutOfBoundsException if <code>off</code> is negative,
|
jaroslav@609
|
179 |
* <code>len</code> is negative, or <code>len</code> is greater than
|
jaroslav@609
|
180 |
* <code>b.length - off</code>
|
jaroslav@609
|
181 |
* @exception ZipException if a ZIP file error has occurred
|
jaroslav@609
|
182 |
* @exception IOException if an I/O error has occurred
|
jaroslav@609
|
183 |
*/
|
jaroslav@609
|
184 |
public int read(byte[] b, int off, int len) throws IOException {
|
jaroslav@609
|
185 |
ensureOpen();
|
jaroslav@609
|
186 |
if (off < 0 || len < 0 || off > b.length - len) {
|
jaroslav@609
|
187 |
throw new IndexOutOfBoundsException();
|
jaroslav@609
|
188 |
} else if (len == 0) {
|
jaroslav@609
|
189 |
return 0;
|
jaroslav@609
|
190 |
}
|
jaroslav@609
|
191 |
|
jaroslav@609
|
192 |
if (entry == null) {
|
jaroslav@609
|
193 |
return -1;
|
jaroslav@609
|
194 |
}
|
jaroslav@694
|
195 |
switch (entry.getMethod()) {
|
jaroslav@609
|
196 |
case DEFLATED:
|
jaroslav@609
|
197 |
len = super.read(b, off, len);
|
jaroslav@609
|
198 |
if (len == -1) {
|
jaroslav@609
|
199 |
readEnd(entry);
|
jaroslav@609
|
200 |
entryEOF = true;
|
jaroslav@609
|
201 |
entry = null;
|
jaroslav@609
|
202 |
} else {
|
jaroslav@609
|
203 |
crc.update(b, off, len);
|
jaroslav@609
|
204 |
}
|
jaroslav@609
|
205 |
return len;
|
jaroslav@609
|
206 |
case STORED:
|
jaroslav@609
|
207 |
if (remaining <= 0) {
|
jaroslav@609
|
208 |
entryEOF = true;
|
jaroslav@609
|
209 |
entry = null;
|
jaroslav@609
|
210 |
return -1;
|
jaroslav@609
|
211 |
}
|
jaroslav@609
|
212 |
if (len > remaining) {
|
jaroslav@609
|
213 |
len = (int)remaining;
|
jaroslav@609
|
214 |
}
|
jaroslav@609
|
215 |
len = in.read(b, off, len);
|
jaroslav@609
|
216 |
if (len == -1) {
|
jaroslav@609
|
217 |
throw new ZipException("unexpected EOF");
|
jaroslav@609
|
218 |
}
|
jaroslav@609
|
219 |
crc.update(b, off, len);
|
jaroslav@609
|
220 |
remaining -= len;
|
jaroslav@694
|
221 |
if (remaining == 0 && entry.getCrc() != crc.getValue()) {
|
jaroslav@609
|
222 |
throw new ZipException(
|
jaroslav@694
|
223 |
"invalid entry CRC (expected 0x" + Long.toHexString(entry.getCrc()) +
|
jaroslav@609
|
224 |
" but got 0x" + Long.toHexString(crc.getValue()) + ")");
|
jaroslav@609
|
225 |
}
|
jaroslav@609
|
226 |
return len;
|
jaroslav@609
|
227 |
default:
|
jaroslav@609
|
228 |
throw new ZipException("invalid compression method");
|
jaroslav@609
|
229 |
}
|
jaroslav@609
|
230 |
}
|
jaroslav@609
|
231 |
|
jaroslav@609
|
232 |
/**
|
jaroslav@609
|
233 |
* Skips specified number of bytes in the current ZIP entry.
|
jaroslav@609
|
234 |
* @param n the number of bytes to skip
|
jaroslav@609
|
235 |
* @return the actual number of bytes skipped
|
jaroslav@609
|
236 |
* @exception ZipException if a ZIP file error has occurred
|
jaroslav@609
|
237 |
* @exception IOException if an I/O error has occurred
|
jaroslav@609
|
238 |
* @exception IllegalArgumentException if n < 0
|
jaroslav@609
|
239 |
*/
|
jaroslav@609
|
240 |
public long skip(long n) throws IOException {
|
jaroslav@609
|
241 |
if (n < 0) {
|
jaroslav@609
|
242 |
throw new IllegalArgumentException("negative skip length");
|
jaroslav@609
|
243 |
}
|
jaroslav@609
|
244 |
ensureOpen();
|
jaroslav@609
|
245 |
int max = (int)Math.min(n, Integer.MAX_VALUE);
|
jaroslav@609
|
246 |
int total = 0;
|
jaroslav@609
|
247 |
while (total < max) {
|
jaroslav@609
|
248 |
int len = max - total;
|
jaroslav@609
|
249 |
if (len > tmpbuf.length) {
|
jaroslav@609
|
250 |
len = tmpbuf.length;
|
jaroslav@609
|
251 |
}
|
jaroslav@609
|
252 |
len = read(tmpbuf, 0, len);
|
jaroslav@609
|
253 |
if (len == -1) {
|
jaroslav@609
|
254 |
entryEOF = true;
|
jaroslav@609
|
255 |
break;
|
jaroslav@609
|
256 |
}
|
jaroslav@609
|
257 |
total += len;
|
jaroslav@609
|
258 |
}
|
jaroslav@609
|
259 |
return total;
|
jaroslav@609
|
260 |
}
|
jaroslav@609
|
261 |
|
jaroslav@609
|
262 |
/**
|
jaroslav@609
|
263 |
* Closes this input stream and releases any system resources associated
|
jaroslav@609
|
264 |
* with the stream.
|
jaroslav@609
|
265 |
* @exception IOException if an I/O error has occurred
|
jaroslav@609
|
266 |
*/
|
jaroslav@609
|
267 |
public void close() throws IOException {
|
jaroslav@609
|
268 |
if (!closed) {
|
jaroslav@609
|
269 |
super.close();
|
jaroslav@609
|
270 |
closed = true;
|
jaroslav@609
|
271 |
}
|
jaroslav@609
|
272 |
}
|
jaroslav@609
|
273 |
|
jaroslav@609
|
274 |
private byte[] b = new byte[256];
|
jaroslav@609
|
275 |
|
jaroslav@609
|
276 |
/*
|
jaroslav@609
|
277 |
* Reads local file (LOC) header for next entry.
|
jaroslav@609
|
278 |
*/
|
jaroslav@609
|
279 |
private ZipEntry readLOC() throws IOException {
|
jaroslav@609
|
280 |
try {
|
jaroslav@609
|
281 |
readFully(tmpbuf, 0, LOCHDR);
|
jaroslav@609
|
282 |
} catch (EOFException e) {
|
jaroslav@609
|
283 |
return null;
|
jaroslav@609
|
284 |
}
|
jaroslav@609
|
285 |
if (get32(tmpbuf, 0) != LOCSIG) {
|
jaroslav@609
|
286 |
return null;
|
jaroslav@609
|
287 |
}
|
jaroslav@609
|
288 |
// get flag first, we need check EFS.
|
jaroslav@609
|
289 |
flag = get16(tmpbuf, LOCFLG);
|
jaroslav@609
|
290 |
// get the entry name and create the ZipEntry first
|
jaroslav@609
|
291 |
int len = get16(tmpbuf, LOCNAM);
|
jaroslav@609
|
292 |
int blen = b.length;
|
jaroslav@609
|
293 |
if (len > blen) {
|
jaroslav@609
|
294 |
do
|
jaroslav@609
|
295 |
blen = blen * 2;
|
jaroslav@609
|
296 |
while (len > blen);
|
jaroslav@609
|
297 |
b = new byte[blen];
|
jaroslav@609
|
298 |
}
|
jaroslav@609
|
299 |
readFully(b, 0, len);
|
jaroslav@609
|
300 |
// Force to use UTF-8 if the EFS bit is ON, even the cs is NOT UTF-8
|
jaroslav@609
|
301 |
ZipEntry e = createZipEntry(((flag & EFS) != 0)
|
jaroslav@611
|
302 |
? toStringUTF8(b, len)
|
jaroslav@611
|
303 |
: toString(b, len));
|
jaroslav@609
|
304 |
// now get the remaining fields for the entry
|
jaroslav@609
|
305 |
if ((flag & 1) == 1) {
|
jaroslav@609
|
306 |
throw new ZipException("encrypted ZIP entry not supported");
|
jaroslav@609
|
307 |
}
|
jaroslav@694
|
308 |
e.setMethod(get16(tmpbuf, LOCHOW));
|
jaroslav@694
|
309 |
e.setTime(get32(tmpbuf, LOCTIM));
|
jaroslav@609
|
310 |
if ((flag & 8) == 8) {
|
jaroslav@609
|
311 |
/* "Data Descriptor" present */
|
jaroslav@694
|
312 |
if (e.getMethod() != DEFLATED) {
|
jaroslav@609
|
313 |
throw new ZipException(
|
jaroslav@609
|
314 |
"only DEFLATED entries can have EXT descriptor");
|
jaroslav@609
|
315 |
}
|
jaroslav@609
|
316 |
} else {
|
jaroslav@694
|
317 |
e.setCrc(get32(tmpbuf, LOCCRC));
|
jaroslav@694
|
318 |
e.setCompressedSize(get32(tmpbuf, LOCSIZ));
|
jaroslav@694
|
319 |
e.setSize(get32(tmpbuf, LOCLEN));
|
jaroslav@609
|
320 |
}
|
jaroslav@609
|
321 |
len = get16(tmpbuf, LOCEXT);
|
jaroslav@609
|
322 |
if (len > 0) {
|
jaroslav@609
|
323 |
byte[] bb = new byte[len];
|
jaroslav@609
|
324 |
readFully(bb, 0, len);
|
jaroslav@609
|
325 |
e.setExtra(bb);
|
jaroslav@609
|
326 |
// extra fields are in "HeaderID(2)DataSize(2)Data... format
|
jaroslav@694
|
327 |
if (e.getCompressedSize() == ZIP64_MAGICVAL || e.getCompressedSize() == ZIP64_MAGICVAL) {
|
jaroslav@609
|
328 |
int off = 0;
|
jaroslav@609
|
329 |
while (off + 4 < len) {
|
jaroslav@609
|
330 |
int sz = get16(bb, off + 2);
|
jaroslav@609
|
331 |
if (get16(bb, off) == ZIP64_EXTID) {
|
jaroslav@609
|
332 |
off += 4;
|
jaroslav@609
|
333 |
// LOC extra zip64 entry MUST include BOTH original and
|
jaroslav@609
|
334 |
// compressed file size fields
|
jaroslav@609
|
335 |
if (sz < 16 || (off + sz) > len ) {
|
jaroslav@609
|
336 |
// Invalid zip64 extra fields, simply skip. Even it's
|
jaroslav@609
|
337 |
// rare, it's possible the entry size happens to be
|
jaroslav@609
|
338 |
// the magic value and it "accidnetly" has some bytes
|
jaroslav@609
|
339 |
// in extra match the id.
|
jaroslav@609
|
340 |
return e;
|
jaroslav@609
|
341 |
}
|
jaroslav@694
|
342 |
e.setSize(get64(bb, off));
|
jaroslav@694
|
343 |
e.setCompressedSize(get64(bb, off + 8));
|
jaroslav@609
|
344 |
break;
|
jaroslav@609
|
345 |
}
|
jaroslav@609
|
346 |
off += (sz + 4);
|
jaroslav@609
|
347 |
}
|
jaroslav@609
|
348 |
}
|
jaroslav@609
|
349 |
}
|
jaroslav@609
|
350 |
return e;
|
jaroslav@609
|
351 |
}
|
jaroslav@609
|
352 |
|
jaroslav@609
|
353 |
/**
|
jaroslav@609
|
354 |
* Creates a new <code>ZipEntry</code> object for the specified
|
jaroslav@609
|
355 |
* entry name.
|
jaroslav@609
|
356 |
*
|
jaroslav@609
|
357 |
* @param name the ZIP file entry name
|
jaroslav@609
|
358 |
* @return the ZipEntry just created
|
jaroslav@609
|
359 |
*/
|
jaroslav@609
|
360 |
protected ZipEntry createZipEntry(String name) {
|
jaroslav@609
|
361 |
return new ZipEntry(name);
|
jaroslav@609
|
362 |
}
|
jaroslav@609
|
363 |
|
jaroslav@609
|
364 |
/*
|
jaroslav@609
|
365 |
* Reads end of deflated entry as well as EXT descriptor if present.
|
jaroslav@609
|
366 |
*/
|
jaroslav@609
|
367 |
private void readEnd(ZipEntry e) throws IOException {
|
jaroslav@609
|
368 |
int n = inf.getRemaining();
|
jaroslav@609
|
369 |
if (n > 0) {
|
jaroslav@609
|
370 |
((PushbackInputStream)in).unread(buf, len - n, n);
|
jaroslav@609
|
371 |
}
|
jaroslav@609
|
372 |
if ((flag & 8) == 8) {
|
jaroslav@609
|
373 |
/* "Data Descriptor" present */
|
jaroslav@609
|
374 |
if (inf.getBytesWritten() > ZIP64_MAGICVAL ||
|
jaroslav@609
|
375 |
inf.getBytesRead() > ZIP64_MAGICVAL) {
|
jaroslav@609
|
376 |
// ZIP64 format
|
jaroslav@609
|
377 |
readFully(tmpbuf, 0, ZIP64_EXTHDR);
|
jaroslav@609
|
378 |
long sig = get32(tmpbuf, 0);
|
jaroslav@609
|
379 |
if (sig != EXTSIG) { // no EXTSIG present
|
jaroslav@694
|
380 |
e.setCrc(sig);
|
jaroslav@694
|
381 |
e.setCompressedSize(get64(tmpbuf, ZIP64_EXTSIZ - ZIP64_EXTCRC));
|
jaroslav@694
|
382 |
e.setSize(get64(tmpbuf, ZIP64_EXTLEN - ZIP64_EXTCRC));
|
jaroslav@609
|
383 |
((PushbackInputStream)in).unread(
|
jaroslav@609
|
384 |
tmpbuf, ZIP64_EXTHDR - ZIP64_EXTCRC - 1, ZIP64_EXTCRC);
|
jaroslav@609
|
385 |
} else {
|
jaroslav@694
|
386 |
e.setCrc(get32(tmpbuf, ZIP64_EXTCRC));
|
jaroslav@694
|
387 |
e.setCompressedSize(get64(tmpbuf, ZIP64_EXTSIZ));
|
jaroslav@694
|
388 |
e.setSize(get64(tmpbuf, ZIP64_EXTLEN));
|
jaroslav@609
|
389 |
}
|
jaroslav@609
|
390 |
} else {
|
jaroslav@609
|
391 |
readFully(tmpbuf, 0, EXTHDR);
|
jaroslav@609
|
392 |
long sig = get32(tmpbuf, 0);
|
jaroslav@609
|
393 |
if (sig != EXTSIG) { // no EXTSIG present
|
jaroslav@694
|
394 |
e.setCrc(sig);
|
jaroslav@694
|
395 |
e.setCompressedSize(get32(tmpbuf, EXTSIZ - EXTCRC));
|
jaroslav@694
|
396 |
e.setSize(get32(tmpbuf, EXTLEN - EXTCRC));
|
jaroslav@609
|
397 |
((PushbackInputStream)in).unread(
|
jaroslav@609
|
398 |
tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC);
|
jaroslav@609
|
399 |
} else {
|
jaroslav@694
|
400 |
e.setCrc(get32(tmpbuf, EXTCRC));
|
jaroslav@694
|
401 |
e.setCompressedSize(get32(tmpbuf, EXTSIZ));
|
jaroslav@694
|
402 |
e.setSize(get32(tmpbuf, EXTLEN));
|
jaroslav@609
|
403 |
}
|
jaroslav@609
|
404 |
}
|
jaroslav@609
|
405 |
}
|
jaroslav@694
|
406 |
if (e.getSize() != inf.getBytesWritten()) {
|
jaroslav@609
|
407 |
throw new ZipException(
|
jaroslav@694
|
408 |
"invalid entry size (expected " + e.getSize() +
|
jaroslav@609
|
409 |
" but got " + inf.getBytesWritten() + " bytes)");
|
jaroslav@609
|
410 |
}
|
jaroslav@694
|
411 |
if (e.getCompressedSize() != inf.getBytesRead()) {
|
jaroslav@609
|
412 |
throw new ZipException(
|
jaroslav@694
|
413 |
"invalid entry compressed size (expected " + e.getCompressedSize() +
|
jaroslav@609
|
414 |
" but got " + inf.getBytesRead() + " bytes)");
|
jaroslav@609
|
415 |
}
|
jaroslav@694
|
416 |
if (e.getCrc() != crc.getValue()) {
|
jaroslav@609
|
417 |
throw new ZipException(
|
jaroslav@694
|
418 |
"invalid entry CRC (expected 0x" + Long.toHexString(e.getCrc()) +
|
jaroslav@609
|
419 |
" but got 0x" + Long.toHexString(crc.getValue()) + ")");
|
jaroslav@609
|
420 |
}
|
jaroslav@609
|
421 |
}
|
jaroslav@609
|
422 |
|
jaroslav@609
|
423 |
/*
|
jaroslav@609
|
424 |
* Reads bytes, blocking until all bytes are read.
|
jaroslav@609
|
425 |
*/
|
jaroslav@609
|
426 |
private void readFully(byte[] b, int off, int len) throws IOException {
|
jaroslav@609
|
427 |
while (len > 0) {
|
jaroslav@609
|
428 |
int n = in.read(b, off, len);
|
jaroslav@609
|
429 |
if (n == -1) {
|
jaroslav@609
|
430 |
throw new EOFException();
|
jaroslav@609
|
431 |
}
|
jaroslav@609
|
432 |
off += n;
|
jaroslav@609
|
433 |
len -= n;
|
jaroslav@609
|
434 |
}
|
jaroslav@609
|
435 |
}
|
jaroslav@609
|
436 |
|
jaroslav@609
|
437 |
/*
|
jaroslav@609
|
438 |
* Fetches unsigned 16-bit value from byte array at specified offset.
|
jaroslav@609
|
439 |
* The bytes are assumed to be in Intel (little-endian) byte order.
|
jaroslav@609
|
440 |
*/
|
jaroslav@609
|
441 |
private static final int get16(byte b[], int off) {
|
jaroslav@609
|
442 |
return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
|
jaroslav@609
|
443 |
}
|
jaroslav@609
|
444 |
|
jaroslav@609
|
445 |
/*
|
jaroslav@609
|
446 |
* Fetches unsigned 32-bit value from byte array at specified offset.
|
jaroslav@609
|
447 |
* The bytes are assumed to be in Intel (little-endian) byte order.
|
jaroslav@609
|
448 |
*/
|
jaroslav@609
|
449 |
private static final long get32(byte b[], int off) {
|
jaroslav@609
|
450 |
return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
|
jaroslav@609
|
451 |
}
|
jaroslav@609
|
452 |
|
jaroslav@609
|
453 |
/*
|
jaroslav@609
|
454 |
* Fetches signed 64-bit value from byte array at specified offset.
|
jaroslav@609
|
455 |
* The bytes are assumed to be in Intel (little-endian) byte order.
|
jaroslav@609
|
456 |
*/
|
jaroslav@609
|
457 |
private static final long get64(byte b[], int off) {
|
jaroslav@609
|
458 |
return get32(b, off) | (get32(b, off+4) << 32);
|
jaroslav@609
|
459 |
}
|
jaroslav@611
|
460 |
|
jaroslav@611
|
461 |
private static String toStringUTF8(byte[] arr, int len) {
|
jaroslav@611
|
462 |
return new String(arr, 0, len);
|
jaroslav@611
|
463 |
}
|
jaroslav@611
|
464 |
|
jaroslav@611
|
465 |
private static String toString(byte[] b, int len) {
|
jaroslav@611
|
466 |
return new String(b, 0, len);
|
jaroslav@611
|
467 |
}
|
jaroslav@609
|
468 |
}
|