| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.apache.tools.zip; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.io.FileOutputStream; |
| 23 |
import java.io.FilterOutputStream; |
| 24 |
import java.io.IOException; |
| 25 |
import java.io.OutputStream; |
| 26 |
import java.io.RandomAccessFile; |
| 27 |
import java.nio.ByteBuffer; |
| 28 |
import java.util.Date; |
| 29 |
import java.util.HashMap; |
| 30 |
import java.util.Iterator; |
| 31 |
import java.util.LinkedList; |
| 32 |
import java.util.List; |
| 33 |
import java.util.Map; |
| 34 |
import java.util.zip.CRC32; |
| 35 |
import java.util.zip.Deflater; |
| 36 |
import java.util.zip.ZipException; |
| 37 |
|
| 38 |
/** |
| 39 |
* Reimplementation of {@link java.util.zip.ZipOutputStream |
| 40 |
* java.util.zip.ZipOutputStream} that does handle the extended |
| 41 |
* functionality of this package, especially internal/external file |
| 42 |
* attributes and extra fields with different layouts for local file |
| 43 |
* data and central directory entries. |
| 44 |
* |
| 45 |
* <p>This class will try to use {@link java.io.RandomAccessFile |
| 46 |
* RandomAccessFile} when you know that the output is going to go to a |
| 47 |
* file.</p> |
| 48 |
* |
| 49 |
* <p>If RandomAccessFile cannot be used, this implementation will use |
| 50 |
* a Data Descriptor to store size and CRC information for {@link |
| 51 |
* #DEFLATED DEFLATED} entries, this means, you don't need to |
| 52 |
* calculate them yourself. Unfortunately this is not possible for |
| 53 |
* the {@link #STORED STORED} method, here setting the CRC and |
| 54 |
* uncompressed size information is required before {@link |
| 55 |
* #putNextEntry putNextEntry} can be called.</p> |
| 56 |
* |
| 57 |
*/ |
| 58 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
| 59 |
public class ZipOutputStream extends FilterOutputStream { |
| 60 |
|
| 61 |
private static final int BYTE_MASK = 0xFF; |
| 62 |
private static final int SHORT = 2; |
| 63 |
private static final int WORD = 4; |
| 64 |
private static final int BUFFER_SIZE = 512; |
| 65 |
/* |
| 66 |
* Apparently Deflater.setInput gets slowed down a lot on Sun JVMs |
| 67 |
* when it gets handed a really big buffer. See |
| 68 |
* https://issues.apache.org/bugzilla/show_bug.cgi?id=45396 |
| 69 |
* |
| 70 |
* Using a buffer size of 8 kB proved to be a good compromise |
| 71 |
*/ |
| 72 |
private static final int DEFLATER_BLOCK_SIZE = 8192; |
| 73 |
|
| 74 |
/** |
| 75 |
* Compression method for deflated entries. |
| 76 |
* |
| 77 |
* @since 1.1 |
| 78 |
*/ |
| 79 |
public static final int DEFLATED = java.util.zip.ZipEntry.DEFLATED; |
| 80 |
|
| 81 |
/** |
| 82 |
* Default compression level for deflated entries. |
| 83 |
* |
| 84 |
* @since Ant 1.7 |
| 85 |
*/ |
| 86 |
public static final int DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION; |
| 87 |
|
| 88 |
/** |
| 89 |
* Compression method for stored entries. |
| 90 |
* |
| 91 |
* @since 1.1 |
| 92 |
*/ |
| 93 |
public static final int STORED = java.util.zip.ZipEntry.STORED; |
| 94 |
|
| 95 |
/** |
| 96 |
* default encoding for file names and comment. |
| 97 |
*/ |
| 98 |
static final String DEFAULT_ENCODING = null; |
| 99 |
|
| 100 |
/** |
| 101 |
* General purpose flag, which indicates that filenames are |
| 102 |
* written in utf-8. |
| 103 |
*/ |
| 104 |
public static final int UFT8_NAMES_FLAG = 1 << 11; |
| 105 |
|
| 106 |
/** |
| 107 |
* General purpose flag, which indicates that filenames are |
| 108 |
* written in utf-8. |
| 109 |
* @deprecated use {@link #UFT8_NAMES_FLAG} instead |
| 110 |
*/ |
| 111 |
public static final int EFS_FLAG = UFT8_NAMES_FLAG; |
| 112 |
|
| 113 |
/** |
| 114 |
* Current entry. |
| 115 |
* |
| 116 |
* @since 1.1 |
| 117 |
*/ |
| 118 |
private ZipEntry entry; |
| 119 |
|
| 120 |
/** |
| 121 |
* The file comment. |
| 122 |
* |
| 123 |
* @since 1.1 |
| 124 |
*/ |
| 125 |
private String comment = ""; |
| 126 |
|
| 127 |
/** |
| 128 |
* Compression level for next entry. |
| 129 |
* |
| 130 |
* @since 1.1 |
| 131 |
*/ |
| 132 |
private int level = DEFAULT_COMPRESSION; |
| 133 |
|
| 134 |
/** |
| 135 |
* Has the compression level changed when compared to the last |
| 136 |
* entry? |
| 137 |
* |
| 138 |
* @since 1.5 |
| 139 |
*/ |
| 140 |
private boolean hasCompressionLevelChanged = false; |
| 141 |
|
| 142 |
/** |
| 143 |
* Default compression method for next entry. |
| 144 |
* |
| 145 |
* @since 1.1 |
| 146 |
*/ |
| 147 |
private int method = java.util.zip.ZipEntry.DEFLATED; |
| 148 |
|
| 149 |
/** |
| 150 |
* List of ZipEntries written so far. |
| 151 |
* |
| 152 |
* @since 1.1 |
| 153 |
*/ |
| 154 |
private final List entries = new LinkedList(); |
| 155 |
|
| 156 |
/** |
| 157 |
* CRC instance to avoid parsing DEFLATED data twice. |
| 158 |
* |
| 159 |
* @since 1.1 |
| 160 |
*/ |
| 161 |
private final CRC32 crc = new CRC32(); |
| 162 |
|
| 163 |
/** |
| 164 |
* Count the bytes written to out. |
| 165 |
* |
| 166 |
* @since 1.1 |
| 167 |
*/ |
| 168 |
private long written = 0; |
| 169 |
|
| 170 |
/** |
| 171 |
* Data for local header data |
| 172 |
* |
| 173 |
* @since 1.1 |
| 174 |
*/ |
| 175 |
private long dataStart = 0; |
| 176 |
|
| 177 |
/** |
| 178 |
* Offset for CRC entry in the local file header data for the |
| 179 |
* current entry starts here. |
| 180 |
* |
| 181 |
* @since 1.15 |
| 182 |
*/ |
| 183 |
private long localDataStart = 0; |
| 184 |
|
| 185 |
/** |
| 186 |
* Start of central directory. |
| 187 |
* |
| 188 |
* @since 1.1 |
| 189 |
*/ |
| 190 |
private long cdOffset = 0; |
| 191 |
|
| 192 |
/** |
| 193 |
* Length of central directory. |
| 194 |
* |
| 195 |
* @since 1.1 |
| 196 |
*/ |
| 197 |
private long cdLength = 0; |
| 198 |
|
| 199 |
/** |
| 200 |
* Helper, a 0 as ZipShort. |
| 201 |
* |
| 202 |
* @since 1.1 |
| 203 |
*/ |
| 204 |
private static final byte[] ZERO = {0, 0}; |
| 205 |
|
| 206 |
/** |
| 207 |
* Helper, a 0 as ZipLong. |
| 208 |
* |
| 209 |
* @since 1.1 |
| 210 |
*/ |
| 211 |
private static final byte[] LZERO = {0, 0, 0, 0}; |
| 212 |
|
| 213 |
/** |
| 214 |
* Holds the offsets of the LFH starts for each entry. |
| 215 |
* |
| 216 |
* @since 1.1 |
| 217 |
*/ |
| 218 |
private final Map offsets = new HashMap(); |
| 219 |
|
| 220 |
/** |
| 221 |
* The encoding to use for filenames and the file comment. |
| 222 |
* |
| 223 |
* <p>For a list of possible values see <a |
| 224 |
* href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html</a>. |
| 225 |
* Defaults to the platform's default character encoding.</p> |
| 226 |
* |
| 227 |
* @since 1.3 |
| 228 |
*/ |
| 229 |
private String encoding = null; |
| 230 |
|
| 231 |
/** |
| 232 |
* The zip encoding to use for filenames and the file comment. |
| 233 |
* |
| 234 |
* This field is of internal use and will be set in {@link |
| 235 |
* #setEncoding(String)}. |
| 236 |
*/ |
| 237 |
private ZipEncoding zipEncoding = |
| 238 |
ZipEncodingHelper.getZipEncoding(DEFAULT_ENCODING); |
| 239 |
|
| 240 |
// CheckStyle:VisibilityModifier OFF - bc |
| 241 |
|
| 242 |
/** |
| 243 |
* This Deflater object is used for output. |
| 244 |
* |
| 245 |
* <p>This attribute is only protected to provide a level of API |
| 246 |
* backwards compatibility. This class used to extend {@link |
| 247 |
* java.util.zip.DeflaterOutputStream DeflaterOutputStream} up to |
| 248 |
* Revision 1.13.</p> |
| 249 |
* |
| 250 |
* @since 1.14 |
| 251 |
*/ |
| 252 |
protected Deflater def = new Deflater(level, true); |
| 253 |
|
| 254 |
/** |
| 255 |
* This buffer servers as a Deflater. |
| 256 |
* |
| 257 |
* <p>This attribute is only protected to provide a level of API |
| 258 |
* backwards compatibility. This class used to extend {@link |
| 259 |
* java.util.zip.DeflaterOutputStream DeflaterOutputStream} up to |
| 260 |
* Revision 1.13.</p> |
| 261 |
* |
| 262 |
* @since 1.14 |
| 263 |
*/ |
| 264 |
protected byte[] buf = new byte[BUFFER_SIZE]; |
| 265 |
|
| 266 |
// CheckStyle:VisibilityModifier ON |
| 267 |
|
| 268 |
/** |
| 269 |
* Optional random access output. |
| 270 |
* |
| 271 |
* @since 1.14 |
| 272 |
*/ |
| 273 |
private RandomAccessFile raf = null; |
| 274 |
|
| 275 |
/** |
| 276 |
* whether to use the general purpose bit flag when writing UTF-8 |
| 277 |
* filenames or not. |
| 278 |
*/ |
| 279 |
private boolean useUTF8Flag = true; |
| 280 |
|
| 281 |
/** |
| 282 |
* Whether to encode non-encodable file names as UTF-8. |
| 283 |
*/ |
| 284 |
private boolean fallbackToUTF8 = false; |
| 285 |
|
| 286 |
/** |
| 287 |
* whether to create UnicodePathExtraField-s for each entry. |
| 288 |
*/ |
| 289 |
private UnicodeExtraFieldPolicy createUnicodeExtraFields = |
| 290 |
UnicodeExtraFieldPolicy.NEVER; |
| 291 |
|
| 292 |
/** |
| 293 |
* Creates a new ZIP OutputStream filtering the underlying stream. |
| 294 |
* @param out the outputstream to zip |
| 295 |
* @since 1.1 |
| 296 |
*/ |
| 297 |
public ZipOutputStream(OutputStream out) { |
| 298 |
super(out); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Creates a new ZIP OutputStream writing to a File. Will use |
| 303 |
* random access if possible. |
| 304 |
* @param file the file to zip to |
| 305 |
* @since 1.14 |
| 306 |
* @throws IOException on error |
| 307 |
*/ |
| 308 |
public ZipOutputStream(File file) throws IOException { |
| 309 |
super(null); |
| 310 |
|
| 311 |
try { |
| 312 |
raf = new RandomAccessFile(file, "rw"); |
| 313 |
raf.setLength(0); |
| 314 |
} catch (IOException e) { |
| 315 |
if (raf != null) { |
| 316 |
try { |
| 317 |
raf.close(); |
| 318 |
} catch (IOException inner) { |
| 319 |
// ignore |
| 320 |
} |
| 321 |
raf = null; |
| 322 |
} |
| 323 |
out = new FileOutputStream(file); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* This method indicates whether this archive is writing to a |
| 329 |
* seekable stream (i.e., to a random access file). |
| 330 |
* |
| 331 |
* <p>For seekable streams, you don't need to calculate the CRC or |
| 332 |
* uncompressed size for {@link #STORED} entries before |
| 333 |
* invoking {@link #putNextEntry}. |
| 334 |
* @return true if seekable |
| 335 |
* @since 1.17 |
| 336 |
*/ |
| 337 |
public boolean isSeekable() { |
| 338 |
return raf != null; |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* The encoding to use for filenames and the file comment. |
| 343 |
* |
| 344 |
* <p>For a list of possible values see <a |
| 345 |
* href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html</a>. |
| 346 |
* Defaults to the platform's default character encoding.</p> |
| 347 |
* @param encoding the encoding value |
| 348 |
* @since 1.3 |
| 349 |
*/ |
| 350 |
public void setEncoding(final String encoding) { |
| 351 |
this.encoding = encoding; |
| 352 |
this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); |
| 353 |
useUTF8Flag &= ZipEncodingHelper.isUTF8(encoding); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* The encoding to use for filenames and the file comment. |
| 358 |
* |
| 359 |
* @return null if using the platform's default character encoding. |
| 360 |
* |
| 361 |
* @since 1.3 |
| 362 |
*/ |
| 363 |
public String getEncoding() { |
| 364 |
return encoding; |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Whether to set the language encoding flag if the file name |
| 369 |
* encoding is UTF-8. |
| 370 |
* |
| 371 |
* <p>Defaults to true.</p> |
| 372 |
*/ |
| 373 |
public void setUseLanguageEncodingFlag(boolean b) { |
| 374 |
useUTF8Flag = b && ZipEncodingHelper.isUTF8(encoding); |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Whether to create Unicode Extra Fields. |
| 379 |
* |
| 380 |
* <p>Defaults to NEVER.</p> |
| 381 |
*/ |
| 382 |
public void setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy b) { |
| 383 |
createUnicodeExtraFields = b; |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Whether to fall back to UTF and the language encoding flag if |
| 388 |
* the file name cannot be encoded using the specified encoding. |
| 389 |
* |
| 390 |
* <p>Defaults to false.</p> |
| 391 |
*/ |
| 392 |
public void setFallbackToUTF8(boolean b) { |
| 393 |
fallbackToUTF8 = b; |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Finishs writing the contents and closes this as well as the |
| 398 |
* underlying stream. |
| 399 |
* |
| 400 |
* @since 1.1 |
| 401 |
* @throws IOException on error |
| 402 |
*/ |
| 403 |
public void finish() throws IOException { |
| 404 |
closeEntry(); |
| 405 |
cdOffset = written; |
| 406 |
for (Iterator i = entries.iterator(); i.hasNext(); ) { |
| 407 |
writeCentralFileHeader((ZipEntry) i.next()); |
| 408 |
} |
| 409 |
cdLength = written - cdOffset; |
| 410 |
writeCentralDirectoryEnd(); |
| 411 |
offsets.clear(); |
| 412 |
entries.clear(); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Writes all necessary data for this entry. |
| 417 |
* |
| 418 |
* @since 1.1 |
| 419 |
* @throws IOException on error |
| 420 |
*/ |
| 421 |
public void closeEntry() throws IOException { |
| 422 |
if (entry == null) { |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
long realCrc = crc.getValue(); |
| 427 |
crc.reset(); |
| 428 |
|
| 429 |
if (entry.getMethod() == DEFLATED) { |
| 430 |
def.finish(); |
| 431 |
while (!def.finished()) { |
| 432 |
deflate(); |
| 433 |
} |
| 434 |
|
| 435 |
entry.setSize(adjustToLong(def.getTotalIn())); |
| 436 |
entry.setCompressedSize(adjustToLong(def.getTotalOut())); |
| 437 |
entry.setCrc(realCrc); |
| 438 |
|
| 439 |
def.reset(); |
| 440 |
|
| 441 |
written += entry.getCompressedSize(); |
| 442 |
} else if (raf == null) { |
| 443 |
if (entry.getCrc() != realCrc) { |
| 444 |
throw new ZipException("bad CRC checksum for entry " |
| 445 |
+ entry.getName() + ": " |
| 446 |
+ Long.toHexString(entry.getCrc()) |
| 447 |
+ " instead of " |
| 448 |
+ Long.toHexString(realCrc)); |
| 449 |
} |
| 450 |
|
| 451 |
if (entry.getSize() != written - dataStart) { |
| 452 |
throw new ZipException("bad size for entry " |
| 453 |
+ entry.getName() + ": " |
| 454 |
+ entry.getSize() |
| 455 |
+ " instead of " |
| 456 |
+ (written - dataStart)); |
| 457 |
} |
| 458 |
} else { /* method is STORED and we used RandomAccessFile */ |
| 459 |
long size = written - dataStart; |
| 460 |
|
| 461 |
entry.setSize(size); |
| 462 |
entry.setCompressedSize(size); |
| 463 |
entry.setCrc(realCrc); |
| 464 |
} |
| 465 |
|
| 466 |
// If random access output, write the local file header containing |
| 467 |
// the correct CRC and compressed/uncompressed sizes |
| 468 |
if (raf != null) { |
| 469 |
long save = raf.getFilePointer(); |
| 470 |
|
| 471 |
raf.seek(localDataStart); |
| 472 |
writeOut(ZipLong.getBytes(entry.getCrc())); |
| 473 |
writeOut(ZipLong.getBytes(entry.getCompressedSize())); |
| 474 |
writeOut(ZipLong.getBytes(entry.getSize())); |
| 475 |
raf.seek(save); |
| 476 |
} |
| 477 |
|
| 478 |
writeDataDescriptor(entry); |
| 479 |
entry = null; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Begin writing next entry. |
| 484 |
* @param ze the entry to write |
| 485 |
* @since 1.1 |
| 486 |
* @throws IOException on error |
| 487 |
*/ |
| 488 |
public void putNextEntry(ZipEntry ze) throws IOException { |
| 489 |
closeEntry(); |
| 490 |
|
| 491 |
entry = ze; |
| 492 |
entries.add(entry); |
| 493 |
|
| 494 |
if (entry.getMethod() == -1) { // not specified |
| 495 |
entry.setMethod(method); |
| 496 |
} |
| 497 |
|
| 498 |
if (entry.getTime() == -1) { // not specified |
| 499 |
entry.setTime(System.currentTimeMillis()); |
| 500 |
} |
| 501 |
|
| 502 |
// Size/CRC not required if RandomAccessFile is used |
| 503 |
if (entry.getMethod() == STORED && raf == null) { |
| 504 |
if (entry.getSize() == -1) { |
| 505 |
throw new ZipException("uncompressed size is required for" |
| 506 |
+ " STORED method when not writing to a" |
| 507 |
+ " file"); |
| 508 |
} |
| 509 |
if (entry.getCrc() == -1) { |
| 510 |
throw new ZipException("crc checksum is required for STORED" |
| 511 |
+ " method when not writing to a file"); |
| 512 |
} |
| 513 |
entry.setCompressedSize(entry.getSize()); |
| 514 |
} |
| 515 |
|
| 516 |
if (entry.getMethod() == DEFLATED && hasCompressionLevelChanged) { |
| 517 |
def.setLevel(level); |
| 518 |
hasCompressionLevelChanged = false; |
| 519 |
} |
| 520 |
writeLocalFileHeader(entry); |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Set the file comment. |
| 525 |
* @param comment the comment |
| 526 |
* @since 1.1 |
| 527 |
*/ |
| 528 |
public void setComment(String comment) { |
| 529 |
this.comment = comment; |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* Sets the compression level for subsequent entries. |
| 534 |
* |
| 535 |
* <p>Default is Deflater.DEFAULT_COMPRESSION.</p> |
| 536 |
* @param level the compression level. |
| 537 |
* @throws IllegalArgumentException if an invalid compression |
| 538 |
* level is specified. |
| 539 |
* @since 1.1 |
| 540 |
*/ |
| 541 |
public void setLevel(int level) { |
| 542 |
if (level < Deflater.DEFAULT_COMPRESSION |
| 543 |
|| level > Deflater.BEST_COMPRESSION) { |
| 544 |
throw new IllegalArgumentException("Invalid compression level: " |
| 545 |
+ level); |
| 546 |
} |
| 547 |
hasCompressionLevelChanged = (this.level != level); |
| 548 |
this.level = level; |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* Sets the default compression method for subsequent entries. |
| 553 |
* |
| 554 |
* <p>Default is DEFLATED.</p> |
| 555 |
* @param method an <code>int</code> from java.util.zip.ZipEntry |
| 556 |
* @since 1.1 |
| 557 |
*/ |
| 558 |
public void setMethod(int method) { |
| 559 |
this.method = method; |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Writes bytes to ZIP entry. |
| 564 |
* @param b the byte array to write |
| 565 |
* @param offset the start position to write from |
| 566 |
* @param length the number of bytes to write |
| 567 |
* @throws IOException on error |
| 568 |
*/ |
| 569 |
public void write(byte[] b, int offset, int length) throws IOException { |
| 570 |
if (entry.getMethod() == DEFLATED) { |
| 571 |
if (length > 0) { |
| 572 |
if (!def.finished()) { |
| 573 |
if (length <= DEFLATER_BLOCK_SIZE) { |
| 574 |
def.setInput(b, offset, length); |
| 575 |
deflateUntilInputIsNeeded(); |
| 576 |
} else { |
| 577 |
final int fullblocks = length / DEFLATER_BLOCK_SIZE; |
| 578 |
for (int i = 0; i < fullblocks; i++) { |
| 579 |
def.setInput(b, offset + i * DEFLATER_BLOCK_SIZE, |
| 580 |
DEFLATER_BLOCK_SIZE); |
| 581 |
deflateUntilInputIsNeeded(); |
| 582 |
} |
| 583 |
final int done = fullblocks * DEFLATER_BLOCK_SIZE; |
| 584 |
if (done < length) { |
| 585 |
def.setInput(b, offset + done, length - done); |
| 586 |
deflateUntilInputIsNeeded(); |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
} else { |
| 592 |
writeOut(b, offset, length); |
| 593 |
written += length; |
| 594 |
} |
| 595 |
crc.update(b, offset, length); |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Writes a single byte to ZIP entry. |
| 600 |
* |
| 601 |
* <p>Delegates to the three arg method.</p> |
| 602 |
* @param b the byte to write |
| 603 |
* @since 1.14 |
| 604 |
* @throws IOException on error |
| 605 |
*/ |
| 606 |
public void write(int b) throws IOException { |
| 607 |
byte[] buff = new byte[1]; |
| 608 |
buff[0] = (byte) (b & BYTE_MASK); |
| 609 |
write(buff, 0, 1); |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* Closes this output stream and releases any system resources |
| 614 |
* associated with the stream. |
| 615 |
* |
| 616 |
* @exception IOException if an I/O error occurs. |
| 617 |
* @since 1.14 |
| 618 |
*/ |
| 619 |
public void close() throws IOException { |
| 620 |
finish(); |
| 621 |
|
| 622 |
if (raf != null) { |
| 623 |
raf.close(); |
| 624 |
} |
| 625 |
if (out != null) { |
| 626 |
out.close(); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
* Flushes this output stream and forces any buffered output bytes |
| 632 |
* to be written out to the stream. |
| 633 |
* |
| 634 |
* @exception IOException if an I/O error occurs. |
| 635 |
* @since 1.14 |
| 636 |
*/ |
| 637 |
public void flush() throws IOException { |
| 638 |
if (out != null) { |
| 639 |
out.flush(); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
/* |
| 644 |
* Various ZIP constants |
| 645 |
*/ |
| 646 |
/** |
| 647 |
* local file header signature |
| 648 |
* |
| 649 |
* @since 1.1 |
| 650 |
*/ |
| 651 |
protected static final byte[] LFH_SIG = ZipLong.getBytes(0X04034B50L); |
| 652 |
/** |
| 653 |
* data descriptor signature |
| 654 |
* |
| 655 |
* @since 1.1 |
| 656 |
*/ |
| 657 |
protected static final byte[] DD_SIG = ZipLong.getBytes(0X08074B50L); |
| 658 |
/** |
| 659 |
* central file header signature |
| 660 |
* |
| 661 |
* @since 1.1 |
| 662 |
*/ |
| 663 |
protected static final byte[] CFH_SIG = ZipLong.getBytes(0X02014B50L); |
| 664 |
/** |
| 665 |
* end of central dir signature |
| 666 |
* |
| 667 |
* @since 1.1 |
| 668 |
*/ |
| 669 |
protected static final byte[] EOCD_SIG = ZipLong.getBytes(0X06054B50L); |
| 670 |
|
| 671 |
/** |
| 672 |
* Writes next block of compressed data to the output stream. |
| 673 |
* @throws IOException on error |
| 674 |
* |
| 675 |
* @since 1.14 |
| 676 |
*/ |
| 677 |
protected final void deflate() throws IOException { |
| 678 |
int len = def.deflate(buf, 0, buf.length); |
| 679 |
if (len > 0) { |
| 680 |
writeOut(buf, 0, len); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
/** |
| 685 |
* Writes the local file header entry |
| 686 |
* @param ze the entry to write |
| 687 |
* @throws IOException on error |
| 688 |
* |
| 689 |
* @since 1.1 |
| 690 |
*/ |
| 691 |
protected void writeLocalFileHeader(ZipEntry ze) throws IOException { |
| 692 |
|
| 693 |
boolean encodable = zipEncoding.canEncode(ze.getName()); |
| 694 |
|
| 695 |
final ZipEncoding entryEncoding; |
| 696 |
|
| 697 |
if (!encodable && fallbackToUTF8) { |
| 698 |
entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING; |
| 699 |
} else { |
| 700 |
entryEncoding = zipEncoding; |
| 701 |
} |
| 702 |
|
| 703 |
ByteBuffer name = entryEncoding.encode(ze.getName()); |
| 704 |
|
| 705 |
if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER) { |
| 706 |
|
| 707 |
if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS |
| 708 |
|| !encodable) { |
| 709 |
ze.addExtraField(new UnicodePathExtraField(ze.getName(), |
| 710 |
name.array(), |
| 711 |
name.arrayOffset(), |
| 712 |
name.limit())); |
| 713 |
} |
| 714 |
|
| 715 |
String comm = ze.getComment(); |
| 716 |
if (comm != null && !"".equals(comm)) { |
| 717 |
|
| 718 |
boolean commentEncodable = this.zipEncoding.canEncode(comm); |
| 719 |
|
| 720 |
if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS |
| 721 |
|| !commentEncodable) { |
| 722 |
ByteBuffer commentB = entryEncoding.encode(comm); |
| 723 |
ze.addExtraField(new UnicodeCommentExtraField(comm, |
| 724 |
commentB.array(), |
| 725 |
commentB.arrayOffset(), |
| 726 |
commentB.limit()) |
| 727 |
); |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
offsets.put(ze, ZipLong.getBytes(written)); |
| 733 |
|
| 734 |
writeOut(LFH_SIG); |
| 735 |
written += WORD; |
| 736 |
|
| 737 |
//store method in local variable to prevent multiple method calls |
| 738 |
final int zipMethod = ze.getMethod(); |
| 739 |
|
| 740 |
writeVersionNeededToExtractAndGeneralPurposeBits(zipMethod, |
| 741 |
!encodable |
| 742 |
&& fallbackToUTF8); |
| 743 |
written += WORD; |
| 744 |
|
| 745 |
// compression method |
| 746 |
writeOut(ZipShort.getBytes(zipMethod)); |
| 747 |
written += SHORT; |
| 748 |
|
| 749 |
// last mod. time and date |
| 750 |
writeOut(toDosTime(ze.getTime())); |
| 751 |
written += WORD; |
| 752 |
|
| 753 |
// CRC |
| 754 |
// compressed length |
| 755 |
// uncompressed length |
| 756 |
localDataStart = written; |
| 757 |
if (zipMethod == DEFLATED || raf != null) { |
| 758 |
writeOut(LZERO); |
| 759 |
writeOut(LZERO); |
| 760 |
writeOut(LZERO); |
| 761 |
} else { |
| 762 |
writeOut(ZipLong.getBytes(ze.getCrc())); |
| 763 |
writeOut(ZipLong.getBytes(ze.getSize())); |
| 764 |
writeOut(ZipLong.getBytes(ze.getSize())); |
| 765 |
} |
| 766 |
// CheckStyle:MagicNumber OFF |
| 767 |
written += 12; |
| 768 |
// CheckStyle:MagicNumber ON |
| 769 |
|
| 770 |
// file name length |
| 771 |
writeOut(ZipShort.getBytes(name.limit())); |
| 772 |
written += SHORT; |
| 773 |
|
| 774 |
// extra field length |
| 775 |
byte[] extra = ze.getLocalFileDataExtra(); |
| 776 |
writeOut(ZipShort.getBytes(extra.length)); |
| 777 |
written += SHORT; |
| 778 |
|
| 779 |
// file name |
| 780 |
writeOut(name.array(), name.arrayOffset(), name.limit()); |
| 781 |
written += name.limit(); |
| 782 |
|
| 783 |
// extra field |
| 784 |
writeOut(extra); |
| 785 |
written += extra.length; |
| 786 |
|
| 787 |
dataStart = written; |
| 788 |
} |
| 789 |
|
| 790 |
/** |
| 791 |
* Writes the data descriptor entry. |
| 792 |
* @param ze the entry to write |
| 793 |
* @throws IOException on error |
| 794 |
* |
| 795 |
* @since 1.1 |
| 796 |
*/ |
| 797 |
protected void writeDataDescriptor(ZipEntry ze) throws IOException { |
| 798 |
if (ze.getMethod() != DEFLATED || raf != null) { |
| 799 |
return; |
| 800 |
} |
| 801 |
writeOut(DD_SIG); |
| 802 |
writeOut(ZipLong.getBytes(entry.getCrc())); |
| 803 |
writeOut(ZipLong.getBytes(entry.getCompressedSize())); |
| 804 |
writeOut(ZipLong.getBytes(entry.getSize())); |
| 805 |
// CheckStyle:MagicNumber OFF |
| 806 |
written += 16; |
| 807 |
// CheckStyle:MagicNumber ON |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* Writes the central file header entry. |
| 812 |
* @param ze the entry to write |
| 813 |
* @throws IOException on error |
| 814 |
* |
| 815 |
* @since 1.1 |
| 816 |
*/ |
| 817 |
protected void writeCentralFileHeader(ZipEntry ze) throws IOException { |
| 818 |
writeOut(CFH_SIG); |
| 819 |
written += WORD; |
| 820 |
|
| 821 |
// version made by |
| 822 |
// CheckStyle:MagicNumber OFF |
| 823 |
writeOut(ZipShort.getBytes((ze.getPlatform() << 8) | 20)); |
| 824 |
written += SHORT; |
| 825 |
|
| 826 |
final int zipMethod = ze.getMethod(); |
| 827 |
final boolean encodable = zipEncoding.canEncode(ze.getName()); |
| 828 |
writeVersionNeededToExtractAndGeneralPurposeBits(zipMethod, |
| 829 |
!encodable |
| 830 |
&& fallbackToUTF8); |
| 831 |
written += WORD; |
| 832 |
|
| 833 |
// compression method |
| 834 |
writeOut(ZipShort.getBytes(zipMethod)); |
| 835 |
written += SHORT; |
| 836 |
|
| 837 |
// last mod. time and date |
| 838 |
writeOut(toDosTime(ze.getTime())); |
| 839 |
written += WORD; |
| 840 |
|
| 841 |
// CRC |
| 842 |
// compressed length |
| 843 |
// uncompressed length |
| 844 |
writeOut(ZipLong.getBytes(ze.getCrc())); |
| 845 |
writeOut(ZipLong.getBytes(ze.getCompressedSize())); |
| 846 |
writeOut(ZipLong.getBytes(ze.getSize())); |
| 847 |
// CheckStyle:MagicNumber OFF |
| 848 |
written += 12; |
| 849 |
// CheckStyle:MagicNumber ON |
| 850 |
|
| 851 |
// file name length |
| 852 |
final ZipEncoding entryEncoding; |
| 853 |
|
| 854 |
if (!encodable && fallbackToUTF8) { |
| 855 |
entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING; |
| 856 |
} else { |
| 857 |
entryEncoding = zipEncoding; |
| 858 |
} |
| 859 |
|
| 860 |
ByteBuffer name = entryEncoding.encode(ze.getName()); |
| 861 |
|
| 862 |
writeOut(ZipShort.getBytes(name.limit())); |
| 863 |
written += SHORT; |
| 864 |
|
| 865 |
// extra field length |
| 866 |
byte[] extra = ze.getCentralDirectoryExtra(); |
| 867 |
writeOut(ZipShort.getBytes(extra.length)); |
| 868 |
written += SHORT; |
| 869 |
|
| 870 |
// file comment length |
| 871 |
String comm = ze.getComment(); |
| 872 |
if (comm == null) { |
| 873 |
comm = ""; |
| 874 |
} |
| 875 |
|
| 876 |
ByteBuffer commentB = entryEncoding.encode(comm); |
| 877 |
|
| 878 |
writeOut(ZipShort.getBytes(commentB.limit())); |
| 879 |
written += SHORT; |
| 880 |
|
| 881 |
// disk number start |
| 882 |
writeOut(ZERO); |
| 883 |
written += SHORT; |
| 884 |
|
| 885 |
// internal file attributes |
| 886 |
writeOut(ZipShort.getBytes(ze.getInternalAttributes())); |
| 887 |
written += SHORT; |
| 888 |
|
| 889 |
// external file attributes |
| 890 |
writeOut(ZipLong.getBytes(ze.getExternalAttributes())); |
| 891 |
written += WORD; |
| 892 |
|
| 893 |
// relative offset of LFH |
| 894 |
writeOut((byte[]) offsets.get(ze)); |
| 895 |
written += WORD; |
| 896 |
|
| 897 |
// file name |
| 898 |
writeOut(name.array(), name.arrayOffset(), name.limit()); |
| 899 |
written += name.limit(); |
| 900 |
|
| 901 |
// extra field |
| 902 |
writeOut(extra); |
| 903 |
written += extra.length; |
| 904 |
|
| 905 |
// file comment |
| 906 |
writeOut(commentB.array(), commentB.arrayOffset(), commentB.limit()); |
| 907 |
written += commentB.limit(); |
| 908 |
} |
| 909 |
|
| 910 |
/** |
| 911 |
* Writes the "End of central dir record". |
| 912 |
* @throws IOException on error |
| 913 |
* |
| 914 |
* @since 1.1 |
| 915 |
*/ |
| 916 |
protected void writeCentralDirectoryEnd() throws IOException { |
| 917 |
writeOut(EOCD_SIG); |
| 918 |
|
| 919 |
// disk numbers |
| 920 |
writeOut(ZERO); |
| 921 |
writeOut(ZERO); |
| 922 |
|
| 923 |
// number of entries |
| 924 |
byte[] num = ZipShort.getBytes(entries.size()); |
| 925 |
writeOut(num); |
| 926 |
writeOut(num); |
| 927 |
|
| 928 |
// length and location of CD |
| 929 |
writeOut(ZipLong.getBytes(cdLength)); |
| 930 |
writeOut(ZipLong.getBytes(cdOffset)); |
| 931 |
|
| 932 |
// ZIP file comment |
| 933 |
ByteBuffer data = this.zipEncoding.encode(comment); |
| 934 |
writeOut(ZipShort.getBytes(data.limit())); |
| 935 |
writeOut(data.array(), data.arrayOffset(), data.limit()); |
| 936 |
} |
| 937 |
|
| 938 |
/** |
| 939 |
* Smallest date/time ZIP can handle. |
| 940 |
* |
| 941 |
* @since 1.1 |
| 942 |
*/ |
| 943 |
private static final byte[] DOS_TIME_MIN = ZipLong.getBytes(0x00002100L); |
| 944 |
|
| 945 |
/** |
| 946 |
* Convert a Date object to a DOS date/time field. |
| 947 |
* @param time the <code>Date</code> to convert |
| 948 |
* @return the date as a <code>ZipLong</code> |
| 949 |
* @since 1.1 |
| 950 |
*/ |
| 951 |
protected static ZipLong toDosTime(Date time) { |
| 952 |
return new ZipLong(toDosTime(time.getTime())); |
| 953 |
} |
| 954 |
|
| 955 |
/** |
| 956 |
* Convert a Date object to a DOS date/time field. |
| 957 |
* |
| 958 |
* <p>Stolen from InfoZip's <code>fileio.c</code></p> |
| 959 |
* @param t number of milliseconds since the epoch |
| 960 |
* @return the date as a byte array |
| 961 |
* @since 1.26 |
| 962 |
*/ |
| 963 |
@SuppressWarnings("deprecation") |
| 964 |
protected static byte[] toDosTime(long t) { |
| 965 |
Date time = new Date(t); |
| 966 |
// CheckStyle:MagicNumberCheck OFF - I do not think that using constants |
| 967 |
// here will improve the readablity |
| 968 |
int year = time.getYear() + 1900; |
| 969 |
if (year < 1980) { |
| 970 |
return DOS_TIME_MIN; |
| 971 |
} |
| 972 |
int month = time.getMonth() + 1; |
| 973 |
long value = ((year - 1980) << 25) |
| 974 |
| (month << 21) |
| 975 |
| (time.getDate() << 16) |
| 976 |
| (time.getHours() << 11) |
| 977 |
| (time.getMinutes() << 5) |
| 978 |
| (time.getSeconds() >> 1); |
| 979 |
return ZipLong.getBytes(value); |
| 980 |
// CheckStyle:MagicNumberCheck ON |
| 981 |
} |
| 982 |
|
| 983 |
/** |
| 984 |
* Retrieve the bytes for the given String in the encoding set for |
| 985 |
* this Stream. |
| 986 |
* @param name the string to get bytes from |
| 987 |
* @return the bytes as a byte array |
| 988 |
* @throws ZipException on error |
| 989 |
* |
| 990 |
* @since 1.3 |
| 991 |
*/ |
| 992 |
protected byte[] getBytes(String name) throws ZipException { |
| 993 |
try { |
| 994 |
ByteBuffer b = |
| 995 |
ZipEncodingHelper.getZipEncoding(encoding).encode(name); |
| 996 |
byte[] result = new byte[b.limit()]; |
| 997 |
System.arraycopy(b.array(), b.arrayOffset(), result, 0, |
| 998 |
result.length); |
| 999 |
return result; |
| 1000 |
} catch (IOException ex) { |
| 1001 |
throw new ZipException("Failed to encode name: " + ex.getMessage()); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
/** |
| 1006 |
* Write bytes to output or random access file. |
| 1007 |
* @param data the byte array to write |
| 1008 |
* @throws IOException on error |
| 1009 |
* |
| 1010 |
* @since 1.14 |
| 1011 |
*/ |
| 1012 |
protected final void writeOut(byte[] data) throws IOException { |
| 1013 |
writeOut(data, 0, data.length); |
| 1014 |
} |
| 1015 |
|
| 1016 |
/** |
| 1017 |
* Write bytes to output or random access file. |
| 1018 |
* @param data the byte array to write |
| 1019 |
* @param offset the start position to write from |
| 1020 |
* @param length the number of bytes to write |
| 1021 |
* @throws IOException on error |
| 1022 |
* |
| 1023 |
* @since 1.14 |
| 1024 |
*/ |
| 1025 |
protected final void writeOut(byte[] data, int offset, int length) |
| 1026 |
throws IOException { |
| 1027 |
if (raf != null) { |
| 1028 |
raf.write(data, offset, length); |
| 1029 |
} else { |
| 1030 |
out.write(data, offset, length); |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
/** |
| 1035 |
* Assumes a negative integer really is a positive integer that |
| 1036 |
* has wrapped around and re-creates the original value. |
| 1037 |
* @param i the value to treat as unsigned int. |
| 1038 |
* @return the unsigned int as a long. |
| 1039 |
* @since 1.34 |
| 1040 |
*/ |
| 1041 |
protected static long adjustToLong(int i) { |
| 1042 |
if (i < 0) { |
| 1043 |
return 2 * ((long) Integer.MAX_VALUE) + 2 + i; |
| 1044 |
} else { |
| 1045 |
return i; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
private void deflateUntilInputIsNeeded() throws IOException { |
| 1050 |
while (!def.needsInput()) { |
| 1051 |
deflate(); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
private void writeVersionNeededToExtractAndGeneralPurposeBits(final int |
| 1056 |
zipMethod, |
| 1057 |
final boolean |
| 1058 |
utfFallback) |
| 1059 |
throws IOException { |
| 1060 |
|
| 1061 |
// CheckStyle:MagicNumber OFF |
| 1062 |
int versionNeededToExtract = 10; |
| 1063 |
int generalPurposeFlag = (useUTF8Flag || utfFallback) ? UFT8_NAMES_FLAG : 0; |
| 1064 |
if (zipMethod == DEFLATED && raf == null) { |
| 1065 |
// requires version 2 as we are going to store length info |
| 1066 |
// in the data descriptor |
| 1067 |
versionNeededToExtract = 20; |
| 1068 |
// bit3 set to signal, we use a data descriptor |
| 1069 |
generalPurposeFlag |= 8; |
| 1070 |
} |
| 1071 |
// CheckStyle:MagicNumber ON |
| 1072 |
|
| 1073 |
// version needed to extract |
| 1074 |
writeOut(ZipShort.getBytes(versionNeededToExtract)); |
| 1075 |
// general purpose bit flag |
| 1076 |
writeOut(ZipShort.getBytes(generalPurposeFlag)); |
| 1077 |
} |
| 1078 |
|
| 1079 |
/** |
| 1080 |
* enum that represents the possible policies for creating Unicode |
| 1081 |
* extra fields. |
| 1082 |
*/ |
| 1083 |
public static final class UnicodeExtraFieldPolicy { |
| 1084 |
/** |
| 1085 |
* Always create Unicode extra fields. |
| 1086 |
*/ |
| 1087 |
public static final UnicodeExtraFieldPolicy ALWAYS = |
| 1088 |
new UnicodeExtraFieldPolicy("always"); |
| 1089 |
/** |
| 1090 |
* Never create Unicode extra fields. |
| 1091 |
*/ |
| 1092 |
public static final UnicodeExtraFieldPolicy NEVER = |
| 1093 |
new UnicodeExtraFieldPolicy("never"); |
| 1094 |
/** |
| 1095 |
* Create Unicode extra fields for filenames that cannot be |
| 1096 |
* encoded using the specified encoding. |
| 1097 |
*/ |
| 1098 |
public static final UnicodeExtraFieldPolicy NOT_ENCODEABLE = |
| 1099 |
new UnicodeExtraFieldPolicy("not encodeable"); |
| 1100 |
|
| 1101 |
private final String name; |
| 1102 |
private UnicodeExtraFieldPolicy(String n) { |
| 1103 |
name = n; |
| 1104 |
} |
| 1105 |
public String toString() { |
| 1106 |
return name; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |