001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.commons.compress.archivers.zip; 019 020import java.io.ByteArrayOutputStream; 021import java.io.File; 022import java.io.FileOutputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.OutputStream; 026import java.nio.ByteBuffer; 027import java.nio.channels.SeekableByteChannel; 028import java.nio.file.Files; 029import java.nio.file.StandardOpenOption; 030import java.util.Calendar; 031import java.util.EnumSet; 032import java.util.HashMap; 033import java.util.LinkedList; 034import java.util.List; 035import java.util.Map; 036import java.util.zip.Deflater; 037import java.util.zip.ZipException; 038 039import org.apache.commons.compress.archivers.ArchiveEntry; 040import org.apache.commons.compress.archivers.ArchiveOutputStream; 041import org.apache.commons.compress.utils.IOUtils; 042 043import static org.apache.commons.compress.archivers.zip.ZipConstants.DATA_DESCRIPTOR_MIN_VERSION; 044import static org.apache.commons.compress.archivers.zip.ZipConstants.DEFLATE_MIN_VERSION; 045import static org.apache.commons.compress.archivers.zip.ZipConstants.DWORD; 046import static org.apache.commons.compress.archivers.zip.ZipConstants.INITIAL_VERSION; 047import static org.apache.commons.compress.archivers.zip.ZipConstants.SHORT; 048import static org.apache.commons.compress.archivers.zip.ZipConstants.WORD; 049import static org.apache.commons.compress.archivers.zip.ZipConstants.ZIP64_MAGIC; 050import static org.apache.commons.compress.archivers.zip.ZipConstants.ZIP64_MAGIC_SHORT; 051import static org.apache.commons.compress.archivers.zip.ZipConstants.ZIP64_MIN_VERSION; 052import static org.apache.commons.compress.archivers.zip.ZipLong.putLong; 053import static org.apache.commons.compress.archivers.zip.ZipShort.putShort; 054 055/** 056 * Reimplementation of {@link java.util.zip.ZipOutputStream 057 * java.util.zip.ZipOutputStream} that does handle the extended 058 * functionality of this package, especially internal/external file 059 * attributes and extra fields with different layouts for local file 060 * data and central directory entries. 061 * 062 * <p>This class will try to use {@link 063 * java.nio.channels.SeekableByteChannel} when it knows that the 064 * output is going to go to a file.</p> 065 * 066 * <p>If SeekableByteChannel cannot be used, this implementation will use 067 * a Data Descriptor to store size and CRC information for {@link 068 * #DEFLATED DEFLATED} entries, this means, you don't need to 069 * calculate them yourself. Unfortunately this is not possible for 070 * the {@link #STORED STORED} method, here setting the CRC and 071 * uncompressed size information is required before {@link 072 * #putArchiveEntry(ArchiveEntry)} can be called.</p> 073 * 074 * <p>As of Apache Commons Compress 1.3 it transparently supports Zip64 075 * extensions and thus individual entries and archives larger than 4 076 * GB or with more than 65536 entries in most cases but explicit 077 * control is provided via {@link #setUseZip64}. If the stream can not 078 * use SeekableByteChannel and you try to write a ZipArchiveEntry of 079 * unknown size then Zip64 extensions will be disabled by default.</p> 080 * 081 * @NotThreadSafe 082 */ 083public class ZipArchiveOutputStream extends ArchiveOutputStream { 084 085 static final int BUFFER_SIZE = 512; 086 private static final int LFH_SIG_OFFSET = 0; 087 private static final int LFH_VERSION_NEEDED_OFFSET = 4; 088 private static final int LFH_GPB_OFFSET = 6; 089 private static final int LFH_METHOD_OFFSET = 8; 090 private static final int LFH_TIME_OFFSET = 10; 091 private static final int LFH_CRC_OFFSET = 14; 092 private static final int LFH_COMPRESSED_SIZE_OFFSET = 18; 093 private static final int LFH_ORIGINAL_SIZE_OFFSET = 22; 094 private static final int LFH_FILENAME_LENGTH_OFFSET = 26; 095 private static final int LFH_EXTRA_LENGTH_OFFSET = 28; 096 private static final int LFH_FILENAME_OFFSET = 30; 097 private static final int CFH_SIG_OFFSET = 0; 098 private static final int CFH_VERSION_MADE_BY_OFFSET = 4; 099 private static final int CFH_VERSION_NEEDED_OFFSET = 6; 100 private static final int CFH_GPB_OFFSET = 8; 101 private static final int CFH_METHOD_OFFSET = 10; 102 private static final int CFH_TIME_OFFSET = 12; 103 private static final int CFH_CRC_OFFSET = 16; 104 private static final int CFH_COMPRESSED_SIZE_OFFSET = 20; 105 private static final int CFH_ORIGINAL_SIZE_OFFSET = 24; 106 private static final int CFH_FILENAME_LENGTH_OFFSET = 28; 107 private static final int CFH_EXTRA_LENGTH_OFFSET = 30; 108 private static final int CFH_COMMENT_LENGTH_OFFSET = 32; 109 private static final int CFH_DISK_NUMBER_OFFSET = 34; 110 private static final int CFH_INTERNAL_ATTRIBUTES_OFFSET = 36; 111 private static final int CFH_EXTERNAL_ATTRIBUTES_OFFSET = 38; 112 private static final int CFH_LFH_OFFSET = 42; 113 private static final int CFH_FILENAME_OFFSET = 46; 114 115 /** indicates if this archive is finished. protected for use in Jar implementation */ 116 protected boolean finished = false; 117 118 /** 119 * Compression method for deflated entries. 120 */ 121 public static final int DEFLATED = java.util.zip.ZipEntry.DEFLATED; 122 123 /** 124 * Default compression level for deflated entries. 125 */ 126 public static final int DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION; 127 128 /** 129 * Compression method for stored entries. 130 */ 131 public static final int STORED = java.util.zip.ZipEntry.STORED; 132 133 /** 134 * default encoding for file names and comment. 135 */ 136 static final String DEFAULT_ENCODING = ZipEncodingHelper.UTF8; 137 138 /** 139 * General purpose flag, which indicates that filenames are 140 * written in UTF-8. 141 * @deprecated use {@link GeneralPurposeBit#UFT8_NAMES_FLAG} instead 142 */ 143 @Deprecated 144 public static final int EFS_FLAG = GeneralPurposeBit.UFT8_NAMES_FLAG; 145 146 private static final byte[] EMPTY = new byte[0]; 147 148 /** 149 * Current entry. 150 */ 151 private CurrentEntry entry; 152 153 /** 154 * The file comment. 155 */ 156 private String comment = ""; 157 158 /** 159 * Compression level for next entry. 160 */ 161 private int level = DEFAULT_COMPRESSION; 162 163 /** 164 * Has the compression level changed when compared to the last 165 * entry? 166 */ 167 private boolean hasCompressionLevelChanged = false; 168 169 /** 170 * Default compression method for next entry. 171 */ 172 private int method = java.util.zip.ZipEntry.DEFLATED; 173 174 /** 175 * List of ZipArchiveEntries written so far. 176 */ 177 private final List<ZipArchiveEntry> entries = 178 new LinkedList<>(); 179 180 private final StreamCompressor streamCompressor; 181 182 /** 183 * Start of central directory. 184 */ 185 private long cdOffset = 0; 186 187 /** 188 * Length of central directory. 189 */ 190 private long cdLength = 0; 191 192 /** 193 * Helper, a 0 as ZipShort. 194 */ 195 private static final byte[] ZERO = {0, 0}; 196 197 /** 198 * Helper, a 0 as ZipLong. 199 */ 200 private static final byte[] LZERO = {0, 0, 0, 0}; 201 202 private static final byte[] ONE = ZipLong.getBytes(1L); 203 204 /** 205 * Holds some book-keeping data for each entry. 206 */ 207 private final Map<ZipArchiveEntry, EntryMetaData> metaData = 208 new HashMap<>(); 209 210 /** 211 * The encoding to use for filenames and the file comment. 212 * 213 * <p>For a list of possible values see <a 214 * 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>. 215 * Defaults to UTF-8.</p> 216 */ 217 private String encoding = DEFAULT_ENCODING; 218 219 /** 220 * The zip encoding to use for filenames and the file comment. 221 * 222 * This field is of internal use and will be set in {@link 223 * #setEncoding(String)}. 224 */ 225 private ZipEncoding zipEncoding = 226 ZipEncodingHelper.getZipEncoding(DEFAULT_ENCODING); 227 228 229 /** 230 * This Deflater object is used for output. 231 * 232 */ 233 protected final Deflater def; 234 /** 235 * Optional random access output. 236 */ 237 private final SeekableByteChannel channel; 238 239 private final OutputStream out; 240 241 /** 242 * whether to use the general purpose bit flag when writing UTF-8 243 * filenames or not. 244 */ 245 private boolean useUTF8Flag = true; 246 247 /** 248 * Whether to encode non-encodable file names as UTF-8. 249 */ 250 private boolean fallbackToUTF8 = false; 251 252 /** 253 * whether to create UnicodePathExtraField-s for each entry. 254 */ 255 private UnicodeExtraFieldPolicy createUnicodeExtraFields = UnicodeExtraFieldPolicy.NEVER; 256 257 /** 258 * Whether anything inside this archive has used a ZIP64 feature. 259 * 260 * @since 1.3 261 */ 262 private boolean hasUsedZip64 = false; 263 264 private Zip64Mode zip64Mode = Zip64Mode.AsNeeded; 265 266 private final byte[] copyBuffer = new byte[32768]; 267 private final Calendar calendarInstance = Calendar.getInstance(); 268 269 /** 270 * Creates a new ZIP OutputStream filtering the underlying stream. 271 * @param out the outputstream to zip 272 */ 273 public ZipArchiveOutputStream(final OutputStream out) { 274 this.out = out; 275 this.channel = null; 276 def = new Deflater(level, true); 277 streamCompressor = StreamCompressor.create(out, def); 278 } 279 280 /** 281 * Creates a new ZIP OutputStream writing to a File. Will use 282 * random access if possible. 283 * @param file the file to zip to 284 * @throws IOException on error 285 */ 286 public ZipArchiveOutputStream(final File file) throws IOException { 287 def = new Deflater(level, true); 288 OutputStream o = null; 289 SeekableByteChannel _channel = null; 290 StreamCompressor _streamCompressor = null; 291 try { 292 _channel = Files.newByteChannel(file.toPath(), 293 EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE, 294 StandardOpenOption.READ, 295 StandardOpenOption.TRUNCATE_EXISTING)); 296 // will never get opened properly when an exception is thrown so doesn't need to get closed 297 _streamCompressor = StreamCompressor.create(_channel, def); //NOSONAR 298 } catch (final IOException e) { 299 IOUtils.closeQuietly(_channel); 300 _channel = null; 301 o = new FileOutputStream(file); 302 _streamCompressor = StreamCompressor.create(o, def); 303 } 304 out = o; 305 channel = _channel; 306 streamCompressor = _streamCompressor; 307 } 308 309 /** 310 * Creates a new ZIP OutputStream writing to a SeekableByteChannel. 311 * 312 * <p>{@link 313 * org.apache.commons.compress.utils.SeekableInMemoryByteChannel} 314 * allows you to write to an in-memory archive using random 315 * access.</p> 316 * 317 * @param channel the channel to zip to 318 * @throws IOException on error 319 * @since 1.13 320 */ 321 public ZipArchiveOutputStream(SeekableByteChannel channel) throws IOException { 322 this.channel = channel; 323 def = new Deflater(level, true); 324 streamCompressor = StreamCompressor.create(channel, def); 325 out = null; 326 } 327 328 /** 329 * This method indicates whether this archive is writing to a 330 * seekable stream (i.e., to a random access file). 331 * 332 * <p>For seekable streams, you don't need to calculate the CRC or 333 * uncompressed size for {@link #STORED} entries before 334 * invoking {@link #putArchiveEntry(ArchiveEntry)}. 335 * @return true if seekable 336 */ 337 public boolean isSeekable() { 338 return channel != 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 UTF-8.</p> 347 * @param encoding the encoding to use for file names, use null 348 * for the platform's default encoding 349 */ 350 public void setEncoding(final String encoding) { 351 this.encoding = encoding; 352 this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); 353 if (useUTF8Flag && !ZipEncodingHelper.isUTF8(encoding)) { 354 useUTF8Flag = false; 355 } 356 } 357 358 /** 359 * The encoding to use for filenames and the file comment. 360 * 361 * @return null if using the platform's default character encoding. 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 * @param b whether to set the language encoding flag if the file 374 * name encoding is UTF-8 375 */ 376 public void setUseLanguageEncodingFlag(final boolean b) { 377 useUTF8Flag = b && ZipEncodingHelper.isUTF8(encoding); 378 } 379 380 /** 381 * Whether to create Unicode Extra Fields. 382 * 383 * <p>Defaults to NEVER.</p> 384 * 385 * @param b whether to create Unicode Extra Fields. 386 */ 387 public void setCreateUnicodeExtraFields(final UnicodeExtraFieldPolicy b) { 388 createUnicodeExtraFields = b; 389 } 390 391 /** 392 * Whether to fall back to UTF and the language encoding flag if 393 * the file name cannot be encoded using the specified encoding. 394 * 395 * <p>Defaults to false.</p> 396 * 397 * @param b whether to fall back to UTF and the language encoding 398 * flag if the file name cannot be encoded using the specified 399 * encoding. 400 */ 401 public void setFallbackToUTF8(final boolean b) { 402 fallbackToUTF8 = b; 403 } 404 405 /** 406 * Whether Zip64 extensions will be used. 407 * 408 * <p>When setting the mode to {@link Zip64Mode#Never Never}, 409 * {@link #putArchiveEntry}, {@link #closeArchiveEntry}, {@link 410 * #finish} or {@link #close} may throw a {@link 411 * Zip64RequiredException} if the entry's size or the total size 412 * of the archive exceeds 4GB or there are more than 65536 entries 413 * inside the archive. Any archive created in this mode will be 414 * readable by implementations that don't support Zip64.</p> 415 * 416 * <p>When setting the mode to {@link Zip64Mode#Always Always}, 417 * Zip64 extensions will be used for all entries. Any archive 418 * created in this mode may be unreadable by implementations that 419 * don't support Zip64 even if all its contents would be.</p> 420 * 421 * <p>When setting the mode to {@link Zip64Mode#AsNeeded 422 * AsNeeded}, Zip64 extensions will transparently be used for 423 * those entries that require them. This mode can only be used if 424 * the uncompressed size of the {@link ZipArchiveEntry} is known 425 * when calling {@link #putArchiveEntry} or the archive is written 426 * to a seekable output (i.e. you have used the {@link 427 * #ZipArchiveOutputStream(java.io.File) File-arg constructor}) - 428 * this mode is not valid when the output stream is not seekable 429 * and the uncompressed size is unknown when {@link 430 * #putArchiveEntry} is called.</p> 431 * 432 * <p>If no entry inside the resulting archive requires Zip64 433 * extensions then {@link Zip64Mode#Never Never} will create the 434 * smallest archive. {@link Zip64Mode#AsNeeded AsNeeded} will 435 * create a slightly bigger archive if the uncompressed size of 436 * any entry has initially been unknown and create an archive 437 * identical to {@link Zip64Mode#Never Never} otherwise. {@link 438 * Zip64Mode#Always Always} will create an archive that is at 439 * least 24 bytes per entry bigger than the one {@link 440 * Zip64Mode#Never Never} would create.</p> 441 * 442 * <p>Defaults to {@link Zip64Mode#AsNeeded AsNeeded} unless 443 * {@link #putArchiveEntry} is called with an entry of unknown 444 * size and data is written to a non-seekable stream - in this 445 * case the default is {@link Zip64Mode#Never Never}.</p> 446 * 447 * @since 1.3 448 * @param mode Whether Zip64 extensions will be used. 449 */ 450 public void setUseZip64(final Zip64Mode mode) { 451 zip64Mode = mode; 452 } 453 454 /** 455 * {@inheritDoc} 456 * @throws Zip64RequiredException if the archive's size exceeds 4 457 * GByte or there are more than 65535 entries inside the archive 458 * and {@link #setUseZip64} is {@link Zip64Mode#Never}. 459 */ 460 @Override 461 public void finish() throws IOException { 462 if (finished) { 463 throw new IOException("This archive has already been finished"); 464 } 465 466 if (entry != null) { 467 throw new IOException("This archive contains unclosed entries."); 468 } 469 470 cdOffset = streamCompressor.getTotalBytesWritten(); 471 writeCentralDirectoryInChunks(); 472 473 cdLength = streamCompressor.getTotalBytesWritten() - cdOffset; 474 writeZip64CentralDirectory(); 475 writeCentralDirectoryEnd(); 476 metaData.clear(); 477 entries.clear(); 478 streamCompressor.close(); 479 finished = true; 480 } 481 482 private void writeCentralDirectoryInChunks() throws IOException { 483 final int NUM_PER_WRITE = 1000; 484 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(70 * NUM_PER_WRITE); 485 int count = 0; 486 for (final ZipArchiveEntry ze : entries) { 487 byteArrayOutputStream.write(createCentralFileHeader(ze)); 488 if (++count > NUM_PER_WRITE){ 489 writeCounted(byteArrayOutputStream.toByteArray()); 490 byteArrayOutputStream.reset(); 491 count = 0; 492 } 493 } 494 writeCounted(byteArrayOutputStream.toByteArray()); 495 } 496 497 /** 498 * Writes all necessary data for this entry. 499 * @throws IOException on error 500 * @throws Zip64RequiredException if the entry's uncompressed or 501 * compressed size exceeds 4 GByte and {@link #setUseZip64} 502 * is {@link Zip64Mode#Never}. 503 */ 504 @Override 505 public void closeArchiveEntry() throws IOException { 506 preClose(); 507 508 flushDeflater(); 509 510 final long bytesWritten = streamCompressor.getTotalBytesWritten() - entry.dataStart; 511 final long realCrc = streamCompressor.getCrc32(); 512 entry.bytesRead = streamCompressor.getBytesRead(); 513 final Zip64Mode effectiveMode = getEffectiveZip64Mode(entry.entry); 514 final boolean actuallyNeedsZip64 = handleSizesAndCrc(bytesWritten, realCrc, effectiveMode); 515 closeEntry(actuallyNeedsZip64, false); 516 streamCompressor.reset(); 517 } 518 519 /** 520 * Writes all necessary data for this entry. 521 * 522 * @param phased This entry is second phase of a 2-phase zip creation, size, compressed size and crc 523 * are known in ZipArchiveEntry 524 * @throws IOException on error 525 * @throws Zip64RequiredException if the entry's uncompressed or 526 * compressed size exceeds 4 GByte and {@link #setUseZip64} 527 * is {@link Zip64Mode#Never}. 528 */ 529 private void closeCopiedEntry(final boolean phased) throws IOException { 530 preClose(); 531 entry.bytesRead = entry.entry.getSize(); 532 final Zip64Mode effectiveMode = getEffectiveZip64Mode(entry.entry); 533 final boolean actuallyNeedsZip64 = checkIfNeedsZip64(effectiveMode); 534 closeEntry(actuallyNeedsZip64, phased); 535 } 536 537 private void closeEntry(final boolean actuallyNeedsZip64, final boolean phased) throws IOException { 538 if (!phased && channel != null) { 539 rewriteSizesAndCrc(actuallyNeedsZip64); 540 } 541 542 if (!phased) { 543 writeDataDescriptor(entry.entry); 544 } 545 entry = null; 546 } 547 548 private void preClose() throws IOException { 549 if (finished) { 550 throw new IOException("Stream has already been finished"); 551 } 552 553 if (entry == null) { 554 throw new IOException("No current entry to close"); 555 } 556 557 if (!entry.hasWritten) { 558 write(EMPTY, 0, 0); 559 } 560 } 561 562 /** 563 * Adds an archive entry with a raw input stream. 564 * 565 * If crc, size and compressed size are supplied on the entry, these values will be used as-is. 566 * Zip64 status is re-established based on the settings in this stream, and the supplied value 567 * is ignored. 568 * 569 * The entry is put and closed immediately. 570 * 571 * @param entry The archive entry to add 572 * @param rawStream The raw input stream of a different entry. May be compressed/encrypted. 573 * @throws IOException If copying fails 574 */ 575 public void addRawArchiveEntry(final ZipArchiveEntry entry, final InputStream rawStream) 576 throws IOException { 577 final ZipArchiveEntry ae = new ZipArchiveEntry(entry); 578 if (hasZip64Extra(ae)) { 579 // Will be re-added as required. this may make the file generated with this method 580 // somewhat smaller than standard mode, 581 // since standard mode is unable to remove the zip 64 header. 582 ae.removeExtraField(Zip64ExtendedInformationExtraField.HEADER_ID); 583 } 584 final boolean is2PhaseSource = ae.getCrc() != ZipArchiveEntry.CRC_UNKNOWN 585 && ae.getSize() != ArchiveEntry.SIZE_UNKNOWN 586 && ae.getCompressedSize() != ArchiveEntry.SIZE_UNKNOWN; 587 putArchiveEntry(ae, is2PhaseSource); 588 copyFromZipInputStream(rawStream); 589 closeCopiedEntry(is2PhaseSource); 590 } 591 592 /** 593 * Ensures all bytes sent to the deflater are written to the stream. 594 */ 595 private void flushDeflater() throws IOException { 596 if (entry.entry.getMethod() == DEFLATED) { 597 streamCompressor.flushDeflater(); 598 } 599 } 600 601 /** 602 * Ensures the current entry's size and CRC information is set to 603 * the values just written, verifies it isn't too big in the 604 * Zip64Mode.Never case and returns whether the entry would 605 * require a Zip64 extra field. 606 */ 607 private boolean handleSizesAndCrc(final long bytesWritten, final long crc, 608 final Zip64Mode effectiveMode) 609 throws ZipException { 610 if (entry.entry.getMethod() == DEFLATED) { 611 /* It turns out def.getBytesRead() returns wrong values if 612 * the size exceeds 4 GB on Java < Java7 613 entry.entry.setSize(def.getBytesRead()); 614 */ 615 entry.entry.setSize(entry.bytesRead); 616 entry.entry.setCompressedSize(bytesWritten); 617 entry.entry.setCrc(crc); 618 619 } else if (channel == null) { 620 if (entry.entry.getCrc() != crc) { 621 throw new ZipException("bad CRC checksum for entry " 622 + entry.entry.getName() + ": " 623 + Long.toHexString(entry.entry.getCrc()) 624 + " instead of " 625 + Long.toHexString(crc)); 626 } 627 628 if (entry.entry.getSize() != bytesWritten) { 629 throw new ZipException("bad size for entry " 630 + entry.entry.getName() + ": " 631 + entry.entry.getSize() 632 + " instead of " 633 + bytesWritten); 634 } 635 } else { /* method is STORED and we used SeekableByteChannel */ 636 entry.entry.setSize(bytesWritten); 637 entry.entry.setCompressedSize(bytesWritten); 638 entry.entry.setCrc(crc); 639 } 640 641 return checkIfNeedsZip64(effectiveMode); 642 } 643 644 /** 645 * Verifies the sizes aren't too big in the Zip64Mode.Never case 646 * and returns whether the entry would require a Zip64 extra 647 * field. 648 */ 649 private boolean checkIfNeedsZip64(final Zip64Mode effectiveMode) 650 throws ZipException { 651 final boolean actuallyNeedsZip64 = isZip64Required(entry.entry, effectiveMode); 652 if (actuallyNeedsZip64 && effectiveMode == Zip64Mode.Never) { 653 throw new Zip64RequiredException(Zip64RequiredException.getEntryTooBigMessage(entry.entry)); 654 } 655 return actuallyNeedsZip64; 656 } 657 658 private boolean isZip64Required(final ZipArchiveEntry entry1, final Zip64Mode requestedMode) { 659 return requestedMode == Zip64Mode.Always || isTooLageForZip32(entry1); 660 } 661 662 private boolean isTooLageForZip32(final ZipArchiveEntry zipArchiveEntry){ 663 return zipArchiveEntry.getSize() >= ZIP64_MAGIC || zipArchiveEntry.getCompressedSize() >= ZIP64_MAGIC; 664 } 665 666 /** 667 * When using random access output, write the local file header 668 * and potentiall the ZIP64 extra containing the correct CRC and 669 * compressed/uncompressed sizes. 670 */ 671 private void rewriteSizesAndCrc(final boolean actuallyNeedsZip64) 672 throws IOException { 673 final long save = channel.position(); 674 675 channel.position(entry.localDataStart); 676 writeOut(ZipLong.getBytes(entry.entry.getCrc())); 677 if (!hasZip64Extra(entry.entry) || !actuallyNeedsZip64) { 678 writeOut(ZipLong.getBytes(entry.entry.getCompressedSize())); 679 writeOut(ZipLong.getBytes(entry.entry.getSize())); 680 } else { 681 writeOut(ZipLong.ZIP64_MAGIC.getBytes()); 682 writeOut(ZipLong.ZIP64_MAGIC.getBytes()); 683 } 684 685 if (hasZip64Extra(entry.entry)) { 686 final ByteBuffer name = getName(entry.entry); 687 final int nameLen = name.limit() - name.position(); 688 // seek to ZIP64 extra, skip header and size information 689 channel.position(entry.localDataStart + 3 * WORD + 2 * SHORT 690 + nameLen + 2 * SHORT); 691 // inside the ZIP64 extra uncompressed size comes 692 // first, unlike the LFH, CD or data descriptor 693 writeOut(ZipEightByteInteger.getBytes(entry.entry.getSize())); 694 writeOut(ZipEightByteInteger.getBytes(entry.entry.getCompressedSize())); 695 696 if (!actuallyNeedsZip64) { 697 // do some cleanup: 698 // * rewrite version needed to extract 699 channel.position(entry.localDataStart - 5 * SHORT); 700 writeOut(ZipShort.getBytes(versionNeededToExtract(entry.entry.getMethod(), false, false))); 701 702 // * remove ZIP64 extra so it doesn't get written 703 // to the central directory 704 entry.entry.removeExtraField(Zip64ExtendedInformationExtraField 705 .HEADER_ID); 706 entry.entry.setExtra(); 707 708 // * reset hasUsedZip64 if it has been set because 709 // of this entry 710 if (entry.causedUseOfZip64) { 711 hasUsedZip64 = false; 712 } 713 } 714 } 715 channel.position(save); 716 } 717 718 /** 719 * {@inheritDoc} 720 * @throws ClassCastException if entry is not an instance of ZipArchiveEntry 721 * @throws Zip64RequiredException if the entry's uncompressed or 722 * compressed size is known to exceed 4 GByte and {@link #setUseZip64} 723 * is {@link Zip64Mode#Never}. 724 */ 725 @Override 726 public void putArchiveEntry(final ArchiveEntry archiveEntry) throws IOException { 727 putArchiveEntry(archiveEntry, false); 728 } 729 730 /** 731 * Writes the headers for an archive entry to the output stream. 732 * The caller must then write the content to the stream and call 733 * {@link #closeArchiveEntry()} to complete the process. 734 735 * @param archiveEntry The archiveEntry 736 * @param phased If true size, compressedSize and crc required to be known up-front in the archiveEntry 737 * @throws ClassCastException if entry is not an instance of ZipArchiveEntry 738 * @throws Zip64RequiredException if the entry's uncompressed or 739 * compressed size is known to exceed 4 GByte and {@link #setUseZip64} 740 * is {@link Zip64Mode#Never}. 741 */ 742 private void putArchiveEntry(final ArchiveEntry archiveEntry, final boolean phased) throws IOException { 743 if (finished) { 744 throw new IOException("Stream has already been finished"); 745 } 746 747 if (entry != null) { 748 closeArchiveEntry(); 749 } 750 751 entry = new CurrentEntry((ZipArchiveEntry) archiveEntry); 752 entries.add(entry.entry); 753 754 setDefaults(entry.entry); 755 756 final Zip64Mode effectiveMode = getEffectiveZip64Mode(entry.entry); 757 validateSizeInformation(effectiveMode); 758 759 if (shouldAddZip64Extra(entry.entry, effectiveMode)) { 760 761 final Zip64ExtendedInformationExtraField z64 = getZip64Extra(entry.entry); 762 763 ZipEightByteInteger size; 764 ZipEightByteInteger compressedSize; 765 if (phased) { 766 // sizes are already known 767 size = new ZipEightByteInteger(entry.entry.getSize()); 768 compressedSize = new ZipEightByteInteger(entry.entry.getCompressedSize()); 769 } else if (entry.entry.getMethod() == STORED 770 && entry.entry.getSize() != ArchiveEntry.SIZE_UNKNOWN) { 771 // actually, we already know the sizes 772 compressedSize = size = new ZipEightByteInteger(entry.entry.getSize()); 773 } else { 774 // just a placeholder, real data will be in data 775 // descriptor or inserted later via SeekableByteChannel 776 compressedSize = size = ZipEightByteInteger.ZERO; 777 } 778 z64.setSize(size); 779 z64.setCompressedSize(compressedSize); 780 entry.entry.setExtra(); 781 } 782 783 if (entry.entry.getMethod() == DEFLATED && hasCompressionLevelChanged) { 784 def.setLevel(level); 785 hasCompressionLevelChanged = false; 786 } 787 writeLocalFileHeader((ZipArchiveEntry) archiveEntry, phased); 788 } 789 790 /** 791 * Provides default values for compression method and last 792 * modification time. 793 */ 794 private void setDefaults(final ZipArchiveEntry entry) { 795 if (entry.getMethod() == -1) { // not specified 796 entry.setMethod(method); 797 } 798 799 if (entry.getTime() == -1) { // not specified 800 entry.setTime(System.currentTimeMillis()); 801 } 802 } 803 804 /** 805 * Throws an exception if the size is unknown for a stored entry 806 * that is written to a non-seekable output or the entry is too 807 * big to be written without Zip64 extra but the mode has been set 808 * to Never. 809 */ 810 private void validateSizeInformation(final Zip64Mode effectiveMode) 811 throws ZipException { 812 // Size/CRC not required if SeekableByteChannel is used 813 if (entry.entry.getMethod() == STORED && channel == null) { 814 if (entry.entry.getSize() == ArchiveEntry.SIZE_UNKNOWN) { 815 throw new ZipException("uncompressed size is required for" 816 + " STORED method when not writing to a" 817 + " file"); 818 } 819 if (entry.entry.getCrc() == ZipArchiveEntry.CRC_UNKNOWN) { 820 throw new ZipException("crc checksum is required for STORED" 821 + " method when not writing to a file"); 822 } 823 entry.entry.setCompressedSize(entry.entry.getSize()); 824 } 825 826 if ((entry.entry.getSize() >= ZIP64_MAGIC 827 || entry.entry.getCompressedSize() >= ZIP64_MAGIC) 828 && effectiveMode == Zip64Mode.Never) { 829 throw new Zip64RequiredException(Zip64RequiredException 830 .getEntryTooBigMessage(entry.entry)); 831 } 832 } 833 834 /** 835 * Whether to addd a Zip64 extended information extra field to the 836 * local file header. 837 * 838 * <p>Returns true if</p> 839 * 840 * <ul> 841 * <li>mode is Always</li> 842 * <li>or we already know it is going to be needed</li> 843 * <li>or the size is unknown and we can ensure it won't hurt 844 * other implementations if we add it (i.e. we can erase its 845 * usage</li> 846 * </ul> 847 */ 848 private boolean shouldAddZip64Extra(final ZipArchiveEntry entry, final Zip64Mode mode) { 849 return mode == Zip64Mode.Always 850 || entry.getSize() >= ZIP64_MAGIC 851 || entry.getCompressedSize() >= ZIP64_MAGIC 852 || (entry.getSize() == ArchiveEntry.SIZE_UNKNOWN 853 && channel != null && mode != Zip64Mode.Never); 854 } 855 856 /** 857 * Set the file comment. 858 * @param comment the comment 859 */ 860 public void setComment(final String comment) { 861 this.comment = comment; 862 } 863 864 /** 865 * Sets the compression level for subsequent entries. 866 * 867 * <p>Default is Deflater.DEFAULT_COMPRESSION.</p> 868 * @param level the compression level. 869 * @throws IllegalArgumentException if an invalid compression 870 * level is specified. 871 */ 872 public void setLevel(final int level) { 873 if (level < Deflater.DEFAULT_COMPRESSION 874 || level > Deflater.BEST_COMPRESSION) { 875 throw new IllegalArgumentException("Invalid compression level: " 876 + level); 877 } 878 hasCompressionLevelChanged = (this.level != level); 879 this.level = level; 880 } 881 882 /** 883 * Sets the default compression method for subsequent entries. 884 * 885 * <p>Default is DEFLATED.</p> 886 * @param method an <code>int</code> from java.util.zip.ZipEntry 887 */ 888 public void setMethod(final int method) { 889 this.method = method; 890 } 891 892 /** 893 * Whether this stream is able to write the given entry. 894 * 895 * <p>May return false if it is set up to use encryption or a 896 * compression method that hasn't been implemented yet.</p> 897 * @since 1.1 898 */ 899 @Override 900 public boolean canWriteEntryData(final ArchiveEntry ae) { 901 if (ae instanceof ZipArchiveEntry) { 902 final ZipArchiveEntry zae = (ZipArchiveEntry) ae; 903 return zae.getMethod() != ZipMethod.IMPLODING.getCode() 904 && zae.getMethod() != ZipMethod.UNSHRINKING.getCode() 905 && ZipUtil.canHandleEntryData(zae); 906 } 907 return false; 908 } 909 910 /** 911 * Writes bytes to ZIP entry. 912 * @param b the byte array to write 913 * @param offset the start position to write from 914 * @param length the number of bytes to write 915 * @throws IOException on error 916 */ 917 @Override 918 public void write(final byte[] b, final int offset, final int length) throws IOException { 919 if (entry == null) { 920 throw new IllegalStateException("No current entry"); 921 } 922 ZipUtil.checkRequestedFeatures(entry.entry); 923 final long writtenThisTime = streamCompressor.write(b, offset, length, entry.entry.getMethod()); 924 count(writtenThisTime); 925 } 926 927 /** 928 * Write bytes to output or random access file. 929 * @param data the byte array to write 930 * @throws IOException on error 931 */ 932 private void writeCounted(final byte[] data) throws IOException { 933 streamCompressor.writeCounted(data); 934 } 935 936 private void copyFromZipInputStream(final InputStream src) throws IOException { 937 if (entry == null) { 938 throw new IllegalStateException("No current entry"); 939 } 940 ZipUtil.checkRequestedFeatures(entry.entry); 941 entry.hasWritten = true; 942 int length; 943 while ((length = src.read(copyBuffer)) >= 0 ) 944 { 945 streamCompressor.writeCounted(copyBuffer, 0, length); 946 count( length ); 947 } 948 } 949 950 /** 951 * Closes this output stream and releases any system resources 952 * associated with the stream. 953 * 954 * @throws IOException if an I/O error occurs. 955 * @throws Zip64RequiredException if the archive's size exceeds 4 956 * GByte or there are more than 65535 entries inside the archive 957 * and {@link #setUseZip64} is {@link Zip64Mode#Never}. 958 */ 959 @Override 960 public void close() throws IOException { 961 try { 962 if (!finished) { 963 finish(); 964 } 965 } finally { 966 destroy(); 967 } 968 } 969 970 /** 971 * Flushes this output stream and forces any buffered output bytes 972 * to be written out to the stream. 973 * 974 * @throws IOException if an I/O error occurs. 975 */ 976 @Override 977 public void flush() throws IOException { 978 if (out != null) { 979 out.flush(); 980 } 981 } 982 983 /* 984 * Various ZIP constants shared between this class, ZipArchiveInputStream and ZipFile 985 */ 986 /** 987 * local file header signature 988 */ 989 static final byte[] LFH_SIG = ZipLong.LFH_SIG.getBytes(); //NOSONAR 990 /** 991 * data descriptor signature 992 */ 993 static final byte[] DD_SIG = ZipLong.DD_SIG.getBytes(); //NOSONAR 994 /** 995 * central file header signature 996 */ 997 static final byte[] CFH_SIG = ZipLong.CFH_SIG.getBytes(); //NOSONAR 998 /** 999 * end of central dir signature 1000 */ 1001 static final byte[] EOCD_SIG = ZipLong.getBytes(0X06054B50L); //NOSONAR 1002 /** 1003 * ZIP64 end of central dir signature 1004 */ 1005 static final byte[] ZIP64_EOCD_SIG = ZipLong.getBytes(0X06064B50L); //NOSONAR 1006 /** 1007 * ZIP64 end of central dir locator signature 1008 */ 1009 static final byte[] ZIP64_EOCD_LOC_SIG = ZipLong.getBytes(0X07064B50L); //NOSONAR 1010 1011 /** 1012 * Writes next block of compressed data to the output stream. 1013 * @throws IOException on error 1014 */ 1015 protected final void deflate() throws IOException { 1016 streamCompressor.deflate(); 1017 } 1018 1019 /** 1020 * Writes the local file header entry 1021 * @param ze the entry to write 1022 * @throws IOException on error 1023 */ 1024 protected void writeLocalFileHeader(final ZipArchiveEntry ze) throws IOException { 1025 writeLocalFileHeader(ze, false); 1026 } 1027 1028 private void writeLocalFileHeader(final ZipArchiveEntry ze, final boolean phased) throws IOException { 1029 final boolean encodable = zipEncoding.canEncode(ze.getName()); 1030 final ByteBuffer name = getName(ze); 1031 1032 if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER) { 1033 addUnicodeExtraFields(ze, encodable, name); 1034 } 1035 1036 final long localHeaderStart = streamCompressor.getTotalBytesWritten(); 1037 final byte[] localHeader = createLocalFileHeader(ze, name, encodable, phased, localHeaderStart); 1038 metaData.put(ze, new EntryMetaData(localHeaderStart, usesDataDescriptor(ze.getMethod(), phased))); 1039 entry.localDataStart = localHeaderStart + LFH_CRC_OFFSET; // At crc offset 1040 writeCounted(localHeader); 1041 entry.dataStart = streamCompressor.getTotalBytesWritten(); 1042 } 1043 1044 1045 private byte[] createLocalFileHeader(final ZipArchiveEntry ze, final ByteBuffer name, final boolean encodable, 1046 final boolean phased, long archiveOffset) { 1047 ResourceAlignmentExtraField oldAlignmentEx = 1048 (ResourceAlignmentExtraField) ze.getExtraField(ResourceAlignmentExtraField.ID); 1049 if (oldAlignmentEx != null) { 1050 ze.removeExtraField(ResourceAlignmentExtraField.ID); 1051 } 1052 1053 int alignment = ze.getAlignment(); 1054 if (alignment <= 0 && oldAlignmentEx != null) { 1055 alignment = oldAlignmentEx.getAlignment(); 1056 } 1057 1058 if (alignment > 1 || (oldAlignmentEx != null && !oldAlignmentEx.allowMethodChange())) { 1059 int oldLength = LFH_FILENAME_OFFSET + 1060 name.limit() - name.position() + 1061 ze.getLocalFileDataExtra().length; 1062 1063 int padding = (int) ((-archiveOffset - oldLength - ZipExtraField.EXTRAFIELD_HEADER_SIZE 1064 - ResourceAlignmentExtraField.BASE_SIZE) & 1065 (alignment - 1)); 1066 ze.addExtraField(new ResourceAlignmentExtraField(alignment, 1067 oldAlignmentEx != null && oldAlignmentEx.allowMethodChange(), padding)); 1068 } 1069 1070 final byte[] extra = ze.getLocalFileDataExtra(); 1071 final int nameLen = name.limit() - name.position(); 1072 final int len = LFH_FILENAME_OFFSET + nameLen + extra.length; 1073 final byte[] buf = new byte[len]; 1074 1075 System.arraycopy(LFH_SIG, 0, buf, LFH_SIG_OFFSET, WORD); 1076 1077 //store method in local variable to prevent multiple method calls 1078 final int zipMethod = ze.getMethod(); 1079 final boolean dataDescriptor = usesDataDescriptor(zipMethod, phased); 1080 1081 putShort(versionNeededToExtract(zipMethod, hasZip64Extra(ze), dataDescriptor), buf, LFH_VERSION_NEEDED_OFFSET); 1082 1083 final GeneralPurposeBit generalPurposeBit = getGeneralPurposeBits(!encodable && fallbackToUTF8, dataDescriptor); 1084 generalPurposeBit.encode(buf, LFH_GPB_OFFSET); 1085 1086 // compression method 1087 putShort(zipMethod, buf, LFH_METHOD_OFFSET); 1088 1089 ZipUtil.toDosTime(calendarInstance, ze.getTime(), buf, LFH_TIME_OFFSET); 1090 1091 // CRC 1092 if (phased){ 1093 putLong(ze.getCrc(), buf, LFH_CRC_OFFSET); 1094 } else if (zipMethod == DEFLATED || channel != null) { 1095 System.arraycopy(LZERO, 0, buf, LFH_CRC_OFFSET, WORD); 1096 } else { 1097 putLong(ze.getCrc(), buf, LFH_CRC_OFFSET); 1098 } 1099 1100 // compressed length 1101 // uncompressed length 1102 if (hasZip64Extra(entry.entry)){ 1103 // point to ZIP64 extended information extra field for 1104 // sizes, may get rewritten once sizes are known if 1105 // stream is seekable 1106 ZipLong.ZIP64_MAGIC.putLong(buf, LFH_COMPRESSED_SIZE_OFFSET); 1107 ZipLong.ZIP64_MAGIC.putLong(buf, LFH_ORIGINAL_SIZE_OFFSET); 1108 } else if (phased) { 1109 putLong(ze.getCompressedSize(), buf, LFH_COMPRESSED_SIZE_OFFSET); 1110 putLong(ze.getSize(), buf, LFH_ORIGINAL_SIZE_OFFSET); 1111 } else if (zipMethod == DEFLATED || channel != null) { 1112 System.arraycopy(LZERO, 0, buf, LFH_COMPRESSED_SIZE_OFFSET, WORD); 1113 System.arraycopy(LZERO, 0, buf, LFH_ORIGINAL_SIZE_OFFSET, WORD); 1114 } else { // Stored 1115 putLong(ze.getSize(), buf, LFH_COMPRESSED_SIZE_OFFSET); 1116 putLong(ze.getSize(), buf, LFH_ORIGINAL_SIZE_OFFSET); 1117 } 1118 // file name length 1119 putShort(nameLen, buf, LFH_FILENAME_LENGTH_OFFSET); 1120 1121 // extra field length 1122 putShort(extra.length, buf, LFH_EXTRA_LENGTH_OFFSET); 1123 1124 // file name 1125 System.arraycopy( name.array(), name.arrayOffset(), buf, LFH_FILENAME_OFFSET, nameLen); 1126 1127 // extra fields 1128 System.arraycopy(extra, 0, buf, LFH_FILENAME_OFFSET + nameLen, extra.length); 1129 1130 return buf; 1131 } 1132 1133 1134 /** 1135 * Adds UnicodeExtra fields for name and file comment if mode is 1136 * ALWAYS or the data cannot be encoded using the configured 1137 * encoding. 1138 */ 1139 private void addUnicodeExtraFields(final ZipArchiveEntry ze, final boolean encodable, 1140 final ByteBuffer name) 1141 throws IOException { 1142 if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS 1143 || !encodable) { 1144 ze.addExtraField(new UnicodePathExtraField(ze.getName(), 1145 name.array(), 1146 name.arrayOffset(), 1147 name.limit() 1148 - name.position())); 1149 } 1150 1151 final String comm = ze.getComment(); 1152 if (comm != null && !"".equals(comm)) { 1153 1154 final boolean commentEncodable = zipEncoding.canEncode(comm); 1155 1156 if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS 1157 || !commentEncodable) { 1158 final ByteBuffer commentB = getEntryEncoding(ze).encode(comm); 1159 ze.addExtraField(new UnicodeCommentExtraField(comm, 1160 commentB.array(), 1161 commentB.arrayOffset(), 1162 commentB.limit() 1163 - commentB.position()) 1164 ); 1165 } 1166 } 1167 } 1168 1169 /** 1170 * Writes the data descriptor entry. 1171 * @param ze the entry to write 1172 * @throws IOException on error 1173 */ 1174 protected void writeDataDescriptor(final ZipArchiveEntry ze) throws IOException { 1175 if (!usesDataDescriptor(ze.getMethod(), false)) { 1176 return; 1177 } 1178 writeCounted(DD_SIG); 1179 writeCounted(ZipLong.getBytes(ze.getCrc())); 1180 if (!hasZip64Extra(ze)) { 1181 writeCounted(ZipLong.getBytes(ze.getCompressedSize())); 1182 writeCounted(ZipLong.getBytes(ze.getSize())); 1183 } else { 1184 writeCounted(ZipEightByteInteger.getBytes(ze.getCompressedSize())); 1185 writeCounted(ZipEightByteInteger.getBytes(ze.getSize())); 1186 } 1187 } 1188 1189 /** 1190 * Writes the central file header entry. 1191 * @param ze the entry to write 1192 * @throws IOException on error 1193 * @throws Zip64RequiredException if the archive's size exceeds 4 1194 * GByte and {@link Zip64Mode #setUseZip64} is {@link 1195 * Zip64Mode#Never}. 1196 */ 1197 protected void writeCentralFileHeader(final ZipArchiveEntry ze) throws IOException { 1198 final byte[] centralFileHeader = createCentralFileHeader(ze); 1199 writeCounted(centralFileHeader); 1200 } 1201 1202 private byte[] createCentralFileHeader(final ZipArchiveEntry ze) throws IOException { 1203 1204 final EntryMetaData entryMetaData = metaData.get(ze); 1205 final boolean needsZip64Extra = hasZip64Extra(ze) 1206 || ze.getCompressedSize() >= ZIP64_MAGIC 1207 || ze.getSize() >= ZIP64_MAGIC 1208 || entryMetaData.offset >= ZIP64_MAGIC 1209 || zip64Mode == Zip64Mode.Always; 1210 1211 if (needsZip64Extra && zip64Mode == Zip64Mode.Never) { 1212 // must be the offset that is too big, otherwise an 1213 // exception would have been throw in putArchiveEntry or 1214 // closeArchiveEntry 1215 throw new Zip64RequiredException(Zip64RequiredException 1216 .ARCHIVE_TOO_BIG_MESSAGE); 1217 } 1218 1219 1220 handleZip64Extra(ze, entryMetaData.offset, needsZip64Extra); 1221 1222 return createCentralFileHeader(ze, getName(ze), entryMetaData, needsZip64Extra); 1223 } 1224 1225 /** 1226 * Writes the central file header entry. 1227 * @param ze the entry to write 1228 * @param name The encoded name 1229 * @param entryMetaData meta data for this file 1230 * @throws IOException on error 1231 */ 1232 private byte[] createCentralFileHeader(final ZipArchiveEntry ze, final ByteBuffer name, 1233 final EntryMetaData entryMetaData, 1234 final boolean needsZip64Extra) throws IOException { 1235 final byte[] extra = ze.getCentralDirectoryExtra(); 1236 1237 // file comment length 1238 String comm = ze.getComment(); 1239 if (comm == null) { 1240 comm = ""; 1241 } 1242 1243 final ByteBuffer commentB = getEntryEncoding(ze).encode(comm); 1244 final int nameLen = name.limit() - name.position(); 1245 final int commentLen = commentB.limit() - commentB.position(); 1246 final int len= CFH_FILENAME_OFFSET + nameLen + extra.length + commentLen; 1247 final byte[] buf = new byte[len]; 1248 1249 System.arraycopy(CFH_SIG, 0, buf, CFH_SIG_OFFSET, WORD); 1250 1251 // version made by 1252 // CheckStyle:MagicNumber OFF 1253 putShort((ze.getPlatform() << 8) | (!hasUsedZip64 ? DATA_DESCRIPTOR_MIN_VERSION : ZIP64_MIN_VERSION), 1254 buf, CFH_VERSION_MADE_BY_OFFSET); 1255 1256 final int zipMethod = ze.getMethod(); 1257 final boolean encodable = zipEncoding.canEncode(ze.getName()); 1258 putShort(versionNeededToExtract(zipMethod, needsZip64Extra, entryMetaData.usesDataDescriptor), 1259 buf, CFH_VERSION_NEEDED_OFFSET); 1260 getGeneralPurposeBits(!encodable && fallbackToUTF8, entryMetaData.usesDataDescriptor).encode(buf, CFH_GPB_OFFSET); 1261 1262 // compression method 1263 putShort(zipMethod, buf, CFH_METHOD_OFFSET); 1264 1265 1266 // last mod. time and date 1267 ZipUtil.toDosTime(calendarInstance, ze.getTime(), buf, CFH_TIME_OFFSET); 1268 1269 // CRC 1270 // compressed length 1271 // uncompressed length 1272 putLong(ze.getCrc(), buf, CFH_CRC_OFFSET); 1273 if (ze.getCompressedSize() >= ZIP64_MAGIC 1274 || ze.getSize() >= ZIP64_MAGIC 1275 || zip64Mode == Zip64Mode.Always) { 1276 ZipLong.ZIP64_MAGIC.putLong(buf, CFH_COMPRESSED_SIZE_OFFSET); 1277 ZipLong.ZIP64_MAGIC.putLong(buf, CFH_ORIGINAL_SIZE_OFFSET); 1278 } else { 1279 putLong(ze.getCompressedSize(), buf, CFH_COMPRESSED_SIZE_OFFSET); 1280 putLong(ze.getSize(), buf, CFH_ORIGINAL_SIZE_OFFSET); 1281 } 1282 1283 putShort(nameLen, buf, CFH_FILENAME_LENGTH_OFFSET); 1284 1285 // extra field length 1286 putShort(extra.length, buf, CFH_EXTRA_LENGTH_OFFSET); 1287 1288 putShort(commentLen, buf, CFH_COMMENT_LENGTH_OFFSET); 1289 1290 // disk number start 1291 System.arraycopy(ZERO, 0, buf, CFH_DISK_NUMBER_OFFSET, SHORT); 1292 1293 // internal file attributes 1294 putShort(ze.getInternalAttributes(), buf, CFH_INTERNAL_ATTRIBUTES_OFFSET); 1295 1296 // external file attributes 1297 putLong(ze.getExternalAttributes(), buf, CFH_EXTERNAL_ATTRIBUTES_OFFSET); 1298 1299 // relative offset of LFH 1300 if (entryMetaData.offset >= ZIP64_MAGIC || zip64Mode == Zip64Mode.Always) { 1301 putLong(ZIP64_MAGIC, buf, CFH_LFH_OFFSET); 1302 } else { 1303 putLong(Math.min(entryMetaData.offset, ZIP64_MAGIC), buf, CFH_LFH_OFFSET); 1304 } 1305 1306 // file name 1307 System.arraycopy(name.array(), name.arrayOffset(), buf, CFH_FILENAME_OFFSET, nameLen); 1308 1309 final int extraStart = CFH_FILENAME_OFFSET + nameLen; 1310 System.arraycopy(extra, 0, buf, extraStart, extra.length); 1311 1312 final int commentStart = extraStart + extra.length; 1313 1314 // file comment 1315 System.arraycopy(commentB.array(), commentB.arrayOffset(), buf, commentStart, commentLen); 1316 return buf; 1317 } 1318 1319 /** 1320 * If the entry needs Zip64 extra information inside the central 1321 * directory then configure its data. 1322 */ 1323 private void handleZip64Extra(final ZipArchiveEntry ze, final long lfhOffset, 1324 final boolean needsZip64Extra) { 1325 if (needsZip64Extra) { 1326 final Zip64ExtendedInformationExtraField z64 = getZip64Extra(ze); 1327 if (ze.getCompressedSize() >= ZIP64_MAGIC 1328 || ze.getSize() >= ZIP64_MAGIC 1329 || zip64Mode == Zip64Mode.Always) { 1330 z64.setCompressedSize(new ZipEightByteInteger(ze.getCompressedSize())); 1331 z64.setSize(new ZipEightByteInteger(ze.getSize())); 1332 } else { 1333 // reset value that may have been set for LFH 1334 z64.setCompressedSize(null); 1335 z64.setSize(null); 1336 } 1337 if (lfhOffset >= ZIP64_MAGIC || zip64Mode == Zip64Mode.Always) { 1338 z64.setRelativeHeaderOffset(new ZipEightByteInteger(lfhOffset)); 1339 } 1340 ze.setExtra(); 1341 } 1342 } 1343 1344 /** 1345 * Writes the "End of central dir record". 1346 * @throws IOException on error 1347 * @throws Zip64RequiredException if the archive's size exceeds 4 1348 * GByte or there are more than 65535 entries inside the archive 1349 * and {@link Zip64Mode #setUseZip64} is {@link Zip64Mode#Never}. 1350 */ 1351 protected void writeCentralDirectoryEnd() throws IOException { 1352 writeCounted(EOCD_SIG); 1353 1354 // disk numbers 1355 writeCounted(ZERO); 1356 writeCounted(ZERO); 1357 1358 // number of entries 1359 final int numberOfEntries = entries.size(); 1360 if (numberOfEntries > ZIP64_MAGIC_SHORT 1361 && zip64Mode == Zip64Mode.Never) { 1362 throw new Zip64RequiredException(Zip64RequiredException 1363 .TOO_MANY_ENTRIES_MESSAGE); 1364 } 1365 if (cdOffset > ZIP64_MAGIC && zip64Mode == Zip64Mode.Never) { 1366 throw new Zip64RequiredException(Zip64RequiredException 1367 .ARCHIVE_TOO_BIG_MESSAGE); 1368 } 1369 1370 final byte[] num = ZipShort.getBytes(Math.min(numberOfEntries, 1371 ZIP64_MAGIC_SHORT)); 1372 writeCounted(num); 1373 writeCounted(num); 1374 1375 // length and location of CD 1376 writeCounted(ZipLong.getBytes(Math.min(cdLength, ZIP64_MAGIC))); 1377 writeCounted(ZipLong.getBytes(Math.min(cdOffset, ZIP64_MAGIC))); 1378 1379 // ZIP file comment 1380 final ByteBuffer data = this.zipEncoding.encode(comment); 1381 final int dataLen = data.limit() - data.position(); 1382 writeCounted(ZipShort.getBytes(dataLen)); 1383 streamCompressor.writeCounted(data.array(), data.arrayOffset(), dataLen); 1384 } 1385 1386 /** 1387 * Writes the "ZIP64 End of central dir record" and 1388 * "ZIP64 End of central dir locator". 1389 * @throws IOException on error 1390 * @since 1.3 1391 */ 1392 protected void writeZip64CentralDirectory() throws IOException { 1393 if (zip64Mode == Zip64Mode.Never) { 1394 return; 1395 } 1396 1397 if (!hasUsedZip64 1398 && (cdOffset >= ZIP64_MAGIC || cdLength >= ZIP64_MAGIC 1399 || entries.size() >= ZIP64_MAGIC_SHORT)) { 1400 // actually "will use" 1401 hasUsedZip64 = true; 1402 } 1403 1404 if (!hasUsedZip64) { 1405 return; 1406 } 1407 1408 final long offset = streamCompressor.getTotalBytesWritten(); 1409 1410 writeOut(ZIP64_EOCD_SIG); 1411 // size, we don't have any variable length as we don't support 1412 // the extensible data sector, yet 1413 writeOut(ZipEightByteInteger 1414 .getBytes(SHORT /* version made by */ 1415 + SHORT /* version needed to extract */ 1416 + WORD /* disk number */ 1417 + WORD /* disk with central directory */ 1418 + DWORD /* number of entries in CD on this disk */ 1419 + DWORD /* total number of entries */ 1420 + DWORD /* size of CD */ 1421 + (long) DWORD /* offset of CD */ 1422 )); 1423 1424 // version made by and version needed to extract 1425 writeOut(ZipShort.getBytes(ZIP64_MIN_VERSION)); 1426 writeOut(ZipShort.getBytes(ZIP64_MIN_VERSION)); 1427 1428 // disk numbers - four bytes this time 1429 writeOut(LZERO); 1430 writeOut(LZERO); 1431 1432 // number of entries 1433 final byte[] num = ZipEightByteInteger.getBytes(entries.size()); 1434 writeOut(num); 1435 writeOut(num); 1436 1437 // length and location of CD 1438 writeOut(ZipEightByteInteger.getBytes(cdLength)); 1439 writeOut(ZipEightByteInteger.getBytes(cdOffset)); 1440 1441 // no "zip64 extensible data sector" for now 1442 1443 // and now the "ZIP64 end of central directory locator" 1444 writeOut(ZIP64_EOCD_LOC_SIG); 1445 1446 // disk number holding the ZIP64 EOCD record 1447 writeOut(LZERO); 1448 // relative offset of ZIP64 EOCD record 1449 writeOut(ZipEightByteInteger.getBytes(offset)); 1450 // total number of disks 1451 writeOut(ONE); 1452 } 1453 1454 /** 1455 * Write bytes to output or random access file. 1456 * @param data the byte array to write 1457 * @throws IOException on error 1458 */ 1459 protected final void writeOut(final byte[] data) throws IOException { 1460 streamCompressor.writeOut(data, 0, data.length); 1461 } 1462 1463 1464 /** 1465 * Write bytes to output or random access file. 1466 * @param data the byte array to write 1467 * @param offset the start position to write from 1468 * @param length the number of bytes to write 1469 * @throws IOException on error 1470 */ 1471 protected final void writeOut(final byte[] data, final int offset, final int length) 1472 throws IOException { 1473 streamCompressor.writeOut(data, offset, length); 1474 } 1475 1476 1477 private GeneralPurposeBit getGeneralPurposeBits(final boolean utfFallback, boolean usesDataDescriptor) { 1478 final GeneralPurposeBit b = new GeneralPurposeBit(); 1479 b.useUTF8ForNames(useUTF8Flag || utfFallback); 1480 if (usesDataDescriptor) { 1481 b.useDataDescriptor(true); 1482 } 1483 return b; 1484 } 1485 1486 private int versionNeededToExtract(final int zipMethod, final boolean zip64, final boolean usedDataDescriptor) { 1487 if (zip64) { 1488 return ZIP64_MIN_VERSION; 1489 } 1490 if (usedDataDescriptor) { 1491 return DATA_DESCRIPTOR_MIN_VERSION; 1492 } 1493 return versionNeededToExtractMethod(zipMethod); 1494 } 1495 1496 private boolean usesDataDescriptor(final int zipMethod, boolean phased) { 1497 return !phased && zipMethod == DEFLATED && channel == null; 1498 } 1499 1500 private int versionNeededToExtractMethod(int zipMethod) { 1501 return zipMethod == DEFLATED ? DEFLATE_MIN_VERSION : INITIAL_VERSION; 1502 } 1503 1504 /** 1505 * Creates a new zip entry taking some information from the given 1506 * file and using the provided name. 1507 * 1508 * <p>The name will be adjusted to end with a forward slash "/" if 1509 * the file is a directory. If the file is not a directory a 1510 * potential trailing forward slash will be stripped from the 1511 * entry name.</p> 1512 * 1513 * <p>Must not be used if the stream has already been closed.</p> 1514 */ 1515 @Override 1516 public ArchiveEntry createArchiveEntry(final File inputFile, final String entryName) 1517 throws IOException { 1518 if (finished) { 1519 throw new IOException("Stream has already been finished"); 1520 } 1521 return new ZipArchiveEntry(inputFile, entryName); 1522 } 1523 1524 /** 1525 * Get the existing ZIP64 extended information extra field or 1526 * create a new one and add it to the entry. 1527 * 1528 * @since 1.3 1529 */ 1530 private Zip64ExtendedInformationExtraField 1531 getZip64Extra(final ZipArchiveEntry ze) { 1532 if (entry != null) { 1533 entry.causedUseOfZip64 = !hasUsedZip64; 1534 } 1535 hasUsedZip64 = true; 1536 Zip64ExtendedInformationExtraField z64 = 1537 (Zip64ExtendedInformationExtraField) 1538 ze.getExtraField(Zip64ExtendedInformationExtraField 1539 .HEADER_ID); 1540 if (z64 == null) { 1541 /* 1542 System.err.println("Adding z64 for " + ze.getName() 1543 + ", method: " + ze.getMethod() 1544 + " (" + (ze.getMethod() == STORED) + ")" 1545 + ", channel: " + (channel != null)); 1546 */ 1547 z64 = new Zip64ExtendedInformationExtraField(); 1548 } 1549 1550 // even if the field is there already, make sure it is the first one 1551 ze.addAsFirstExtraField(z64); 1552 1553 return z64; 1554 } 1555 1556 /** 1557 * Is there a ZIP64 extended information extra field for the 1558 * entry? 1559 * 1560 * @since 1.3 1561 */ 1562 private boolean hasZip64Extra(final ZipArchiveEntry ze) { 1563 return ze.getExtraField(Zip64ExtendedInformationExtraField 1564 .HEADER_ID) 1565 != null; 1566 } 1567 1568 /** 1569 * If the mode is AsNeeded and the entry is a compressed entry of 1570 * unknown size that gets written to a non-seekable stream then 1571 * change the default to Never. 1572 * 1573 * @since 1.3 1574 */ 1575 private Zip64Mode getEffectiveZip64Mode(final ZipArchiveEntry ze) { 1576 if (zip64Mode != Zip64Mode.AsNeeded 1577 || channel != null 1578 || ze.getMethod() != DEFLATED 1579 || ze.getSize() != ArchiveEntry.SIZE_UNKNOWN) { 1580 return zip64Mode; 1581 } 1582 return Zip64Mode.Never; 1583 } 1584 1585 private ZipEncoding getEntryEncoding(final ZipArchiveEntry ze) { 1586 final boolean encodable = zipEncoding.canEncode(ze.getName()); 1587 return !encodable && fallbackToUTF8 1588 ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding; 1589 } 1590 1591 private ByteBuffer getName(final ZipArchiveEntry ze) throws IOException { 1592 return getEntryEncoding(ze).encode(ze.getName()); 1593 } 1594 1595 /** 1596 * Closes the underlying stream/file without finishing the 1597 * archive, the result will likely be a corrupt archive. 1598 * 1599 * <p>This method only exists to support tests that generate 1600 * corrupt archives so they can clean up any temporary files.</p> 1601 */ 1602 void destroy() throws IOException { 1603 try { 1604 if (channel != null) { 1605 channel.close(); 1606 } 1607 } finally { 1608 if (out != null) { 1609 out.close(); 1610 } 1611 } 1612 } 1613 1614 /** 1615 * enum that represents the possible policies for creating Unicode 1616 * extra fields. 1617 */ 1618 public static final class UnicodeExtraFieldPolicy { 1619 /** 1620 * Always create Unicode extra fields. 1621 */ 1622 public static final UnicodeExtraFieldPolicy ALWAYS = new UnicodeExtraFieldPolicy("always"); 1623 /** 1624 * Never create Unicode extra fields. 1625 */ 1626 public static final UnicodeExtraFieldPolicy NEVER = new UnicodeExtraFieldPolicy("never"); 1627 /** 1628 * Create Unicode extra fields for filenames that cannot be 1629 * encoded using the specified encoding. 1630 */ 1631 public static final UnicodeExtraFieldPolicy NOT_ENCODEABLE = 1632 new UnicodeExtraFieldPolicy("not encodeable"); 1633 1634 private final String name; 1635 private UnicodeExtraFieldPolicy(final String n) { 1636 name = n; 1637 } 1638 @Override 1639 public String toString() { 1640 return name; 1641 } 1642 } 1643 1644 /** 1645 * Structure collecting information for the entry that is 1646 * currently being written. 1647 */ 1648 private static final class CurrentEntry { 1649 private CurrentEntry(final ZipArchiveEntry entry) { 1650 this.entry = entry; 1651 } 1652 /** 1653 * Current ZIP entry. 1654 */ 1655 private final ZipArchiveEntry entry; 1656 /** 1657 * Offset for CRC entry in the local file header data for the 1658 * current entry starts here. 1659 */ 1660 private long localDataStart = 0; 1661 /** 1662 * Data for local header data 1663 */ 1664 private long dataStart = 0; 1665 /** 1666 * Number of bytes read for the current entry (can't rely on 1667 * Deflater#getBytesRead) when using DEFLATED. 1668 */ 1669 private long bytesRead = 0; 1670 /** 1671 * Whether current entry was the first one using ZIP64 features. 1672 */ 1673 private boolean causedUseOfZip64 = false; 1674 /** 1675 * Whether write() has been called at all. 1676 * 1677 * <p>In order to create a valid archive {@link 1678 * #closeArchiveEntry closeArchiveEntry} will write an empty 1679 * array to get the CRC right if nothing has been written to 1680 * the stream at all.</p> 1681 */ 1682 private boolean hasWritten; 1683 } 1684 1685 private static final class EntryMetaData { 1686 private final long offset; 1687 private final boolean usesDataDescriptor; 1688 private EntryMetaData(long offset, boolean usesDataDescriptor) { 1689 this.offset = offset; 1690 this.usesDataDescriptor = usesDataDescriptor; 1691 } 1692 } 1693}