Class TarArchiveInputStream

java.lang.Object
java.io.InputStream
org.apache.commons.compress.archivers.ArchiveInputStream
org.apache.commons.compress.archivers.tar.TarArchiveInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class TarArchiveInputStream extends ArchiveInputStream
The TarInputStream reads a UNIX tar archive as an InputStream. methods are provided to position at each successive entry in the archive, and the read each entry as a normal input stream using read().
  • Field Details

    • SMALL_BUFFER_SIZE

      private static final int SMALL_BUFFER_SIZE
      See Also:
    • smallBuf

      private final byte[] smallBuf
    • recordSize

      private final int recordSize
      The size the TAR header
    • recordBuffer

      private final byte[] recordBuffer
      The buffer to store the TAR header
    • blockSize

      private final int blockSize
      The size of a block
    • hasHitEOF

      private boolean hasHitEOF
      True if file has hit EOF
    • entrySize

      private long entrySize
      Size of the current entry
    • entryOffset

      private long entryOffset
      How far into the entry the stream is at
    • inputStream

      private final InputStream inputStream
      An input stream to read from
    • sparseInputStreams

      private List<InputStream> sparseInputStreams
      Input streams for reading sparse entries
    • currentSparseInputStreamIndex

      private int currentSparseInputStreamIndex
      the index of current input stream being read when reading sparse entries
    • currEntry

      private TarArchiveEntry currEntry
      The meta-data about the current entry
    • zipEncoding

      private final ZipEncoding zipEncoding
      The encoding of the file
    • encoding

      final String encoding
    • globalPaxHeaders

      private Map<String,String> globalPaxHeaders
    • globalSparseHeaders

      private final List<TarArchiveStructSparse> globalSparseHeaders
    • lenient

      private final boolean lenient
  • Constructor Details

    • TarArchiveInputStream

      public TarArchiveInputStream(InputStream is)
      Constructor for TarInputStream.
      Parameters:
      is - the input stream to use
    • TarArchiveInputStream

      public TarArchiveInputStream(InputStream is, boolean lenient)
      Constructor for TarInputStream.
      Parameters:
      is - the input stream to use
      lenient - when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to TarArchiveEntry.UNKNOWN. When set to false such illegal fields cause an exception instead.
      Since:
      1.19
    • TarArchiveInputStream

      public TarArchiveInputStream(InputStream is, String encoding)
      Constructor for TarInputStream.
      Parameters:
      is - the input stream to use
      encoding - name of the encoding to use for file names
      Since:
      1.4
    • TarArchiveInputStream

      public TarArchiveInputStream(InputStream is, int blockSize)
      Constructor for TarInputStream.
      Parameters:
      is - the input stream to use
      blockSize - the block size to use
    • TarArchiveInputStream

      public TarArchiveInputStream(InputStream is, int blockSize, String encoding)
      Constructor for TarInputStream.
      Parameters:
      is - the input stream to use
      blockSize - the block size to use
      encoding - name of the encoding to use for file names
      Since:
      1.4
    • TarArchiveInputStream

      public TarArchiveInputStream(InputStream is, int blockSize, int recordSize)
      Constructor for TarInputStream.
      Parameters:
      is - the input stream to use
      blockSize - the block size to use
      recordSize - the record size to use
    • TarArchiveInputStream

      public TarArchiveInputStream(InputStream is, int blockSize, int recordSize, String encoding)
      Constructor for TarInputStream.
      Parameters:
      is - the input stream to use
      blockSize - the block size to use
      recordSize - the record size to use
      encoding - name of the encoding to use for file names
      Since:
      1.4
    • TarArchiveInputStream

      public TarArchiveInputStream(InputStream is, int blockSize, int recordSize, String encoding, boolean lenient)
      Constructor for TarInputStream.
      Parameters:
      is - the input stream to use
      blockSize - the block size to use
      recordSize - the record size to use
      encoding - name of the encoding to use for file names
      lenient - when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to TarArchiveEntry.UNKNOWN. When set to false such illegal fields cause an exception instead.
      Since:
      1.19
  • Method Details

    • close

      public void close() throws IOException
      Closes this stream. Calls the TarBuffer's close() method.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - on error
    • getRecordSize

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

      public int available() throws IOException
      Get the available data that can be read from the current entry in the archive. This does not indicate how much data is left in the entire archive, only in the current entry. This value is determined from the entry's size header field and the amount of data already read from the current entry. Integer.MAX_VALUE is returned in case more than Integer.MAX_VALUE bytes are left in the current entry in the archive.
      Overrides:
      available in class InputStream
      Returns:
      The number of available bytes for the current entry.
      Throws:
      IOException - for signature
    • skip

      public long skip(long n) throws IOException
      Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file or end of entry before n bytes have been skipped; are only two possibilities. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.
      Overrides:
      skip in class InputStream
      Parameters:
      n - the number of bytes to be skipped.
      Returns:
      the actual number of bytes skipped.
      Throws:
      IOException - if a truncated tar archive is detected or some other I/O error occurs
    • skipSparse

      private long skipSparse(long n) throws IOException
      Skip n bytes from current input stream, if the current input stream doesn't have enough data to skip, jump to the next input stream and skip the rest bytes, keep doing this until total n bytes are skipped or the input streams are all skipped
      Parameters:
      n - bytes of data to skip
      Returns:
      actual bytes of data skipped
      Throws:
      IOException
    • markSupported

      public boolean markSupported()
      Since we do not support marking just yet, we return false.
      Overrides:
      markSupported in class InputStream
      Returns:
      False.
    • mark

      public void mark(int markLimit)
      Since we do not support marking just yet, we do nothing.
      Overrides:
      mark in class InputStream
      Parameters:
      markLimit - The limit to mark.
    • reset

      public void reset()
      Since we do not support marking just yet, we do nothing.
      Overrides:
      reset in class InputStream
    • getNextTarEntry

      public TarArchiveEntry getNextTarEntry() throws IOException
      Get the next entry in this tar archive. This will skip over any remaining data in the current entry, if there is one, and place the input stream at the header of the next entry, and read the header and instantiate a new TarEntry from the header bytes and return that entry. If there are no more entries in the archive, null will be returned to indicate that the end of the archive has been reached.
      Returns:
      The next TarEntry in the archive, or null.
      Throws:
      IOException - on error
    • skipRecordPadding

      private void skipRecordPadding() throws IOException
      The last record block should be written at the full size, so skip any additional space used to fill a record after an entry.
      Throws:
      IOException - if a truncated tar archive is detected
    • getActuallySkipped

      private long getActuallySkipped(long available, long skipped, long expected) throws IOException
      For FileInputStream, the skip always return the number you input, so we need the available bytes to determine how many bytes are actually skipped
      Parameters:
      available - available bytes returned by inputStream.available()
      skipped - skipped bytes returned by inputStream.skip()
      expected - bytes expected to skip
      Returns:
      number of bytes actually skipped
      Throws:
      IOException - if a truncated tar archive is detected
    • getLongNameData

      protected byte[] getLongNameData() throws IOException
      Get the next entry in this tar archive as longname data.
      Returns:
      The next entry in the archive as longname data, or null.
      Throws:
      IOException - on error
    • getRecord

      private byte[] getRecord() throws IOException
      Get the next record in this tar archive. This will skip over any remaining data in the current entry, if there is one, and place the input stream at the header of the next entry.

      If there are no more entries in the archive, null will be returned to indicate that the end of the archive has been reached. At the same time the hasHitEOF marker will be set to true.

      Returns:
      The next header in the archive, or null.
      Throws:
      IOException - on error
    • isEOFRecord

      protected boolean isEOFRecord(byte[] record)
      Determine if an archive record indicate End of Archive. End of archive is indicated by a record that consists entirely of null bytes.
      Parameters:
      record - The record data to check.
      Returns:
      true if the record data is an End of Archive
    • readRecord

      protected byte[] readRecord() throws IOException
      Read a record from the input stream and return the data.
      Returns:
      The record data or null if EOF has been hit.
      Throws:
      IOException - on error
    • readGlobalPaxHeaders

      private void readGlobalPaxHeaders() throws IOException
      Throws:
      IOException
    • paxHeaders

      private void paxHeaders() throws IOException
      For PAX Format 0.0, the sparse headers(GNU.sparse.offset and GNU.sparse.numbytes) may appear multi times, and they look like: GNU.sparse.size=size GNU.sparse.numblocks=numblocks repeat numblocks times GNU.sparse.offset=offset GNU.sparse.numbytes=numbytes end repeat For PAX Format 0.1, the sparse headers are stored in a single variable : GNU.sparse.map GNU.sparse.map Map of non-null data chunks. It is a string consisting of comma-separated values "offset,size[,offset-1,size-1...]" For PAX Format 1.X: The sparse map itself is stored in the file data block, preceding the actual file data. It consists of a series of decimal numbers delimited by newlines. The map is padded with nulls to the nearest block boundary. The first number gives the number of entries in the map. Following are map entries, each one consisting of two numbers giving the offset and size of the data block it describes.
      Throws:
      IOException
    • applyPaxHeadersToCurrentEntry

      private void applyPaxHeadersToCurrentEntry(Map<String,String> headers, List<TarArchiveStructSparse> sparseHeaders) throws IOException
      Throws:
      IOException
    • readOldGNUSparse

      private void readOldGNUSparse() throws IOException
      Adds the sparse chunks from the current entry to the sparse chunks, including any additional sparse entries following the current entry.
      Throws:
      IOException - on error
    • isDirectory

      private boolean isDirectory()
    • getNextEntry

      public ArchiveEntry getNextEntry() throws IOException
      Returns the next Archive Entry in this Stream.
      Specified by:
      getNextEntry in class ArchiveInputStream
      Returns:
      the next entry, or null if there are no more entries
      Throws:
      IOException - if the next entry could not be read
    • tryToConsumeSecondEOFRecord

      private void tryToConsumeSecondEOFRecord() throws IOException
      Tries to read the next record rewinding the stream if it is not a EOF record.

      This is meant to protect against cases where a tar implementation has written only one EOF record when two are expected. Actually this won't help since a non-conforming implementation likely won't fill full blocks consisting of - by default - ten records either so we probably have already read beyond the archive anyway.

      Throws:
      IOException
    • read

      public int read(byte[] buf, int offset, int numToRead) throws IOException
      Reads bytes from the current tar archive entry. This method is aware of the boundaries of the current entry in the archive and will deal with them as if they were this stream's start and EOF.
      Overrides:
      read in class InputStream
      Parameters:
      buf - The buffer into which to place bytes read.
      offset - The offset at which to place bytes read.
      numToRead - The number of bytes to read.
      Returns:
      The number of bytes read, or -1 at EOF.
      Throws:
      IOException - on error
    • readSparse

      private int readSparse(byte[] buf, int offset, int numToRead) throws IOException
      For sparse tar entries, there are many "holes"(consisting of all 0) in the file. Only the non-zero data is stored in tar files, and they are stored separately. The structure of non-zero data is introduced by the sparse headers using the offset, where a block of non-zero data starts, and numbytes, the length of the non-zero data block. When reading sparse entries, the actual data is read out with "holes" and non-zero data combined together according to the sparse headers.
      Parameters:
      buf - The buffer into which to place bytes read.
      offset - The offset at which to place bytes read.
      numToRead - The number of bytes to read.
      Returns:
      The number of bytes read, or -1 at EOF.
      Throws:
      IOException - on error
    • canReadEntryData

      public boolean canReadEntryData(ArchiveEntry ae)
      Whether this class is able to read the given entry.
      Overrides:
      canReadEntryData in class ArchiveInputStream
      Parameters:
      ae - the entry to test
      Returns:
      The implementation will return true if the ArchiveEntry is an instance of TarArchiveEntry
    • getCurrentEntry

      public TarArchiveEntry getCurrentEntry()
      Get the current TAR Archive Entry that this input stream is processing
      Returns:
      The current Archive Entry
    • setCurrentEntry

      protected final void setCurrentEntry(TarArchiveEntry e)
    • isAtEOF

      protected final boolean isAtEOF()
    • setAtEOF

      protected final void setAtEOF(boolean b)
    • consumeRemainderOfLastBlock

      private void consumeRemainderOfLastBlock() throws IOException
      This method is invoked once the end of the archive is hit, it tries to consume the remaining bytes under the assumption that the tool creating this archive has padded the last block.
      Throws:
      IOException
    • matches

      public static boolean matches(byte[] signature, int length)
      Checks if the signature matches what is expected for a tar file.
      Parameters:
      signature - the bytes to check
      length - the number of bytes to check
      Returns:
      true, if this stream is a tar archive stream, false otherwise
    • buildSparseInputStreams

      private void buildSparseInputStreams() throws IOException
      Build the input streams consisting of all-zero input streams and non-zero input streams. When reading from the non-zero input streams, the data is actually read from the original input stream. The size of each input stream is introduced by the sparse headers. NOTE : Some all-zero input streams and non-zero input streams have the size of 0. We DO NOT store the 0 size input streams because they are meaningless.
      Throws:
      IOException