author | sherman |
Wed, 04 Mar 2009 09:26:41 -0800 | |
changeset 2178 | 5a730fff4d0d |
parent 1228 | 1515928f48cd |
child 2438 | 21c111b51aa8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
2
diff
changeset
|
2 |
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.util.zip; |
|
27 |
||
28 |
import java.io.OutputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.util.Vector; |
|
31 |
import java.util.HashSet; |
|
32 |
||
33 |
/** |
|
34 |
* This class implements an output stream filter for writing files in the |
|
35 |
* ZIP file format. Includes support for both compressed and uncompressed |
|
36 |
* entries. |
|
37 |
* |
|
38 |
* @author David Connelly |
|
39 |
*/ |
|
40 |
public |
|
41 |
class ZipOutputStream extends DeflaterOutputStream implements ZipConstants { |
|
42 |
||
43 |
private static class XEntry { |
|
44 |
public final ZipEntry entry; |
|
45 |
public final long offset; |
|
46 |
public final int flag; |
|
47 |
public XEntry(ZipEntry entry, long offset) { |
|
48 |
this.entry = entry; |
|
49 |
this.offset = offset; |
|
50 |
this.flag = (entry.method == DEFLATED && |
|
51 |
(entry.size == -1 || |
|
52 |
entry.csize == -1 || |
|
53 |
entry.crc == -1)) |
|
54 |
// store size, compressed size, and crc-32 in data descriptor |
|
55 |
// immediately following the compressed entry data |
|
56 |
? 8 |
|
57 |
// store size, compressed size, and crc-32 in LOC header |
|
58 |
: 0; |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
private XEntry current; |
|
63 |
private Vector<XEntry> xentries = new Vector<XEntry>(); |
|
64 |
private HashSet<String> names = new HashSet<String>(); |
|
65 |
private CRC32 crc = new CRC32(); |
|
66 |
private long written = 0; |
|
67 |
private long locoff = 0; |
|
68 |
private String comment; |
|
69 |
private int method = DEFLATED; |
|
70 |
private boolean finished; |
|
71 |
||
72 |
private boolean closed = false; |
|
73 |
||
74 |
private static int version(ZipEntry e) throws ZipException { |
|
75 |
switch (e.method) { |
|
76 |
case DEFLATED: return 20; |
|
77 |
case STORED: return 10; |
|
78 |
default: throw new ZipException("unsupported compression method"); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Checks to make sure that this stream has not been closed. |
|
84 |
*/ |
|
85 |
private void ensureOpen() throws IOException { |
|
86 |
if (closed) { |
|
87 |
throw new IOException("Stream closed"); |
|
88 |
} |
|
89 |
} |
|
90 |
/** |
|
91 |
* Compression method for uncompressed (STORED) entries. |
|
92 |
*/ |
|
93 |
public static final int STORED = ZipEntry.STORED; |
|
94 |
||
95 |
/** |
|
96 |
* Compression method for compressed (DEFLATED) entries. |
|
97 |
*/ |
|
98 |
public static final int DEFLATED = ZipEntry.DEFLATED; |
|
99 |
||
100 |
/** |
|
101 |
* Creates a new ZIP output stream. |
|
102 |
* @param out the actual output stream |
|
103 |
*/ |
|
104 |
public ZipOutputStream(OutputStream out) { |
|
105 |
super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true)); |
|
106 |
usesDefaultDeflater = true; |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Sets the ZIP file comment. |
|
111 |
* @param comment the comment string |
|
112 |
* @exception IllegalArgumentException if the length of the specified |
|
113 |
* ZIP file comment is greater than 0xFFFF bytes |
|
114 |
*/ |
|
115 |
public void setComment(String comment) { |
|
116 |
if (comment != null && comment.length() > 0xffff/3 |
|
117 |
&& getUTF8Length(comment) > 0xffff) { |
|
118 |
throw new IllegalArgumentException("ZIP file comment too long."); |
|
119 |
} |
|
120 |
this.comment = comment; |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Sets the default compression method for subsequent entries. This |
|
125 |
* default will be used whenever the compression method is not specified |
|
126 |
* for an individual ZIP file entry, and is initially set to DEFLATED. |
|
127 |
* @param method the default compression method |
|
128 |
* @exception IllegalArgumentException if the specified compression method |
|
129 |
* is invalid |
|
130 |
*/ |
|
131 |
public void setMethod(int method) { |
|
132 |
if (method != DEFLATED && method != STORED) { |
|
133 |
throw new IllegalArgumentException("invalid compression method"); |
|
134 |
} |
|
135 |
this.method = method; |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Sets the compression level for subsequent entries which are DEFLATED. |
|
140 |
* The default setting is DEFAULT_COMPRESSION. |
|
141 |
* @param level the compression level (0-9) |
|
142 |
* @exception IllegalArgumentException if the compression level is invalid |
|
143 |
*/ |
|
144 |
public void setLevel(int level) { |
|
145 |
def.setLevel(level); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Begins writing a new ZIP file entry and positions the stream to the |
|
150 |
* start of the entry data. Closes the current entry if still active. |
|
151 |
* The default compression method will be used if no compression method |
|
152 |
* was specified for the entry, and the current time will be used if |
|
153 |
* the entry has no set modification time. |
|
154 |
* @param e the ZIP entry to be written |
|
155 |
* @exception ZipException if a ZIP format error has occurred |
|
156 |
* @exception IOException if an I/O error has occurred |
|
157 |
*/ |
|
158 |
public void putNextEntry(ZipEntry e) throws IOException { |
|
159 |
ensureOpen(); |
|
160 |
if (current != null) { |
|
161 |
closeEntry(); // close previous entry |
|
162 |
} |
|
163 |
if (e.time == -1) { |
|
164 |
e.setTime(System.currentTimeMillis()); |
|
165 |
} |
|
166 |
if (e.method == -1) { |
|
167 |
e.method = method; // use default method |
|
168 |
} |
|
169 |
switch (e.method) { |
|
170 |
case DEFLATED: |
|
171 |
break; |
|
172 |
case STORED: |
|
173 |
// compressed size, uncompressed size, and crc-32 must all be |
|
174 |
// set for entries using STORED compression method |
|
175 |
if (e.size == -1) { |
|
176 |
e.size = e.csize; |
|
177 |
} else if (e.csize == -1) { |
|
178 |
e.csize = e.size; |
|
179 |
} else if (e.size != e.csize) { |
|
180 |
throw new ZipException( |
|
181 |
"STORED entry where compressed != uncompressed size"); |
|
182 |
} |
|
183 |
if (e.size == -1 || e.crc == -1) { |
|
184 |
throw new ZipException( |
|
185 |
"STORED entry missing size, compressed size, or crc-32"); |
|
186 |
} |
|
187 |
break; |
|
188 |
default: |
|
189 |
throw new ZipException("unsupported compression method"); |
|
190 |
} |
|
191 |
if (! names.add(e.name)) { |
|
192 |
throw new ZipException("duplicate entry: " + e.name); |
|
193 |
} |
|
194 |
current = new XEntry(e, written); |
|
195 |
xentries.add(current); |
|
196 |
writeLOC(current); |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* Closes the current ZIP entry and positions the stream for writing |
|
201 |
* the next entry. |
|
202 |
* @exception ZipException if a ZIP format error has occurred |
|
203 |
* @exception IOException if an I/O error has occurred |
|
204 |
*/ |
|
205 |
public void closeEntry() throws IOException { |
|
206 |
ensureOpen(); |
|
207 |
if (current != null) { |
|
208 |
ZipEntry e = current.entry; |
|
209 |
switch (e.method) { |
|
210 |
case DEFLATED: |
|
211 |
def.finish(); |
|
212 |
while (!def.finished()) { |
|
213 |
deflate(); |
|
214 |
} |
|
215 |
if ((current.flag & 8) == 0) { |
|
216 |
// verify size, compressed size, and crc-32 settings |
|
217 |
if (e.size != def.getBytesRead()) { |
|
218 |
throw new ZipException( |
|
219 |
"invalid entry size (expected " + e.size + |
|
220 |
" but got " + def.getBytesRead() + " bytes)"); |
|
221 |
} |
|
222 |
if (e.csize != def.getBytesWritten()) { |
|
223 |
throw new ZipException( |
|
224 |
"invalid entry compressed size (expected " + |
|
225 |
e.csize + " but got " + def.getBytesWritten() + " bytes)"); |
|
226 |
} |
|
227 |
if (e.crc != crc.getValue()) { |
|
228 |
throw new ZipException( |
|
229 |
"invalid entry CRC-32 (expected 0x" + |
|
230 |
Long.toHexString(e.crc) + " but got 0x" + |
|
231 |
Long.toHexString(crc.getValue()) + ")"); |
|
232 |
} |
|
233 |
} else { |
|
234 |
e.size = def.getBytesRead(); |
|
235 |
e.csize = def.getBytesWritten(); |
|
236 |
e.crc = crc.getValue(); |
|
237 |
writeEXT(e); |
|
238 |
} |
|
239 |
def.reset(); |
|
240 |
written += e.csize; |
|
241 |
break; |
|
242 |
case STORED: |
|
243 |
// we already know that both e.size and e.csize are the same |
|
244 |
if (e.size != written - locoff) { |
|
245 |
throw new ZipException( |
|
246 |
"invalid entry size (expected " + e.size + |
|
247 |
" but got " + (written - locoff) + " bytes)"); |
|
248 |
} |
|
249 |
if (e.crc != crc.getValue()) { |
|
250 |
throw new ZipException( |
|
251 |
"invalid entry crc-32 (expected 0x" + |
|
252 |
Long.toHexString(e.crc) + " but got 0x" + |
|
253 |
Long.toHexString(crc.getValue()) + ")"); |
|
254 |
} |
|
255 |
break; |
|
256 |
default: |
|
257 |
throw new ZipException("invalid compression method"); |
|
258 |
} |
|
259 |
crc.reset(); |
|
260 |
current = null; |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Writes an array of bytes to the current ZIP entry data. This method |
|
266 |
* will block until all the bytes are written. |
|
267 |
* @param b the data to be written |
|
268 |
* @param off the start offset in the data |
|
269 |
* @param len the number of bytes that are written |
|
270 |
* @exception ZipException if a ZIP file error has occurred |
|
271 |
* @exception IOException if an I/O error has occurred |
|
272 |
*/ |
|
273 |
public synchronized void write(byte[] b, int off, int len) |
|
274 |
throws IOException |
|
275 |
{ |
|
276 |
ensureOpen(); |
|
277 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
278 |
throw new IndexOutOfBoundsException(); |
|
279 |
} else if (len == 0) { |
|
280 |
return; |
|
281 |
} |
|
282 |
||
283 |
if (current == null) { |
|
284 |
throw new ZipException("no current ZIP entry"); |
|
285 |
} |
|
286 |
ZipEntry entry = current.entry; |
|
287 |
switch (entry.method) { |
|
288 |
case DEFLATED: |
|
289 |
super.write(b, off, len); |
|
290 |
break; |
|
291 |
case STORED: |
|
292 |
written += len; |
|
293 |
if (written - locoff > entry.size) { |
|
294 |
throw new ZipException( |
|
295 |
"attempt to write past end of STORED entry"); |
|
296 |
} |
|
297 |
out.write(b, off, len); |
|
298 |
break; |
|
299 |
default: |
|
300 |
throw new ZipException("invalid compression method"); |
|
301 |
} |
|
302 |
crc.update(b, off, len); |
|
303 |
} |
|
304 |
||
305 |
/** |
|
306 |
* Finishes writing the contents of the ZIP output stream without closing |
|
307 |
* the underlying stream. Use this method when applying multiple filters |
|
308 |
* in succession to the same output stream. |
|
309 |
* @exception ZipException if a ZIP file error has occurred |
|
310 |
* @exception IOException if an I/O exception has occurred |
|
311 |
*/ |
|
312 |
public void finish() throws IOException { |
|
313 |
ensureOpen(); |
|
314 |
if (finished) { |
|
315 |
return; |
|
316 |
} |
|
317 |
if (current != null) { |
|
318 |
closeEntry(); |
|
319 |
} |
|
320 |
// write central directory |
|
321 |
long off = written; |
|
322 |
for (XEntry xentry : xentries) |
|
323 |
writeCEN(xentry); |
|
324 |
writeEND(off, written - off); |
|
325 |
finished = true; |
|
326 |
} |
|
327 |
||
328 |
/** |
|
329 |
* Closes the ZIP output stream as well as the stream being filtered. |
|
330 |
* @exception ZipException if a ZIP file error has occurred |
|
331 |
* @exception IOException if an I/O error has occurred |
|
332 |
*/ |
|
333 |
public void close() throws IOException { |
|
334 |
if (!closed) { |
|
335 |
super.close(); |
|
336 |
closed = true; |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
/* |
|
341 |
* Writes local file (LOC) header for specified entry. |
|
342 |
*/ |
|
343 |
private void writeLOC(XEntry xentry) throws IOException { |
|
344 |
ZipEntry e = xentry.entry; |
|
345 |
int flag = xentry.flag; |
|
346 |
writeInt(LOCSIG); // LOC header signature |
|
347 |
writeShort(version(e)); // version needed to extract |
|
348 |
writeShort(flag); // general purpose bit flag |
|
349 |
writeShort(e.method); // compression method |
|
350 |
writeInt(e.time); // last modification time |
|
351 |
if ((flag & 8) == 8) { |
|
352 |
// store size, uncompressed size, and crc-32 in data descriptor |
|
353 |
// immediately following compressed entry data |
|
354 |
writeInt(0); |
|
355 |
writeInt(0); |
|
356 |
writeInt(0); |
|
357 |
} else { |
|
358 |
writeInt(e.crc); // crc-32 |
|
359 |
writeInt(e.csize); // compressed size |
|
360 |
writeInt(e.size); // uncompressed size |
|
361 |
} |
|
362 |
byte[] nameBytes = getUTF8Bytes(e.name); |
|
363 |
writeShort(nameBytes.length); |
|
364 |
writeShort(e.extra != null ? e.extra.length : 0); |
|
365 |
writeBytes(nameBytes, 0, nameBytes.length); |
|
366 |
if (e.extra != null) { |
|
367 |
writeBytes(e.extra, 0, e.extra.length); |
|
368 |
} |
|
369 |
locoff = written; |
|
370 |
} |
|
371 |
||
372 |
/* |
|
373 |
* Writes extra data descriptor (EXT) for specified entry. |
|
374 |
*/ |
|
375 |
private void writeEXT(ZipEntry e) throws IOException { |
|
376 |
writeInt(EXTSIG); // EXT header signature |
|
377 |
writeInt(e.crc); // crc-32 |
|
378 |
writeInt(e.csize); // compressed size |
|
379 |
writeInt(e.size); // uncompressed size |
|
380 |
} |
|
381 |
||
382 |
/* |
|
383 |
* Write central directory (CEN) header for specified entry. |
|
384 |
* REMIND: add support for file attributes |
|
385 |
*/ |
|
386 |
private void writeCEN(XEntry xentry) throws IOException { |
|
387 |
ZipEntry e = xentry.entry; |
|
388 |
int flag = xentry.flag; |
|
389 |
int version = version(e); |
|
390 |
writeInt(CENSIG); // CEN header signature |
|
391 |
writeShort(version); // version made by |
|
392 |
writeShort(version); // version needed to extract |
|
393 |
writeShort(flag); // general purpose bit flag |
|
394 |
writeShort(e.method); // compression method |
|
395 |
writeInt(e.time); // last modification time |
|
396 |
writeInt(e.crc); // crc-32 |
|
397 |
writeInt(e.csize); // compressed size |
|
398 |
writeInt(e.size); // uncompressed size |
|
399 |
byte[] nameBytes = getUTF8Bytes(e.name); |
|
400 |
writeShort(nameBytes.length); |
|
401 |
writeShort(e.extra != null ? e.extra.length : 0); |
|
402 |
byte[] commentBytes; |
|
403 |
if (e.comment != null) { |
|
404 |
commentBytes = getUTF8Bytes(e.comment); |
|
405 |
writeShort(commentBytes.length); |
|
406 |
} else { |
|
407 |
commentBytes = null; |
|
408 |
writeShort(0); |
|
409 |
} |
|
410 |
writeShort(0); // starting disk number |
|
411 |
writeShort(0); // internal file attributes (unused) |
|
412 |
writeInt(0); // external file attributes (unused) |
|
413 |
writeInt(xentry.offset); // relative offset of local header |
|
414 |
writeBytes(nameBytes, 0, nameBytes.length); |
|
415 |
if (e.extra != null) { |
|
416 |
writeBytes(e.extra, 0, e.extra.length); |
|
417 |
} |
|
418 |
if (commentBytes != null) { |
|
419 |
writeBytes(commentBytes, 0, commentBytes.length); |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
/* |
|
424 |
* Writes end of central directory (END) header. |
|
425 |
*/ |
|
426 |
private void writeEND(long off, long len) throws IOException { |
|
427 |
int count = xentries.size(); |
|
428 |
writeInt(ENDSIG); // END record signature |
|
429 |
writeShort(0); // number of this disk |
|
430 |
writeShort(0); // central directory start disk |
|
431 |
writeShort(count); // number of directory entries on disk |
|
432 |
writeShort(count); // total number of directory entries |
|
433 |
writeInt(len); // length of central directory |
|
434 |
writeInt(off); // offset of central directory |
|
435 |
if (comment != null) { // zip file comment |
|
436 |
byte[] b = getUTF8Bytes(comment); |
|
437 |
writeShort(b.length); |
|
438 |
writeBytes(b, 0, b.length); |
|
439 |
} else { |
|
440 |
writeShort(0); |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
/* |
|
445 |
* Writes a 16-bit short to the output stream in little-endian byte order. |
|
446 |
*/ |
|
447 |
private void writeShort(int v) throws IOException { |
|
448 |
OutputStream out = this.out; |
|
449 |
out.write((v >>> 0) & 0xff); |
|
450 |
out.write((v >>> 8) & 0xff); |
|
451 |
written += 2; |
|
452 |
} |
|
453 |
||
454 |
/* |
|
455 |
* Writes a 32-bit int to the output stream in little-endian byte order. |
|
456 |
*/ |
|
457 |
private void writeInt(long v) throws IOException { |
|
458 |
OutputStream out = this.out; |
|
459 |
out.write((int)((v >>> 0) & 0xff)); |
|
460 |
out.write((int)((v >>> 8) & 0xff)); |
|
461 |
out.write((int)((v >>> 16) & 0xff)); |
|
462 |
out.write((int)((v >>> 24) & 0xff)); |
|
463 |
written += 4; |
|
464 |
} |
|
465 |
||
466 |
/* |
|
467 |
* Writes an array of bytes to the output stream. |
|
468 |
*/ |
|
469 |
private void writeBytes(byte[] b, int off, int len) throws IOException { |
|
470 |
super.out.write(b, off, len); |
|
471 |
written += len; |
|
472 |
} |
|
473 |
||
474 |
/* |
|
475 |
* Returns the length of String's UTF8 encoding. |
|
476 |
*/ |
|
477 |
static int getUTF8Length(String s) { |
|
478 |
int count = 0; |
|
479 |
for (int i = 0; i < s.length(); i++) { |
|
480 |
char ch = s.charAt(i); |
|
481 |
if (ch <= 0x7f) { |
|
482 |
count++; |
|
483 |
} else if (ch <= 0x7ff) { |
|
484 |
count += 2; |
|
485 |
} else { |
|
486 |
count += 3; |
|
487 |
} |
|
488 |
} |
|
489 |
return count; |
|
490 |
} |
|
491 |
||
492 |
/* |
|
493 |
* Returns an array of bytes representing the UTF8 encoding |
|
494 |
* of the specified String. |
|
495 |
*/ |
|
496 |
private static byte[] getUTF8Bytes(String s) { |
|
497 |
char[] c = s.toCharArray(); |
|
498 |
int len = c.length; |
|
499 |
// Count the number of encoded bytes... |
|
500 |
int count = 0; |
|
501 |
for (int i = 0; i < len; i++) { |
|
502 |
int ch = c[i]; |
|
503 |
if (ch <= 0x7f) { |
|
504 |
count++; |
|
505 |
} else if (ch <= 0x7ff) { |
|
506 |
count += 2; |
|
507 |
} else { |
|
508 |
count += 3; |
|
509 |
} |
|
510 |
} |
|
511 |
// Now return the encoded bytes... |
|
512 |
byte[] b = new byte[count]; |
|
513 |
int off = 0; |
|
514 |
for (int i = 0; i < len; i++) { |
|
515 |
int ch = c[i]; |
|
516 |
if (ch <= 0x7f) { |
|
517 |
b[off++] = (byte)ch; |
|
518 |
} else if (ch <= 0x7ff) { |
|
519 |
b[off++] = (byte)((ch >> 6) | 0xc0); |
|
520 |
b[off++] = (byte)((ch & 0x3f) | 0x80); |
|
521 |
} else { |
|
522 |
b[off++] = (byte)((ch >> 12) | 0xe0); |
|
523 |
b[off++] = (byte)(((ch >> 6) & 0x3f) | 0x80); |
|
524 |
b[off++] = (byte)((ch & 0x3f) | 0x80); |
|
525 |
} |
|
526 |
} |
|
527 |
return b; |
|
528 |
} |
|
529 |
} |