001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress.archivers.tar; 020 021import java.io.File; 022import java.io.IOException; 023import java.io.OutputStream; 024import java.io.StringWriter; 025import java.io.UnsupportedEncodingException; 026import java.nio.ByteBuffer; 027import java.util.Arrays; 028import java.util.Date; 029import java.util.HashMap; 030import java.util.Map; 031import org.apache.commons.compress.archivers.ArchiveEntry; 032import org.apache.commons.compress.archivers.ArchiveOutputStream; 033import org.apache.commons.compress.archivers.zip.ZipEncoding; 034import org.apache.commons.compress.archivers.zip.ZipEncodingHelper; 035import org.apache.commons.compress.utils.CharsetNames; 036import org.apache.commons.compress.utils.CountingOutputStream; 037import org.apache.commons.compress.utils.FixedLengthBlockOutputStream; 038 039/** 040 * The TarOutputStream writes a UNIX tar archive as an OutputStream. Methods are provided to put 041 * entries, and then write their contents by writing to this stream using write(). 042 * 043 * <p>tar archives consist of a sequence of records of 512 bytes each 044 * that are grouped into blocks. Prior to Apache Commons Compress 1.14 045 * it has been possible to configure a record size different from 512 046 * bytes and arbitrary block sizes. Starting with Compress 1.15 512 is 047 * the only valid option for the record size and the block size must 048 * be a multiple of 512. Also the default block size changed from 049 * 10240 bytes prior to Compress 1.15 to 512 bytes with Compress 050 * 1.15.</p> 051 * 052 * @NotThreadSafe 053 */ 054public class TarArchiveOutputStream extends ArchiveOutputStream { 055 056 /** 057 * Fail if a long file name is required in the archive. 058 */ 059 public static final int LONGFILE_ERROR = 0; 060 061 /** 062 * Long paths will be truncated in the archive. 063 */ 064 public static final int LONGFILE_TRUNCATE = 1; 065 066 /** 067 * GNU tar extensions are used to store long file names in the archive. 068 */ 069 public static final int LONGFILE_GNU = 2; 070 071 /** 072 * POSIX/PAX extensions are used to store long file names in the archive. 073 */ 074 public static final int LONGFILE_POSIX = 3; 075 076 /** 077 * Fail if a big number (e.g. size > 8GiB) is required in the archive. 078 */ 079 public static final int BIGNUMBER_ERROR = 0; 080 081 /** 082 * star/GNU tar/BSD tar extensions are used to store big number in the archive. 083 */ 084 public static final int BIGNUMBER_STAR = 1; 085 086 /** 087 * POSIX/PAX extensions are used to store big numbers in the archive. 088 */ 089 public static final int BIGNUMBER_POSIX = 2; 090 private static final int RECORD_SIZE = 512; 091 092 private long currSize; 093 private String currName; 094 private long currBytes; 095 private final byte[] recordBuf; 096 private int longFileMode = LONGFILE_ERROR; 097 private int bigNumberMode = BIGNUMBER_ERROR; 098 private int recordsWritten; 099 private final int recordsPerBlock; 100 101 private boolean closed = false; 102 103 /** 104 * Indicates if putArchiveEntry has been called without closeArchiveEntry 105 */ 106 private boolean haveUnclosedEntry = false; 107 108 /** 109 * indicates if this archive is finished 110 */ 111 private boolean finished = false; 112 113 private final FixedLengthBlockOutputStream out; 114 private final CountingOutputStream countingOut; 115 116 private final ZipEncoding zipEncoding; 117 118 // the provided encoding (for unit tests) 119 final String encoding; 120 121 private boolean addPaxHeadersForNonAsciiNames = false; 122 private static final ZipEncoding ASCII = 123 ZipEncodingHelper.getZipEncoding("ASCII"); 124 125 private static final int BLOCK_SIZE_UNSPECIFIED = -511; 126 127 /** 128 * Constructor for TarArchiveOutputStream. 129 * 130 * <p>Uses a block size of 512 bytes.</p> 131 * 132 * @param os the output stream to use 133 */ 134 public TarArchiveOutputStream(final OutputStream os) { 135 this(os, BLOCK_SIZE_UNSPECIFIED); 136 } 137 138 /** 139 * Constructor for TarArchiveOutputStream. 140 * 141 * <p>Uses a block size of 512 bytes.</p> 142 * 143 * @param os the output stream to use 144 * @param encoding name of the encoding to use for file names 145 * @since 1.4 146 */ 147 public TarArchiveOutputStream(final OutputStream os, final String encoding) { 148 this(os, BLOCK_SIZE_UNSPECIFIED, encoding); 149 } 150 151 /** 152 * Constructor for TarArchiveOutputStream. 153 * 154 * @param os the output stream to use 155 * @param blockSize the block size to use. Must be a multiple of 512 bytes. 156 */ 157 public TarArchiveOutputStream(final OutputStream os, final int blockSize) { 158 this(os, blockSize, null); 159 } 160 161 162 /** 163 * Constructor for TarArchiveOutputStream. 164 * 165 * @param os the output stream to use 166 * @param blockSize the block size to use 167 * @param recordSize the record size to use. Must be 512 bytes. 168 * @deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown 169 * if any other value is used 170 */ 171 @Deprecated 172 public TarArchiveOutputStream(final OutputStream os, final int blockSize, 173 final int recordSize) { 174 this(os, blockSize, recordSize, null); 175 } 176 177 /** 178 * Constructor for TarArchiveOutputStream. 179 * 180 * @param os the output stream to use 181 * @param blockSize the block size to use . Must be a multiple of 512 bytes. 182 * @param recordSize the record size to use. Must be 512 bytes. 183 * @param encoding name of the encoding to use for file names 184 * @since 1.4 185 * @deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown 186 * if any other value is used. 187 */ 188 @Deprecated 189 public TarArchiveOutputStream(final OutputStream os, final int blockSize, 190 final int recordSize, final String encoding) { 191 this(os, blockSize, encoding); 192 if (recordSize != RECORD_SIZE) { 193 throw new IllegalArgumentException( 194 "Tar record size must always be 512 bytes. Attempt to set size of " + recordSize); 195 } 196 197 } 198 199 /** 200 * Constructor for TarArchiveOutputStream. 201 * 202 * @param os the output stream to use 203 * @param blockSize the block size to use. Must be a multiple of 512 bytes. 204 * @param encoding name of the encoding to use for file names 205 * @since 1.4 206 */ 207 public TarArchiveOutputStream(final OutputStream os, final int blockSize, 208 final String encoding) { 209 int realBlockSize; 210 if (BLOCK_SIZE_UNSPECIFIED == blockSize) { 211 realBlockSize = RECORD_SIZE; 212 } else { 213 realBlockSize = blockSize; 214 } 215 216 if (realBlockSize <=0 || realBlockSize % RECORD_SIZE != 0) { 217 throw new IllegalArgumentException("Block size must be a multiple of 512 bytes. Attempt to use set size of " + blockSize); 218 } 219 out = new FixedLengthBlockOutputStream(countingOut = new CountingOutputStream(os), 220 RECORD_SIZE); 221 this.encoding = encoding; 222 this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); 223 224 this.recordBuf = new byte[RECORD_SIZE]; 225 this.recordsPerBlock = realBlockSize / RECORD_SIZE; 226 } 227 228 /** 229 * Set the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1) or 230 * LONGFILE_GNU(2). This specifies the treatment of long file names (names >= 231 * TarConstants.NAMELEN). Default is LONGFILE_ERROR. 232 * 233 * @param longFileMode the mode to use 234 */ 235 public void setLongFileMode(final int longFileMode) { 236 this.longFileMode = longFileMode; 237 } 238 239 /** 240 * Set the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_POSIX(1) or 241 * BIGNUMBER_STAR(2). This specifies the treatment of big files (sizes > 242 * TarConstants.MAXSIZE) and other numeric values to big to fit into a traditional tar header. 243 * Default is BIGNUMBER_ERROR. 244 * 245 * @param bigNumberMode the mode to use 246 * @since 1.4 247 */ 248 public void setBigNumberMode(final int bigNumberMode) { 249 this.bigNumberMode = bigNumberMode; 250 } 251 252 /** 253 * Whether to add a PAX extension header for non-ASCII file names. 254 * 255 * @param b whether to add a PAX extension header for non-ASCII file names. 256 * @since 1.4 257 */ 258 public void setAddPaxHeadersForNonAsciiNames(final boolean b) { 259 addPaxHeadersForNonAsciiNames = b; 260 } 261 262 @Deprecated 263 @Override 264 public int getCount() { 265 return (int) getBytesWritten(); 266 } 267 268 @Override 269 public long getBytesWritten() { 270 return countingOut.getBytesWritten(); 271 } 272 273 /** 274 * Ends the TAR archive without closing the underlying OutputStream. 275 * 276 * An archive consists of a series of file entries terminated by an 277 * end-of-archive entry, which consists of two 512 blocks of zero bytes. 278 * POSIX.1 requires two EOF records, like some other implementations. 279 * 280 * @throws IOException on error 281 */ 282 @Override 283 public void finish() throws IOException { 284 if (finished) { 285 throw new IOException("This archive has already been finished"); 286 } 287 288 if (haveUnclosedEntry) { 289 throw new IOException("This archive contains unclosed entries."); 290 } 291 writeEOFRecord(); 292 writeEOFRecord(); 293 padAsNeeded(); 294 out.flush(); 295 finished = true; 296 } 297 298 /** 299 * Closes the underlying OutputStream. 300 * 301 * @throws IOException on error 302 */ 303 @Override 304 public void close() throws IOException { 305 try { 306 if (!finished) { 307 finish(); 308 } 309 } finally { 310 if (!closed) { 311 out.close(); 312 closed = true; 313 } 314 } 315 } 316 317 /** 318 * Get the record size being used by this stream's TarBuffer. 319 * 320 * @return The TarBuffer record size. 321 * @deprecated 322 */ 323 @Deprecated 324 public int getRecordSize() { 325 return RECORD_SIZE; 326 } 327 328 /** 329 * Put an entry on the output stream. This writes the entry's header record and positions the 330 * output stream for writing the contents of the entry. Once this method is called, the stream 331 * is ready for calls to write() to write the entry's contents. Once the contents are written, 332 * closeArchiveEntry() <B>MUST</B> be called to ensure that all buffered data is completely 333 * written to the output stream. 334 * 335 * @param archiveEntry The TarEntry to be written to the archive. 336 * @throws IOException on error 337 * @throws ClassCastException if archiveEntry is not an instance of TarArchiveEntry 338 */ 339 @Override 340 public void putArchiveEntry(final ArchiveEntry archiveEntry) throws IOException { 341 if (finished) { 342 throw new IOException("Stream has already been finished"); 343 } 344 final TarArchiveEntry entry = (TarArchiveEntry) archiveEntry; 345 if (entry.isGlobalPaxHeader()) { 346 final byte[] data = encodeExtendedPaxHeadersContents(entry.getExtraPaxHeaders()); 347 entry.setSize(data.length); 348 entry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR); 349 writeRecord(recordBuf); 350 currSize= entry.getSize(); 351 currBytes = 0; 352 this.haveUnclosedEntry = true; 353 write(data); 354 closeArchiveEntry(); 355 } else { 356 final Map<String, String> paxHeaders = new HashMap<>(); 357 final String entryName = entry.getName(); 358 final boolean paxHeaderContainsPath = handleLongName(entry, entryName, paxHeaders, "path", 359 TarConstants.LF_GNUTYPE_LONGNAME, "file name"); 360 361 final String linkName = entry.getLinkName(); 362 final boolean paxHeaderContainsLinkPath = linkName != null && linkName.length() > 0 363 && handleLongName(entry, linkName, paxHeaders, "linkpath", 364 TarConstants.LF_GNUTYPE_LONGLINK, "link name"); 365 366 if (bigNumberMode == BIGNUMBER_POSIX) { 367 addPaxHeadersForBigNumbers(paxHeaders, entry); 368 } else if (bigNumberMode != BIGNUMBER_STAR) { 369 failForBigNumbers(entry); 370 } 371 372 if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsPath 373 && !ASCII.canEncode(entryName)) { 374 paxHeaders.put("path", entryName); 375 } 376 377 if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsLinkPath 378 && (entry.isLink() || entry.isSymbolicLink()) 379 && !ASCII.canEncode(linkName)) { 380 paxHeaders.put("linkpath", linkName); 381 } 382 paxHeaders.putAll(entry.getExtraPaxHeaders()); 383 384 if (paxHeaders.size() > 0) { 385 writePaxHeaders(entry, entryName, paxHeaders); 386 } 387 388 entry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR); 389 writeRecord(recordBuf); 390 391 currBytes = 0; 392 393 if (entry.isDirectory()) { 394 currSize = 0; 395 } else { 396 currSize = entry.getSize(); 397 } 398 currName = entryName; 399 haveUnclosedEntry = true; 400 } 401 } 402 403 /** 404 * Close an entry. This method MUST be called for all file entries that contain data. The reason 405 * is that we must buffer data written to the stream in order to satisfy the buffer's record 406 * based writes. Thus, there may be data fragments still being assembled that must be written to 407 * the output stream before this entry is closed and the next entry written. 408 * 409 * @throws IOException on error 410 */ 411 @Override 412 public void closeArchiveEntry() throws IOException { 413 if (finished) { 414 throw new IOException("Stream has already been finished"); 415 } 416 if (!haveUnclosedEntry) { 417 throw new IOException("No current entry to close"); 418 } 419 out.flushBlock(); 420 if (currBytes < currSize) { 421 throw new IOException("entry '" + currName + "' closed at '" 422 + currBytes 423 + "' before the '" + currSize 424 + "' bytes specified in the header were written"); 425 } 426 recordsWritten += (currSize / RECORD_SIZE); 427 if (0 != currSize % RECORD_SIZE) { 428 recordsWritten++; 429 } 430 haveUnclosedEntry = false; 431 } 432 433 /** 434 * Writes bytes to the current tar archive entry. This method is aware of the current entry and 435 * will throw an exception if you attempt to write bytes past the length specified for the 436 * current entry. 437 * 438 * @param wBuf The buffer to write to the archive. 439 * @param wOffset The offset in the buffer from which to get bytes. 440 * @param numToWrite The number of bytes to write. 441 * @throws IOException on error 442 */ 443 @Override 444 public void write(final byte[] wBuf, int wOffset, int numToWrite) throws IOException { 445 if (!haveUnclosedEntry) { 446 throw new IllegalStateException("No current tar entry"); 447 } 448 if (currBytes + numToWrite > currSize) { 449 throw new IOException("request to write '" + numToWrite 450 + "' bytes exceeds size in header of '" 451 + currSize + "' bytes for entry '" 452 + currName + "'"); 453 } 454 out.write(wBuf, wOffset, numToWrite); 455 currBytes += numToWrite; 456 } 457 458 /** 459 * Writes a PAX extended header with the given map as contents. 460 * 461 * @since 1.4 462 */ 463 void writePaxHeaders(final TarArchiveEntry entry, 464 final String entryName, 465 final Map<String, String> headers) throws IOException { 466 String name = "./PaxHeaders.X/" + stripTo7Bits(entryName); 467 if (name.length() >= TarConstants.NAMELEN) { 468 name = name.substring(0, TarConstants.NAMELEN - 1); 469 } 470 final TarArchiveEntry pex = new TarArchiveEntry(name, 471 TarConstants.LF_PAX_EXTENDED_HEADER_LC); 472 transferModTime(entry, pex); 473 474 final byte[] data = encodeExtendedPaxHeadersContents(headers); 475 pex.setSize(data.length); 476 putArchiveEntry(pex); 477 write(data); 478 closeArchiveEntry(); 479 } 480 481 private byte[] encodeExtendedPaxHeadersContents(Map<String, String> headers) 482 throws UnsupportedEncodingException { 483 final StringWriter w = new StringWriter(); 484 for (final Map.Entry<String, String> h : headers.entrySet()) { 485 final String key = h.getKey(); 486 final String value = h.getValue(); 487 int len = key.length() + value.length() 488 + 3 /* blank, equals and newline */ 489 + 2 /* guess 9 < actual length < 100 */; 490 String line = len + " " + key + "=" + value + "\n"; 491 int actualLength = line.getBytes(CharsetNames.UTF_8).length; 492 while (len != actualLength) { 493 // Adjust for cases where length < 10 or > 100 494 // or where UTF-8 encoding isn't a single octet 495 // per character. 496 // Must be in loop as size may go from 99 to 100 in 497 // first pass so we'd need a second. 498 len = actualLength; 499 line = len + " " + key + "=" + value + "\n"; 500 actualLength = line.getBytes(CharsetNames.UTF_8).length; 501 } 502 w.write(line); 503 } 504 return w.toString().getBytes(CharsetNames.UTF_8); 505 } 506 507 private String stripTo7Bits(final String name) { 508 final int length = name.length(); 509 final StringBuilder result = new StringBuilder(length); 510 for (int i = 0; i < length; i++) { 511 final char stripped = (char) (name.charAt(i) & 0x7F); 512 if (shouldBeReplaced(stripped)) { 513 result.append("_"); 514 } else { 515 result.append(stripped); 516 } 517 } 518 return result.toString(); 519 } 520 521 /** 522 * @return true if the character could lead to problems when used inside a TarArchiveEntry name 523 * for a PAX header. 524 */ 525 private boolean shouldBeReplaced(final char c) { 526 return c == 0 // would be read as Trailing null 527 || c == '/' // when used as last character TAE will consider the PAX header a directory 528 || c == '\\'; // same as '/' as slashes get "normalized" on Windows 529 } 530 531 /** 532 * Write an EOF (end of archive) record to the tar archive. An EOF record consists of a record 533 * of all zeros. 534 */ 535 private void writeEOFRecord() throws IOException { 536 Arrays.fill(recordBuf, (byte) 0); 537 writeRecord(recordBuf); 538 } 539 540 @Override 541 public void flush() throws IOException { 542 out.flush(); 543 } 544 545 @Override 546 public ArchiveEntry createArchiveEntry(final File inputFile, final String entryName) 547 throws IOException { 548 if (finished) { 549 throw new IOException("Stream has already been finished"); 550 } 551 return new TarArchiveEntry(inputFile, entryName); 552 } 553 554 /** 555 * Write an archive record to the archive. 556 * 557 * @param record The record data to write to the archive. 558 * @throws IOException on error 559 */ 560 private void writeRecord(final byte[] record) throws IOException { 561 if (record.length != RECORD_SIZE) { 562 throw new IOException("record to write has length '" 563 + record.length 564 + "' which is not the record size of '" 565 + RECORD_SIZE + "'"); 566 } 567 568 out.write(record); 569 recordsWritten++; 570 } 571 572 private void padAsNeeded() throws IOException { 573 final int start = recordsWritten % recordsPerBlock; 574 if (start != 0) { 575 for (int i = start; i < recordsPerBlock; i++) { 576 writeEOFRecord(); 577 } 578 } 579 } 580 581 private void addPaxHeadersForBigNumbers(final Map<String, String> paxHeaders, 582 final TarArchiveEntry entry) { 583 addPaxHeaderForBigNumber(paxHeaders, "size", entry.getSize(), 584 TarConstants.MAXSIZE); 585 addPaxHeaderForBigNumber(paxHeaders, "gid", entry.getLongGroupId(), 586 TarConstants.MAXID); 587 addPaxHeaderForBigNumber(paxHeaders, "mtime", 588 entry.getModTime().getTime() / 1000, 589 TarConstants.MAXSIZE); 590 addPaxHeaderForBigNumber(paxHeaders, "uid", entry.getLongUserId(), 591 TarConstants.MAXID); 592 // star extensions by J\u00f6rg Schilling 593 addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devmajor", 594 entry.getDevMajor(), TarConstants.MAXID); 595 addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devminor", 596 entry.getDevMinor(), TarConstants.MAXID); 597 // there is no PAX header for file mode 598 failForBigNumber("mode", entry.getMode(), TarConstants.MAXID); 599 } 600 601 private void addPaxHeaderForBigNumber(final Map<String, String> paxHeaders, 602 final String header, final long value, 603 final long maxValue) { 604 if (value < 0 || value > maxValue) { 605 paxHeaders.put(header, String.valueOf(value)); 606 } 607 } 608 609 private void failForBigNumbers(final TarArchiveEntry entry) { 610 failForBigNumber("entry size", entry.getSize(), TarConstants.MAXSIZE); 611 failForBigNumberWithPosixMessage("group id", entry.getLongGroupId(), TarConstants.MAXID); 612 failForBigNumber("last modification time", 613 entry.getModTime().getTime() / 1000, 614 TarConstants.MAXSIZE); 615 failForBigNumber("user id", entry.getLongUserId(), TarConstants.MAXID); 616 failForBigNumber("mode", entry.getMode(), TarConstants.MAXID); 617 failForBigNumber("major device number", entry.getDevMajor(), 618 TarConstants.MAXID); 619 failForBigNumber("minor device number", entry.getDevMinor(), 620 TarConstants.MAXID); 621 } 622 623 private void failForBigNumber(final String field, final long value, final long maxValue) { 624 failForBigNumber(field, value, maxValue, ""); 625 } 626 627 private void failForBigNumberWithPosixMessage(final String field, final long value, 628 final long maxValue) { 629 failForBigNumber(field, value, maxValue, 630 " Use STAR or POSIX extensions to overcome this limit"); 631 } 632 633 private void failForBigNumber(final String field, final long value, final long maxValue, 634 final String additionalMsg) { 635 if (value < 0 || value > maxValue) { 636 throw new RuntimeException(field + " '" + value //NOSONAR 637 + "' is too big ( > " 638 + maxValue + " )." + additionalMsg); 639 } 640 } 641 642 /** 643 * Handles long file or link names according to the longFileMode setting. 644 * 645 * <p>I.e. if the given name is too long to be written to a plain tar header then <ul> <li>it 646 * creates a pax header who's name is given by the paxHeaderName parameter if longFileMode is 647 * POSIX</li> <li>it creates a GNU longlink entry who's type is given by the linkType parameter 648 * if longFileMode is GNU</li> <li>it throws an exception if longFileMode is ERROR</li> <li>it 649 * truncates the name if longFileMode is TRUNCATE</li> </ul></p> 650 * 651 * @param entry entry the name belongs to 652 * @param name the name to write 653 * @param paxHeaders current map of pax headers 654 * @param paxHeaderName name of the pax header to write 655 * @param linkType type of the GNU entry to write 656 * @param fieldName the name of the field 657 * @return whether a pax header has been written. 658 */ 659 private boolean handleLongName(final TarArchiveEntry entry, final String name, 660 final Map<String, String> paxHeaders, 661 final String paxHeaderName, final byte linkType, final String fieldName) 662 throws IOException { 663 final ByteBuffer encodedName = zipEncoding.encode(name); 664 final int len = encodedName.limit() - encodedName.position(); 665 if (len >= TarConstants.NAMELEN) { 666 667 if (longFileMode == LONGFILE_POSIX) { 668 paxHeaders.put(paxHeaderName, name); 669 return true; 670 } else if (longFileMode == LONGFILE_GNU) { 671 // create a TarEntry for the LongLink, the contents 672 // of which are the link's name 673 final TarArchiveEntry longLinkEntry = new TarArchiveEntry(TarConstants.GNU_LONGLINK, 674 linkType); 675 676 longLinkEntry.setSize(len + 1L); // +1 for NUL 677 transferModTime(entry, longLinkEntry); 678 putArchiveEntry(longLinkEntry); 679 write(encodedName.array(), encodedName.arrayOffset(), len); 680 write(0); // NUL terminator 681 closeArchiveEntry(); 682 } else if (longFileMode != LONGFILE_TRUNCATE) { 683 throw new RuntimeException(fieldName + " '" + name //NOSONAR 684 + "' is too long ( > " 685 + TarConstants.NAMELEN + " bytes)"); 686 } 687 } 688 return false; 689 } 690 691 private void transferModTime(final TarArchiveEntry from, final TarArchiveEntry to) { 692 Date fromModTime = from.getModTime(); 693 final long fromModTimeSeconds = fromModTime.getTime() / 1000; 694 if (fromModTimeSeconds < 0 || fromModTimeSeconds > TarConstants.MAXSIZE) { 695 fromModTime = new Date(0); 696 } 697 to.setModTime(fromModTime); 698 } 699}