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