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 if (!finished) { 306 finish(); 307 } 308 309 if (!closed) { 310 out.close(); 311 closed = true; 312 } 313 } 314 315 /** 316 * Get the record size being used by this stream's TarBuffer. 317 * 318 * @return The TarBuffer record size. 319 * @deprecated 320 */ 321 @Deprecated 322 public int getRecordSize() { 323 return RECORD_SIZE; 324 } 325 326 /** 327 * Put an entry on the output stream. This writes the entry's header record and positions the 328 * output stream for writing the contents of the entry. Once this method is called, the stream 329 * is ready for calls to write() to write the entry's contents. Once the contents are written, 330 * closeArchiveEntry() <B>MUST</B> be called to ensure that all buffered data is completely 331 * written to the output stream. 332 * 333 * @param archiveEntry The TarEntry to be written to the archive. 334 * @throws IOException on error 335 * @throws ClassCastException if archiveEntry is not an instance of TarArchiveEntry 336 */ 337 @Override 338 public void putArchiveEntry(final ArchiveEntry archiveEntry) throws IOException { 339 if (finished) { 340 throw new IOException("Stream has already been finished"); 341 } 342 final TarArchiveEntry entry = (TarArchiveEntry) archiveEntry; 343 if (entry.isGlobalPaxHeader()) { 344 final byte[] data = encodeExtendedPaxHeadersContents(entry.getExtraPaxHeaders()); 345 entry.setSize(data.length); 346 entry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR); 347 writeRecord(recordBuf); 348 currSize= entry.getSize(); 349 currBytes = 0; 350 this.haveUnclosedEntry = true; 351 write(data); 352 closeArchiveEntry(); 353 } else { 354 final Map<String, String> paxHeaders = new HashMap<>(); 355 final String entryName = entry.getName(); 356 final boolean paxHeaderContainsPath = handleLongName(entry, entryName, paxHeaders, "path", 357 TarConstants.LF_GNUTYPE_LONGNAME, "file name"); 358 359 final String linkName = entry.getLinkName(); 360 final boolean paxHeaderContainsLinkPath = linkName != null && linkName.length() > 0 361 && handleLongName(entry, linkName, paxHeaders, "linkpath", 362 TarConstants.LF_GNUTYPE_LONGLINK, "link name"); 363 364 if (bigNumberMode == BIGNUMBER_POSIX) { 365 addPaxHeadersForBigNumbers(paxHeaders, entry); 366 } else if (bigNumberMode != BIGNUMBER_STAR) { 367 failForBigNumbers(entry); 368 } 369 370 if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsPath 371 && !ASCII.canEncode(entryName)) { 372 paxHeaders.put("path", entryName); 373 } 374 375 if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsLinkPath 376 && (entry.isLink() || entry.isSymbolicLink()) 377 && !ASCII.canEncode(linkName)) { 378 paxHeaders.put("linkpath", linkName); 379 } 380 paxHeaders.putAll(entry.getExtraPaxHeaders()); 381 382 if (paxHeaders.size() > 0) { 383 writePaxHeaders(entry, entryName, paxHeaders); 384 } 385 386 entry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR); 387 writeRecord(recordBuf); 388 389 currBytes = 0; 390 391 if (entry.isDirectory()) { 392 currSize = 0; 393 } else { 394 currSize = entry.getSize(); 395 } 396 currName = entryName; 397 haveUnclosedEntry = true; 398 } 399 } 400 401 /** 402 * Close an entry. This method MUST be called for all file entries that contain data. The reason 403 * is that we must buffer data written to the stream in order to satisfy the buffer's record 404 * based writes. Thus, there may be data fragments still being assembled that must be written to 405 * the output stream before this entry is closed and the next entry written. 406 * 407 * @throws IOException on error 408 */ 409 @Override 410 public void closeArchiveEntry() throws IOException { 411 if (finished) { 412 throw new IOException("Stream has already been finished"); 413 } 414 if (!haveUnclosedEntry) { 415 throw new IOException("No current entry to close"); 416 } 417 out.flushBlock(); 418 if (currBytes < currSize) { 419 throw new IOException("entry '" + currName + "' closed at '" 420 + currBytes 421 + "' before the '" + currSize 422 + "' bytes specified in the header were written"); 423 } 424 recordsWritten += (currSize / RECORD_SIZE); 425 if (0 != currSize % RECORD_SIZE) { 426 recordsWritten++; 427 } 428 haveUnclosedEntry = false; 429 } 430 431 /** 432 * Writes bytes to the current tar archive entry. This method is aware of the current entry and 433 * will throw an exception if you attempt to write bytes past the length specified for the 434 * current entry. 435 * 436 * @param wBuf The buffer to write to the archive. 437 * @param wOffset The offset in the buffer from which to get bytes. 438 * @param numToWrite The number of bytes to write. 439 * @throws IOException on error 440 */ 441 @Override 442 public void write(final byte[] wBuf, int wOffset, int numToWrite) throws IOException { 443 if (!haveUnclosedEntry) { 444 throw new IllegalStateException("No current tar entry"); 445 } 446 if (currBytes + numToWrite > currSize) { 447 throw new IOException("request to write '" + numToWrite 448 + "' bytes exceeds size in header of '" 449 + currSize + "' bytes for entry '" 450 + currName + "'"); 451 } 452 out.write(wBuf, wOffset, numToWrite); 453 currBytes += numToWrite; 454 } 455 456 /** 457 * Writes a PAX extended header with the given map as contents. 458 * 459 * @since 1.4 460 */ 461 void writePaxHeaders(final TarArchiveEntry entry, 462 final String entryName, 463 final Map<String, String> headers) throws IOException { 464 String name = "./PaxHeaders.X/" + stripTo7Bits(entryName); 465 if (name.length() >= TarConstants.NAMELEN) { 466 name = name.substring(0, TarConstants.NAMELEN - 1); 467 } 468 final TarArchiveEntry pex = new TarArchiveEntry(name, 469 TarConstants.LF_PAX_EXTENDED_HEADER_LC); 470 transferModTime(entry, pex); 471 472 final byte[] data = encodeExtendedPaxHeadersContents(headers); 473 pex.setSize(data.length); 474 putArchiveEntry(pex); 475 write(data); 476 closeArchiveEntry(); 477 } 478 479 private byte[] encodeExtendedPaxHeadersContents(Map<String, String> headers) 480 throws UnsupportedEncodingException { 481 final StringWriter w = new StringWriter(); 482 for (final Map.Entry<String, String> h : headers.entrySet()) { 483 final String key = h.getKey(); 484 final String value = h.getValue(); 485 int len = key.length() + value.length() 486 + 3 /* blank, equals and newline */ 487 + 2 /* guess 9 < actual length < 100 */; 488 String line = len + " " + key + "=" + value + "\n"; 489 int actualLength = line.getBytes(CharsetNames.UTF_8).length; 490 while (len != actualLength) { 491 // Adjust for cases where length < 10 or > 100 492 // or where UTF-8 encoding isn't a single octet 493 // per character. 494 // Must be in loop as size may go from 99 to 100 in 495 // first pass so we'd need a second. 496 len = actualLength; 497 line = len + " " + key + "=" + value + "\n"; 498 actualLength = line.getBytes(CharsetNames.UTF_8).length; 499 } 500 w.write(line); 501 } 502 return w.toString().getBytes(CharsetNames.UTF_8); 503 } 504 505 private String stripTo7Bits(final String name) { 506 final int length = name.length(); 507 final StringBuilder result = new StringBuilder(length); 508 for (int i = 0; i < length; i++) { 509 final char stripped = (char) (name.charAt(i) & 0x7F); 510 if (shouldBeReplaced(stripped)) { 511 result.append("_"); 512 } else { 513 result.append(stripped); 514 } 515 } 516 return result.toString(); 517 } 518 519 /** 520 * @return true if the character could lead to problems when used inside a TarArchiveEntry name 521 * for a PAX header. 522 */ 523 private boolean shouldBeReplaced(final char c) { 524 return c == 0 // would be read as Trailing null 525 || c == '/' // when used as last character TAE will consider the PAX header a directory 526 || c == '\\'; // same as '/' as slashes get "normalized" on Windows 527 } 528 529 /** 530 * Write an EOF (end of archive) record to the tar archive. An EOF record consists of a record 531 * of all zeros. 532 */ 533 private void writeEOFRecord() throws IOException { 534 Arrays.fill(recordBuf, (byte) 0); 535 writeRecord(recordBuf); 536 } 537 538 @Override 539 public void flush() throws IOException { 540 out.flush(); 541 } 542 543 @Override 544 public ArchiveEntry createArchiveEntry(final File inputFile, final String entryName) 545 throws IOException { 546 if (finished) { 547 throw new IOException("Stream has already been finished"); 548 } 549 return new TarArchiveEntry(inputFile, entryName); 550 } 551 552 /** 553 * Write an archive record to the archive. 554 * 555 * @param record The record data to write to the archive. 556 * @throws IOException on error 557 */ 558 private void writeRecord(final byte[] record) throws IOException { 559 if (record.length != RECORD_SIZE) { 560 throw new IOException("record to write has length '" 561 + record.length 562 + "' which is not the record size of '" 563 + RECORD_SIZE + "'"); 564 } 565 566 out.write(record); 567 recordsWritten++; 568 } 569 570 private void padAsNeeded() throws IOException { 571 final int start = recordsWritten % recordsPerBlock; 572 if (start != 0) { 573 for (int i = start; i < recordsPerBlock; i++) { 574 writeEOFRecord(); 575 } 576 } 577 } 578 579 private void addPaxHeadersForBigNumbers(final Map<String, String> paxHeaders, 580 final TarArchiveEntry entry) { 581 addPaxHeaderForBigNumber(paxHeaders, "size", entry.getSize(), 582 TarConstants.MAXSIZE); 583 addPaxHeaderForBigNumber(paxHeaders, "gid", entry.getLongGroupId(), 584 TarConstants.MAXID); 585 addPaxHeaderForBigNumber(paxHeaders, "mtime", 586 entry.getModTime().getTime() / 1000, 587 TarConstants.MAXSIZE); 588 addPaxHeaderForBigNumber(paxHeaders, "uid", entry.getLongUserId(), 589 TarConstants.MAXID); 590 // star extensions by J\u00f6rg Schilling 591 addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devmajor", 592 entry.getDevMajor(), TarConstants.MAXID); 593 addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devminor", 594 entry.getDevMinor(), TarConstants.MAXID); 595 // there is no PAX header for file mode 596 failForBigNumber("mode", entry.getMode(), TarConstants.MAXID); 597 } 598 599 private void addPaxHeaderForBigNumber(final Map<String, String> paxHeaders, 600 final String header, final long value, 601 final long maxValue) { 602 if (value < 0 || value > maxValue) { 603 paxHeaders.put(header, String.valueOf(value)); 604 } 605 } 606 607 private void failForBigNumbers(final TarArchiveEntry entry) { 608 failForBigNumber("entry size", entry.getSize(), TarConstants.MAXSIZE); 609 failForBigNumberWithPosixMessage("group id", entry.getLongGroupId(), TarConstants.MAXID); 610 failForBigNumber("last modification time", 611 entry.getModTime().getTime() / 1000, 612 TarConstants.MAXSIZE); 613 failForBigNumber("user id", entry.getLongUserId(), TarConstants.MAXID); 614 failForBigNumber("mode", entry.getMode(), TarConstants.MAXID); 615 failForBigNumber("major device number", entry.getDevMajor(), 616 TarConstants.MAXID); 617 failForBigNumber("minor device number", entry.getDevMinor(), 618 TarConstants.MAXID); 619 } 620 621 private void failForBigNumber(final String field, final long value, final long maxValue) { 622 failForBigNumber(field, value, maxValue, ""); 623 } 624 625 private void failForBigNumberWithPosixMessage(final String field, final long value, 626 final long maxValue) { 627 failForBigNumber(field, value, maxValue, 628 " Use STAR or POSIX extensions to overcome this limit"); 629 } 630 631 private void failForBigNumber(final String field, final long value, final long maxValue, 632 final String additionalMsg) { 633 if (value < 0 || value > maxValue) { 634 throw new RuntimeException(field + " '" + value //NOSONAR 635 + "' is too big ( > " 636 + maxValue + " )." + additionalMsg); 637 } 638 } 639 640 /** 641 * Handles long file or link names according to the longFileMode setting. 642 * 643 * <p>I.e. if the given name is too long to be written to a plain tar header then <ul> <li>it 644 * creates a pax header who's name is given by the paxHeaderName parameter if longFileMode is 645 * POSIX</li> <li>it creates a GNU longlink entry who's type is given by the linkType parameter 646 * if longFileMode is GNU</li> <li>it throws an exception if longFileMode is ERROR</li> <li>it 647 * truncates the name if longFileMode is TRUNCATE</li> </ul></p> 648 * 649 * @param entry entry the name belongs to 650 * @param name the name to write 651 * @param paxHeaders current map of pax headers 652 * @param paxHeaderName name of the pax header to write 653 * @param linkType type of the GNU entry to write 654 * @param fieldName the name of the field 655 * @return whether a pax header has been written. 656 */ 657 private boolean handleLongName(final TarArchiveEntry entry, final String name, 658 final Map<String, String> paxHeaders, 659 final String paxHeaderName, final byte linkType, final String fieldName) 660 throws IOException { 661 final ByteBuffer encodedName = zipEncoding.encode(name); 662 final int len = encodedName.limit() - encodedName.position(); 663 if (len >= TarConstants.NAMELEN) { 664 665 if (longFileMode == LONGFILE_POSIX) { 666 paxHeaders.put(paxHeaderName, name); 667 return true; 668 } else if (longFileMode == LONGFILE_GNU) { 669 // create a TarEntry for the LongLink, the contents 670 // of which are the link's name 671 final TarArchiveEntry longLinkEntry = new TarArchiveEntry(TarConstants.GNU_LONGLINK, 672 linkType); 673 674 longLinkEntry.setSize(len + 1L); // +1 for NUL 675 transferModTime(entry, longLinkEntry); 676 putArchiveEntry(longLinkEntry); 677 write(encodedName.array(), encodedName.arrayOffset(), len); 678 write(0); // NUL terminator 679 closeArchiveEntry(); 680 } else if (longFileMode != LONGFILE_TRUNCATE) { 681 throw new RuntimeException(fieldName + " '" + name //NOSONAR 682 + "' is too long ( > " 683 + TarConstants.NAMELEN + " bytes)"); 684 } 685 } 686 return false; 687 } 688 689 private void transferModTime(final TarArchiveEntry from, final TarArchiveEntry to) { 690 Date fromModTime = from.getModTime(); 691 final long fromModTimeSeconds = fromModTime.getTime() / 1000; 692 if (fromModTimeSeconds < 0 || fromModTimeSeconds > TarConstants.MAXSIZE) { 693 fromModTime = new Date(0); 694 } 695 to.setModTime(fromModTime); 696 } 697}