Class TarArchiveOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class TarArchiveOutputStream
    extends ArchiveOutputStream
    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 stream using write().

    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 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 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.

    • Field Detail

      • LONGFILE_ERROR

        public static final int LONGFILE_ERROR
        Fail if a long file name is required in the archive.
        See Also:
        Constant Field Values
      • LONGFILE_TRUNCATE

        public static final int LONGFILE_TRUNCATE
        Long paths will be truncated in the archive.
        See Also:
        Constant Field Values
      • LONGFILE_GNU

        public static final int LONGFILE_GNU
        GNU tar extensions are used to store long file names in the archive.
        See Also:
        Constant Field Values
      • LONGFILE_POSIX

        public static final int LONGFILE_POSIX
        POSIX/PAX extensions are used to store long file names in the archive.
        See Also:
        Constant Field Values
      • BIGNUMBER_ERROR

        public static final int BIGNUMBER_ERROR
        Fail if a big number (e.g. size > 8GiB) is required in the archive.
        See Also:
        Constant Field Values
      • BIGNUMBER_STAR

        public static final int BIGNUMBER_STAR
        star/GNU tar/BSD tar extensions are used to store big number in the archive.
        See Also:
        Constant Field Values
      • BIGNUMBER_POSIX

        public static final int BIGNUMBER_POSIX
        POSIX/PAX extensions are used to store big numbers in the archive.
        See Also:
        Constant Field Values
      • currSize

        private long currSize
      • currName

        private java.lang.String currName
      • currBytes

        private long currBytes
      • recordBuf

        private final byte[] recordBuf
      • longFileMode

        private int longFileMode
      • bigNumberMode

        private int bigNumberMode
      • recordsWritten

        private int recordsWritten
      • recordsPerBlock

        private final int recordsPerBlock
      • closed

        private boolean closed
      • haveUnclosedEntry

        private boolean haveUnclosedEntry
        Indicates if putArchiveEntry has been called without closeArchiveEntry
      • finished

        private boolean finished
        indicates if this archive is finished
      • encoding

        final java.lang.String encoding
      • addPaxHeadersForNonAsciiNames

        private boolean addPaxHeadersForNonAsciiNames
      • BLOCK_SIZE_UNSPECIFIED

        private static final int BLOCK_SIZE_UNSPECIFIED
        See Also:
        Constant Field Values
    • Constructor Detail

      • TarArchiveOutputStream

        public TarArchiveOutputStream​(java.io.OutputStream os)
        Constructor for TarArchiveOutputStream.

        Uses a block size of 512 bytes.

        Parameters:
        os - the output stream to use
      • TarArchiveOutputStream

        public TarArchiveOutputStream​(java.io.OutputStream os,
                                      java.lang.String encoding)
        Constructor for TarArchiveOutputStream.

        Uses a block size of 512 bytes.

        Parameters:
        os - the output stream to use
        encoding - name of the encoding to use for file names
        Since:
        1.4
      • TarArchiveOutputStream

        public TarArchiveOutputStream​(java.io.OutputStream os,
                                      int blockSize)
        Constructor for TarArchiveOutputStream.
        Parameters:
        os - the output stream to use
        blockSize - the block size to use. Must be a multiple of 512 bytes.
      • TarArchiveOutputStream

        @Deprecated
        public TarArchiveOutputStream​(java.io.OutputStream os,
                                      int blockSize,
                                      int recordSize)
        Deprecated.
        recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used
        Constructor for TarArchiveOutputStream.
        Parameters:
        os - the output stream to use
        blockSize - the block size to use
        recordSize - the record size to use. Must be 512 bytes.
      • TarArchiveOutputStream

        @Deprecated
        public TarArchiveOutputStream​(java.io.OutputStream os,
                                      int blockSize,
                                      int recordSize,
                                      java.lang.String encoding)
        Deprecated.
        recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used.
        Constructor for TarArchiveOutputStream.
        Parameters:
        os - the output stream to use
        blockSize - the block size to use . Must be a multiple of 512 bytes.
        recordSize - the record size to use. Must be 512 bytes.
        encoding - name of the encoding to use for file names
        Since:
        1.4
      • TarArchiveOutputStream

        public TarArchiveOutputStream​(java.io.OutputStream os,
                                      int blockSize,
                                      java.lang.String encoding)
        Constructor for TarArchiveOutputStream.
        Parameters:
        os - the output stream to use
        blockSize - the block size to use. Must be a multiple of 512 bytes.
        encoding - name of the encoding to use for file names
        Since:
        1.4
    • Method Detail

      • setLongFileMode

        public void setLongFileMode​(int longFileMode)
        Set the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1) or LONGFILE_GNU(2). This specifies the treatment of long file names (names >= TarConstants.NAMELEN). Default is LONGFILE_ERROR.
        Parameters:
        longFileMode - the mode to use
      • setBigNumberMode

        public void setBigNumberMode​(int bigNumberMode)
        Set the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_POSIX(1) or BIGNUMBER_STAR(2). This specifies the treatment of big files (sizes > TarConstants.MAXSIZE) and other numeric values to big to fit into a traditional tar header. Default is BIGNUMBER_ERROR.
        Parameters:
        bigNumberMode - the mode to use
        Since:
        1.4
      • setAddPaxHeadersForNonAsciiNames

        public void setAddPaxHeadersForNonAsciiNames​(boolean b)
        Whether to add a PAX extension header for non-ASCII file names.
        Parameters:
        b - whether to add a PAX extension header for non-ASCII file names.
        Since:
        1.4
      • getCount

        @Deprecated
        public int getCount()
        Deprecated.
        Description copied from class: ArchiveOutputStream
        Returns the current number of bytes written to this stream.
        Overrides:
        getCount in class ArchiveOutputStream
        Returns:
        the number of written bytes
      • finish

        public void finish()
                    throws java.io.IOException
        Ends the TAR archive without closing the underlying OutputStream. 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 two EOF records, like some other implementations.
        Specified by:
        finish in class ArchiveOutputStream
        Throws:
        java.io.IOException - on error
      • close

        public void close()
                   throws java.io.IOException
        Closes the underlying OutputStream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException - on error
      • getRecordSize

        @Deprecated
        public int getRecordSize()
        Deprecated.
        Get the record size being used by this stream's TarBuffer.
        Returns:
        The TarBuffer record size.
      • putArchiveEntry

        public void putArchiveEntry​(ArchiveEntry archiveEntry)
                             throws java.io.IOException
        Put 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 this method is called, the stream is ready for calls to write() to write the entry's contents. Once the contents are written, closeArchiveEntry() MUST be called to ensure that all buffered data is completely written to the output stream.
        Specified by:
        putArchiveEntry in class ArchiveOutputStream
        Parameters:
        archiveEntry - The TarEntry to be written to the archive.
        Throws:
        java.io.IOException - on error
        java.lang.ClassCastException - if archiveEntry is not an instance of TarArchiveEntry
      • closeArchiveEntry

        public void closeArchiveEntry()
                               throws java.io.IOException
        Close 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 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 this entry is closed and the next entry written.
        Specified by:
        closeArchiveEntry in class ArchiveOutputStream
        Throws:
        java.io.IOException - on error
      • write

        public void write​(byte[] wBuf,
                          int wOffset,
                          int numToWrite)
                   throws java.io.IOException
        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 the length specified for the current entry.
        Overrides:
        write in class java.io.OutputStream
        Parameters:
        wBuf - The buffer to write to the archive.
        wOffset - The offset in the buffer from which to get bytes.
        numToWrite - The number of bytes to write.
        Throws:
        java.io.IOException - on error
      • writePaxHeaders

        void writePaxHeaders​(TarArchiveEntry entry,
                             java.lang.String entryName,
                             java.util.Map<java.lang.String,​java.lang.String> headers)
                      throws java.io.IOException
        Writes a PAX extended header with the given map as contents.
        Throws:
        java.io.IOException
        Since:
        1.4
      • encodeExtendedPaxHeadersContents

        private byte[] encodeExtendedPaxHeadersContents​(java.util.Map<java.lang.String,​java.lang.String> headers)
                                                 throws java.io.UnsupportedEncodingException
        Throws:
        java.io.UnsupportedEncodingException
      • stripTo7Bits

        private java.lang.String stripTo7Bits​(java.lang.String name)
      • shouldBeReplaced

        private boolean shouldBeReplaced​(char c)
        Returns:
        true if the character could lead to problems when used inside a TarArchiveEntry name for a PAX header.
      • writeEOFRecord

        private void writeEOFRecord()
                             throws java.io.IOException
        Write an EOF (end of archive) record to the tar archive. An EOF record consists of a record of all zeros.
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • createArchiveEntry

        public ArchiveEntry createArchiveEntry​(java.io.File inputFile,
                                               java.lang.String entryName)
                                        throws java.io.IOException
        Description copied from class: ArchiveOutputStream
        Create an archive entry using the inputFile and entryName provided.
        Specified by:
        createArchiveEntry in class ArchiveOutputStream
        Parameters:
        inputFile - the file to create the entry from
        entryName - name to use for the entry
        Returns:
        the ArchiveEntry set up with details from the file
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeRecord

        private void writeRecord​(byte[] record)
                          throws java.io.IOException
        Write an archive record to the archive.
        Parameters:
        record - The record data to write to the archive.
        Throws:
        java.io.IOException - on error
      • padAsNeeded

        private void padAsNeeded()
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • addPaxHeadersForBigNumbers

        private void addPaxHeadersForBigNumbers​(java.util.Map<java.lang.String,​java.lang.String> paxHeaders,
                                                TarArchiveEntry entry)
      • addPaxHeaderForBigNumber

        private void addPaxHeaderForBigNumber​(java.util.Map<java.lang.String,​java.lang.String> paxHeaders,
                                              java.lang.String header,
                                              long value,
                                              long maxValue)
      • failForBigNumbers

        private void failForBigNumbers​(TarArchiveEntry entry)
      • failForBigNumber

        private void failForBigNumber​(java.lang.String field,
                                      long value,
                                      long maxValue)
      • failForBigNumberWithPosixMessage

        private void failForBigNumberWithPosixMessage​(java.lang.String field,
                                                      long value,
                                                      long maxValue)
      • failForBigNumber

        private void failForBigNumber​(java.lang.String field,
                                      long value,
                                      long maxValue,
                                      java.lang.String additionalMsg)
      • handleLongName

        private boolean handleLongName​(TarArchiveEntry entry,
                                       java.lang.String name,
                                       java.util.Map<java.lang.String,​java.lang.String> paxHeaders,
                                       java.lang.String paxHeaderName,
                                       byte linkType,
                                       java.lang.String fieldName)
                                throws java.io.IOException
        Handles long file or link names according to the longFileMode setting.

        I.e. if the given name is too long to be written to a plain tar header then

        • it creates a pax header who's name is given by the paxHeaderName parameter if longFileMode is POSIX
        • it creates a GNU longlink entry who's type is given by the linkType parameter if longFileMode is GNU
        • it throws an exception if longFileMode is ERROR
        • it truncates the name if longFileMode is TRUNCATE

        Parameters:
        entry - entry the name belongs to
        name - the name to write
        paxHeaders - current map of pax headers
        paxHeaderName - name of the pax header to write
        linkType - type of the GNU entry to write
        fieldName - the name of the field
        Returns:
        whether a pax header has been written.
        Throws:
        java.io.IOException