TarArchiveOutputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.archivers.tar;

  20. import static java.nio.charset.StandardCharsets.UTF_8;

  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.io.StringWriter;
  25. import java.math.BigDecimal;
  26. import java.math.RoundingMode;
  27. import java.nio.ByteBuffer;
  28. import java.nio.charset.StandardCharsets;
  29. import java.nio.file.LinkOption;
  30. import java.nio.file.Path;
  31. import java.nio.file.attribute.FileTime;
  32. import java.time.Instant;
  33. import java.util.HashMap;
  34. import java.util.Map;

  35. import org.apache.commons.compress.archivers.ArchiveOutputStream;
  36. import org.apache.commons.compress.archivers.zip.ZipEncoding;
  37. import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
  38. import org.apache.commons.compress.utils.FixedLengthBlockOutputStream;
  39. import org.apache.commons.compress.utils.TimeUtils;
  40. import org.apache.commons.io.Charsets;
  41. import org.apache.commons.io.file.attribute.FileTimes;
  42. import org.apache.commons.io.output.CountingOutputStream;
  43. import org.apache.commons.lang3.ArrayFill;

  44. /**
  45.  * The TarOutputStream writes a UNIX tar archive as an OutputStream. Methods are provided to put entries, and then write their contents by writing to this
  46.  * stream using write().
  47.  *
  48.  * <p>
  49.  * tar archives consist of a sequence of records of 512 bytes each that are grouped into blocks. Prior to Apache Commons Compress 1.14 it has been possible to
  50.  * configure a record size different from 512 bytes and arbitrary block sizes. Starting with Compress 1.15 512 is the only valid option for the record size and
  51.  * the block size must be a multiple of 512. Also the default block size changed from 10240 bytes prior to Compress 1.15 to 512 bytes with Compress 1.15.
  52.  * </p>
  53.  *
  54.  * @NotThreadSafe
  55.  */
  56. public class TarArchiveOutputStream extends ArchiveOutputStream<TarArchiveEntry> {

  57.     /**
  58.      * Fail if a long file name is required in the archive.
  59.      */
  60.     public static final int LONGFILE_ERROR = 0;

  61.     /**
  62.      * Long paths will be truncated in the archive.
  63.      */
  64.     public static final int LONGFILE_TRUNCATE = 1;

  65.     /**
  66.      * GNU tar extensions are used to store long file names in the archive.
  67.      */
  68.     public static final int LONGFILE_GNU = 2;

  69.     /**
  70.      * POSIX/PAX extensions are used to store long file names in the archive.
  71.      */
  72.     public static final int LONGFILE_POSIX = 3;

  73.     /**
  74.      * Fail if a big number (e.g. size &gt; 8GiB) is required in the archive.
  75.      */
  76.     public static final int BIGNUMBER_ERROR = 0;

  77.     /**
  78.      * star/GNU tar/BSD tar extensions are used to store big number in the archive.
  79.      */
  80.     public static final int BIGNUMBER_STAR = 1;

  81.     /**
  82.      * POSIX/PAX extensions are used to store big numbers in the archive.
  83.      */
  84.     public static final int BIGNUMBER_POSIX = 2;
  85.     private static final int RECORD_SIZE = 512;

  86.     private static final ZipEncoding ASCII = ZipEncodingHelper.getZipEncoding(StandardCharsets.US_ASCII);

  87.     private static final int BLOCK_SIZE_UNSPECIFIED = -511;
  88.     private long currSize;
  89.     private String currName;
  90.     private long currBytes;
  91.     private final byte[] recordBuf;
  92.     private int longFileMode = LONGFILE_ERROR;
  93.     private int bigNumberMode = BIGNUMBER_ERROR;

  94.     private long recordsWritten;

  95.     private final int recordsPerBlock;

  96.     /**
  97.      * Indicates if putArchiveEntry has been called without closeArchiveEntry
  98.      */
  99.     private boolean haveUnclosedEntry;

  100.     private final CountingOutputStream countingOut;

  101.     private final ZipEncoding zipEncoding;

  102.     /**
  103.      * The provided encoding (for unit tests).
  104.      */
  105.     final String charsetName;

  106.     private boolean addPaxHeadersForNonAsciiNames;

  107.     /**
  108.      * Constructs a new instance.
  109.      *
  110.      * <p>
  111.      * Uses a block size of 512 bytes.
  112.      * </p>
  113.      *
  114.      * @param os the output stream to use
  115.      */
  116.     public TarArchiveOutputStream(final OutputStream os) {
  117.         this(os, BLOCK_SIZE_UNSPECIFIED);
  118.     }

  119.     /**
  120.      * Constructs a new instance.
  121.      *
  122.      * @param os        the output stream to use
  123.      * @param blockSize the block size to use. Must be a multiple of 512 bytes.
  124.      */
  125.     public TarArchiveOutputStream(final OutputStream os, final int blockSize) {
  126.         this(os, blockSize, null);
  127.     }

  128.     /**
  129.      * Constructs a new instance.
  130.      *
  131.      * @param os         the output stream to use
  132.      * @param blockSize  the block size to use
  133.      * @param recordSize the record size to use. Must be 512 bytes.
  134.      * @deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used
  135.      */
  136.     @Deprecated
  137.     public TarArchiveOutputStream(final OutputStream os, final int blockSize, final int recordSize) {
  138.         this(os, blockSize, recordSize, null);
  139.     }

  140.     /**
  141.      * Constructs a new instance.
  142.      *
  143.      * @param os         the output stream to use
  144.      * @param blockSize  the block size to use . Must be a multiple of 512 bytes.
  145.      * @param recordSize the record size to use. Must be 512 bytes.
  146.      * @param encoding   name of the encoding to use for file names
  147.      * @since 1.4
  148.      * @deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used.
  149.      */
  150.     @Deprecated
  151.     public TarArchiveOutputStream(final OutputStream os, final int blockSize, final int recordSize, final String encoding) {
  152.         this(os, blockSize, encoding);
  153.         if (recordSize != RECORD_SIZE) {
  154.             throw new IllegalArgumentException("Tar record size must always be 512 bytes. Attempt to set size of " + recordSize);
  155.         }

  156.     }

  157.     /**
  158.      * Constructs a new instance.
  159.      *
  160.      * @param os        the output stream to use
  161.      * @param blockSize the block size to use. Must be a multiple of 512 bytes.
  162.      * @param encoding  name of the encoding to use for file names
  163.      * @since 1.4
  164.      */
  165.     public TarArchiveOutputStream(final OutputStream os, final int blockSize, final String encoding) {
  166.         super(os);
  167.         final int realBlockSize;
  168.         if (BLOCK_SIZE_UNSPECIFIED == blockSize) {
  169.             realBlockSize = RECORD_SIZE;
  170.         } else {
  171.             realBlockSize = blockSize;
  172.         }

  173.         if (realBlockSize <= 0 || realBlockSize % RECORD_SIZE != 0) {
  174.             throw new IllegalArgumentException("Block size must be a multiple of 512 bytes. Attempt to use set size of " + blockSize);
  175.         }
  176.         this.out = new FixedLengthBlockOutputStream(countingOut = new CountingOutputStream(os), RECORD_SIZE);
  177.         this.charsetName = Charsets.toCharset(encoding).name();
  178.         this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);

  179.         this.recordBuf = new byte[RECORD_SIZE];
  180.         this.recordsPerBlock = realBlockSize / RECORD_SIZE;
  181.     }

  182.     /**
  183.      * Constructs a new instance.
  184.      *
  185.      * <p>
  186.      * Uses a block size of 512 bytes.
  187.      * </p>
  188.      *
  189.      * @param os       the output stream to use
  190.      * @param encoding name of the encoding to use for file names
  191.      * @since 1.4
  192.      */
  193.     public TarArchiveOutputStream(final OutputStream os, final String encoding) {
  194.         this(os, BLOCK_SIZE_UNSPECIFIED, encoding);
  195.     }

  196.     private void addFileTimePaxHeader(final Map<String, String> paxHeaders, final String header, final FileTime value) {
  197.         if (value != null) {
  198.             final Instant instant = value.toInstant();
  199.             final long seconds = instant.getEpochSecond();
  200.             final int nanos = instant.getNano();
  201.             if (nanos == 0) {
  202.                 paxHeaders.put(header, String.valueOf(seconds));
  203.             } else {
  204.                 addInstantPaxHeader(paxHeaders, header, seconds, nanos);
  205.             }
  206.         }
  207.     }

  208.     private void addFileTimePaxHeaderForBigNumber(final Map<String, String> paxHeaders, final String header, final FileTime value, final long maxValue) {
  209.         if (value != null) {
  210.             final Instant instant = value.toInstant();
  211.             final long seconds = instant.getEpochSecond();
  212.             final int nanos = instant.getNano();
  213.             if (nanos == 0) {
  214.                 addPaxHeaderForBigNumber(paxHeaders, header, seconds, maxValue);
  215.             } else {
  216.                 addInstantPaxHeader(paxHeaders, header, seconds, nanos);
  217.             }
  218.         }
  219.     }

  220.     private void addInstantPaxHeader(final Map<String, String> paxHeaders, final String header, final long seconds, final int nanos) {
  221.         final BigDecimal bdSeconds = BigDecimal.valueOf(seconds);
  222.         final BigDecimal bdNanos = BigDecimal.valueOf(nanos).movePointLeft(9).setScale(7, RoundingMode.DOWN);
  223.         final BigDecimal timestamp = bdSeconds.add(bdNanos);
  224.         paxHeaders.put(header, timestamp.toPlainString());
  225.     }

  226.     private void addPaxHeaderForBigNumber(final Map<String, String> paxHeaders, final String header, final long value, final long maxValue) {
  227.         if (value < 0 || value > maxValue) {
  228.             paxHeaders.put(header, String.valueOf(value));
  229.         }
  230.     }

  231.     private void addPaxHeadersForBigNumbers(final Map<String, String> paxHeaders, final TarArchiveEntry entry) {
  232.         addPaxHeaderForBigNumber(paxHeaders, "size", entry.getSize(), TarConstants.MAXSIZE);
  233.         addPaxHeaderForBigNumber(paxHeaders, "gid", entry.getLongGroupId(), TarConstants.MAXID);
  234.         addFileTimePaxHeaderForBigNumber(paxHeaders, "mtime", entry.getLastModifiedTime(), TarConstants.MAXSIZE);
  235.         addFileTimePaxHeader(paxHeaders, "atime", entry.getLastAccessTime());
  236.         if (entry.getStatusChangeTime() != null) {
  237.             addFileTimePaxHeader(paxHeaders, "ctime", entry.getStatusChangeTime());
  238.         } else {
  239.             // ctime is usually set from creation time on platforms where the real ctime is not available
  240.             addFileTimePaxHeader(paxHeaders, "ctime", entry.getCreationTime());
  241.         }
  242.         addPaxHeaderForBigNumber(paxHeaders, "uid", entry.getLongUserId(), TarConstants.MAXID);
  243.         // libarchive extensions
  244.         addFileTimePaxHeader(paxHeaders, "LIBARCHIVE.creationtime", entry.getCreationTime());
  245.         // star extensions by J�rg Schilling
  246.         addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devmajor", entry.getDevMajor(), TarConstants.MAXID);
  247.         addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devminor", entry.getDevMinor(), TarConstants.MAXID);
  248.         // there is no PAX header for file mode
  249.         failForBigNumber("mode", entry.getMode(), TarConstants.MAXID);
  250.     }

  251.     /**
  252.      * Closes the underlying OutputStream.
  253.      *
  254.      * @throws IOException on error
  255.      */
  256.     @Override
  257.     public void close() throws IOException {
  258.         try {
  259.             if (!isFinished()) {
  260.                 finish();
  261.             }
  262.         } finally {
  263.             if (!isClosed()) {
  264.                 super.close();
  265.             }
  266.         }
  267.     }

  268.     /**
  269.      * Closes an entry. This method MUST be called for all file entries that contain data. The reason is that we must buffer data written to the stream in order
  270.      * to satisfy the buffer's record based writes. Thus, there may be data fragments still being assembled that must be written to the output stream before
  271.      * this entry is closed and the next entry written.
  272.      *
  273.      * @throws IOException on error
  274.      */
  275.     @Override
  276.     public void closeArchiveEntry() throws IOException {
  277.         checkFinished();
  278.         if (!haveUnclosedEntry) {
  279.             throw new IOException("No current entry to close");
  280.         }
  281.         ((FixedLengthBlockOutputStream) out).flushBlock();
  282.         if (currBytes < currSize) {
  283.             throw new IOException(
  284.                     "Entry '" + currName + "' closed at '" + currBytes + "' before the '" + currSize + "' bytes specified in the header were written");
  285.         }
  286.         recordsWritten += currSize / RECORD_SIZE;

  287.         if (0 != currSize % RECORD_SIZE) {
  288.             recordsWritten++;
  289.         }
  290.         haveUnclosedEntry = false;
  291.     }

  292.     @Override
  293.     public TarArchiveEntry createArchiveEntry(final File inputFile, final String entryName) throws IOException {
  294.         checkFinished();
  295.         return new TarArchiveEntry(inputFile, entryName);
  296.     }

  297.     @Override
  298.     public TarArchiveEntry createArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException {
  299.         checkFinished();
  300.         return new TarArchiveEntry(inputPath, entryName, options);
  301.     }

  302.     private byte[] encodeExtendedPaxHeadersContents(final Map<String, String> headers) {
  303.         final StringWriter w = new StringWriter();
  304.         headers.forEach((k, v) -> {
  305.             int len = k.length() + v.length() + 3 /* blank, equals and newline */
  306.                     + 2 /* guess 9 < actual length < 100 */;
  307.             String line = len + " " + k + "=" + v + "\n";
  308.             int actualLength = line.getBytes(UTF_8).length;
  309.             while (len != actualLength) {
  310.                 // Adjust for cases where length < 10 or > 100
  311.                 // or where UTF-8 encoding isn't a single octet
  312.                 // per character.
  313.                 // Must be in loop as size may go from 99 to 100 in
  314.                 // first pass, so we'd need a second.
  315.                 len = actualLength;
  316.                 line = len + " " + k + "=" + v + "\n";
  317.                 actualLength = line.getBytes(UTF_8).length;
  318.             }
  319.             w.write(line);
  320.         });
  321.         return w.toString().getBytes(UTF_8);
  322.     }

  323.     private void failForBigNumber(final String field, final long value, final long maxValue) {
  324.         failForBigNumber(field, value, maxValue, "");
  325.     }

  326.     private void failForBigNumber(final String field, final long value, final long maxValue, final String additionalMsg) {
  327.         if (value < 0 || value > maxValue) {
  328.             throw new IllegalArgumentException(field + " '" + value // NOSONAR
  329.                     + "' is too big ( > " + maxValue + " )." + additionalMsg);
  330.         }
  331.     }

  332.     private void failForBigNumbers(final TarArchiveEntry entry) {
  333.         failForBigNumber("entry size", entry.getSize(), TarConstants.MAXSIZE);
  334.         failForBigNumberWithPosixMessage("group id", entry.getLongGroupId(), TarConstants.MAXID);
  335.         failForBigNumber("last modification time", TimeUtils.toUnixTime(entry.getLastModifiedTime()), TarConstants.MAXSIZE);
  336.         failForBigNumber("user id", entry.getLongUserId(), TarConstants.MAXID);
  337.         failForBigNumber("mode", entry.getMode(), TarConstants.MAXID);
  338.         failForBigNumber("major device number", entry.getDevMajor(), TarConstants.MAXID);
  339.         failForBigNumber("minor device number", entry.getDevMinor(), TarConstants.MAXID);
  340.     }

  341.     private void failForBigNumberWithPosixMessage(final String field, final long value, final long maxValue) {
  342.         failForBigNumber(field, value, maxValue, " Use STAR or POSIX extensions to overcome this limit");
  343.     }

  344.     /**
  345.      * Finishes the TAR archive without closing the underlying OutputStream.
  346.      *
  347.      * An archive consists of a series of file entries terminated by an end-of-archive entry, which consists of two 512 blocks of zero bytes. POSIX.1 requires
  348.      * two EOF records, like some other implementations.
  349.      *
  350.      * @throws IOException on error
  351.      */
  352.     @Override
  353.     public void finish() throws IOException {
  354.         checkFinished();
  355.         if (haveUnclosedEntry) {
  356.             throw new IOException("This archive contains unclosed entries.");
  357.         }
  358.         writeEOFRecord();
  359.         writeEOFRecord();
  360.         padAsNeeded();
  361.         out.flush();
  362.         super.finish();
  363.     }

  364.     @Override
  365.     public long getBytesWritten() {
  366.         return countingOut.getByteCount();
  367.     }

  368.     @Deprecated
  369.     @Override
  370.     public int getCount() {
  371.         return (int) getBytesWritten();
  372.     }

  373.     /**
  374.      * Gets the record size being used by this stream's TarBuffer.
  375.      *
  376.      * @return The TarBuffer record size.
  377.      * @deprecated
  378.      */
  379.     @Deprecated
  380.     public int getRecordSize() {
  381.         return RECORD_SIZE;
  382.     }

  383.     /**
  384.      * Handles long file or link names according to the longFileMode setting.
  385.      *
  386.      * <p>
  387.      * I.e. if the given name is too long to be written to a plain tar header then
  388.      * <ul>
  389.      * <li>it creates a pax header who's name is given by the paxHeaderName parameter if longFileMode is POSIX</li>
  390.      * <li>it creates a GNU longlink entry who's type is given by the linkType parameter if longFileMode is GNU</li>
  391.      * <li>it throws an exception if longFileMode is ERROR</li>
  392.      * <li>it truncates the name if longFileMode is TRUNCATE</li>
  393.      * </ul>
  394.      * </p>
  395.      *
  396.      * @param entry         entry the name belongs to
  397.      * @param name          the name to write
  398.      * @param paxHeaders    current map of pax headers
  399.      * @param paxHeaderName name of the pax header to write
  400.      * @param linkType      type of the GNU entry to write
  401.      * @param fieldName     the name of the field
  402.      * @throws IllegalArgumentException if the {@link TarArchiveOutputStream#longFileMode} equals {@link TarArchiveOutputStream#LONGFILE_ERROR} and the file
  403.      *                                  name is too long
  404.      * @return whether a pax header has been written.
  405.      */
  406.     private boolean handleLongName(final TarArchiveEntry entry, final String name, final Map<String, String> paxHeaders, final String paxHeaderName,
  407.             final byte linkType, final String fieldName) throws IOException {
  408.         final ByteBuffer encodedName = zipEncoding.encode(name);
  409.         final int len = encodedName.limit() - encodedName.position();
  410.         if (len >= TarConstants.NAMELEN) {

  411.             if (longFileMode == LONGFILE_POSIX) {
  412.                 paxHeaders.put(paxHeaderName, name);
  413.                 return true;
  414.             }
  415.             if (longFileMode == LONGFILE_GNU) {
  416.                 // create a TarEntry for the LongLink, the contents
  417.                 // of which are the link's name
  418.                 final TarArchiveEntry longLinkEntry = new TarArchiveEntry(TarConstants.GNU_LONGLINK, linkType);

  419.                 longLinkEntry.setSize(len + 1L); // +1 for NUL
  420.                 transferModTime(entry, longLinkEntry);
  421.                 putArchiveEntry(longLinkEntry);
  422.                 write(encodedName.array(), encodedName.arrayOffset(), len);
  423.                 write(0); // NUL terminator
  424.                 closeArchiveEntry();
  425.             } else if (longFileMode != LONGFILE_TRUNCATE) {
  426.                 throw new IllegalArgumentException(fieldName + " '" + name // NOSONAR
  427.                         + "' is too long ( > " + TarConstants.NAMELEN + " bytes)");
  428.             }
  429.         }
  430.         return false;
  431.     }

  432.     private void padAsNeeded() throws IOException {
  433.         final int start = Math.toIntExact(recordsWritten % recordsPerBlock);
  434.         if (start != 0) {
  435.             for (int i = start; i < recordsPerBlock; i++) {
  436.                 writeEOFRecord();
  437.             }
  438.         }
  439.     }

  440.     /**
  441.      * Puts an entry on the output stream. This writes the entry's header record and positions the output stream for writing the contents of the entry. Once
  442.      * this method is called, the stream is ready for calls to write() to write the entry's contents. Once the contents are written, closeArchiveEntry()
  443.      * <B>MUST</B> be called to ensure that all buffered data is completely written to the output stream.
  444.      *
  445.      * @param archiveEntry The TarEntry to be written to the archive.
  446.      * @throws IOException              on error
  447.      * @throws ClassCastException       if archiveEntry is not an instance of TarArchiveEntry
  448.      * @throws IllegalArgumentException if the {@link TarArchiveOutputStream#longFileMode} equals {@link TarArchiveOutputStream#LONGFILE_ERROR} and the file
  449.      *                                  name is too long
  450.      * @throws IllegalArgumentException if the {@link TarArchiveOutputStream#bigNumberMode} equals {@link TarArchiveOutputStream#BIGNUMBER_ERROR} and one of the
  451.      *                                  numeric values exceeds the limits of a traditional tar header.
  452.      */
  453.     @Override
  454.     public void putArchiveEntry(final TarArchiveEntry archiveEntry) throws IOException {
  455.         checkFinished();
  456.         if (archiveEntry.isGlobalPaxHeader()) {
  457.             final byte[] data = encodeExtendedPaxHeadersContents(archiveEntry.getExtraPaxHeaders());
  458.             archiveEntry.setSize(data.length);
  459.             archiveEntry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR);
  460.             writeRecord(recordBuf);
  461.             currSize = archiveEntry.getSize();
  462.             currBytes = 0;
  463.             this.haveUnclosedEntry = true;
  464.             write(data);
  465.             closeArchiveEntry();
  466.         } else {
  467.             final Map<String, String> paxHeaders = new HashMap<>();
  468.             final String entryName = archiveEntry.getName();
  469.             final boolean paxHeaderContainsPath = handleLongName(archiveEntry, entryName, paxHeaders, "path", TarConstants.LF_GNUTYPE_LONGNAME, "file name");
  470.             final String linkName = archiveEntry.getLinkName();
  471.             final boolean paxHeaderContainsLinkPath = linkName != null && !linkName.isEmpty()
  472.                     && handleLongName(archiveEntry, linkName, paxHeaders, "linkpath", TarConstants.LF_GNUTYPE_LONGLINK, "link name");

  473.             if (bigNumberMode == BIGNUMBER_POSIX) {
  474.                 addPaxHeadersForBigNumbers(paxHeaders, archiveEntry);
  475.             } else if (bigNumberMode != BIGNUMBER_STAR) {
  476.                 failForBigNumbers(archiveEntry);
  477.             }

  478.             if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsPath && !ASCII.canEncode(entryName)) {
  479.                 paxHeaders.put("path", entryName);
  480.             }

  481.             if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsLinkPath && (archiveEntry.isLink() || archiveEntry.isSymbolicLink())
  482.                     && !ASCII.canEncode(linkName)) {
  483.                 paxHeaders.put("linkpath", linkName);
  484.             }
  485.             paxHeaders.putAll(archiveEntry.getExtraPaxHeaders());

  486.             if (!paxHeaders.isEmpty()) {
  487.                 writePaxHeaders(archiveEntry, entryName, paxHeaders);
  488.             }

  489.             archiveEntry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR);
  490.             writeRecord(recordBuf);

  491.             currBytes = 0;

  492.             if (archiveEntry.isDirectory()) {
  493.                 currSize = 0;
  494.             } else {
  495.                 currSize = archiveEntry.getSize();
  496.             }
  497.             currName = entryName;
  498.             haveUnclosedEntry = true;
  499.         }
  500.     }

  501.     /**
  502.      * Sets whether to add a PAX extension header for non-ASCII file names.
  503.      *
  504.      * @param b whether to add a PAX extension header for non-ASCII file names.
  505.      * @since 1.4
  506.      */
  507.     public void setAddPaxHeadersForNonAsciiNames(final boolean b) {
  508.         addPaxHeadersForNonAsciiNames = b;
  509.     }

  510.     /**
  511.      * Sets the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_STAR(1) or BIGNUMBER_POSIX(2). This specifies the treatment of big files (sizes &gt;
  512.      * TarConstants.MAXSIZE) and other numeric values too big to fit into a traditional tar header. Default is BIGNUMBER_ERROR.
  513.      *
  514.      * @param bigNumberMode the mode to use
  515.      * @since 1.4
  516.      */
  517.     public void setBigNumberMode(final int bigNumberMode) {
  518.         this.bigNumberMode = bigNumberMode;
  519.     }

  520.     /**
  521.      * Sets the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1), LONGFILE_GNU(2) or LONGFILE_POSIX(3). This specifies the treatment of long
  522.      * file names (names &gt;= TarConstants.NAMELEN). Default is LONGFILE_ERROR.
  523.      *
  524.      * @param longFileMode the mode to use
  525.      */
  526.     public void setLongFileMode(final int longFileMode) {
  527.         this.longFileMode = longFileMode;
  528.     }

  529.     /**
  530.      * Tests whether the character could lead to problems when used inside a TarArchiveEntry name for a PAX header.
  531.      *
  532.      * @return true if the character could lead to problems when used inside a TarArchiveEntry name for a PAX header.
  533.      */
  534.     private boolean shouldBeReplaced(final char c) {
  535.         return c == 0 // would be read as Trailing null
  536.                 || c == '/' // when used as last character TAE will consider the PAX header a directory
  537.                 || c == '\\'; // same as '/' as slashes get "normalized" on Windows
  538.     }

  539.     private String stripTo7Bits(final String name) {
  540.         final int length = name.length();
  541.         final StringBuilder result = new StringBuilder(length);
  542.         for (int i = 0; i < length; i++) {
  543.             final char stripped = (char) (name.charAt(i) & 0x7F);
  544.             if (shouldBeReplaced(stripped)) {
  545.                 result.append("_");
  546.             } else {
  547.                 result.append(stripped);
  548.             }
  549.         }
  550.         return result.toString();
  551.     }

  552.     private void transferModTime(final TarArchiveEntry from, final TarArchiveEntry to) {
  553.         long fromModTimeSeconds = TimeUtils.toUnixTime(from.getLastModifiedTime());
  554.         if (fromModTimeSeconds < 0 || fromModTimeSeconds > TarConstants.MAXSIZE) {
  555.             fromModTimeSeconds = 0;
  556.         }
  557.         to.setLastModifiedTime(FileTimes.fromUnixTime(fromModTimeSeconds));
  558.     }

  559.     /**
  560.      * Writes bytes to the current tar archive entry. This method is aware of the current entry and will throw an exception if you attempt to write bytes past
  561.      * the length specified for the current entry.
  562.      *
  563.      * @param wBuf       The buffer to write to the archive.
  564.      * @param wOffset    The offset in the buffer from which to get bytes.
  565.      * @param numToWrite The number of bytes to write.
  566.      * @throws IOException on error
  567.      */
  568.     @Override
  569.     public void write(final byte[] wBuf, final int wOffset, final int numToWrite) throws IOException {
  570.         if (!haveUnclosedEntry) {
  571.             throw new IllegalStateException("No current tar entry");
  572.         }
  573.         if (currBytes + numToWrite > currSize) {
  574.             throw new IOException(
  575.                     "Request to write '" + numToWrite + "' bytes exceeds size in header of '" + currSize + "' bytes for entry '" + currName + "'");
  576.         }
  577.         out.write(wBuf, wOffset, numToWrite);
  578.         currBytes += numToWrite;
  579.     }

  580.     /**
  581.      * Writes an EOF (end of archive) record to the tar archive. An EOF record consists of a record of all zeros.
  582.      */
  583.     private void writeEOFRecord() throws IOException {
  584.         writeRecord(ArrayFill.fill(recordBuf, (byte) 0));
  585.     }

  586.     /**
  587.      * Writes a PAX extended header with the given map as contents.
  588.      *
  589.      * @since 1.4
  590.      */
  591.     void writePaxHeaders(final TarArchiveEntry entry, final String entryName, final Map<String, String> headers) throws IOException {
  592.         String name = "./PaxHeaders.X/" + stripTo7Bits(entryName);
  593.         if (name.length() >= TarConstants.NAMELEN) {
  594.             name = name.substring(0, TarConstants.NAMELEN - 1);
  595.         }
  596.         final TarArchiveEntry pex = new TarArchiveEntry(name, TarConstants.LF_PAX_EXTENDED_HEADER_LC);
  597.         transferModTime(entry, pex);

  598.         final byte[] data = encodeExtendedPaxHeadersContents(headers);
  599.         pex.setSize(data.length);
  600.         putArchiveEntry(pex);
  601.         write(data);
  602.         closeArchiveEntry();
  603.     }

  604.     /**
  605.      * Writes an archive record to the archive.
  606.      *
  607.      * @param record The record data to write to the archive.
  608.      * @throws IOException on error
  609.      */
  610.     private void writeRecord(final byte[] record) throws IOException {
  611.         if (record.length != RECORD_SIZE) {
  612.             throw new IOException("Record to write has length '" + record.length + "' which is not the record size of '" + RECORD_SIZE + "'");
  613.         }

  614.         out.write(record);
  615.         recordsWritten++;
  616.     }
  617. }