Class TarFile
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Since:
- 1.21
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SeekableByteChannel
private final int
private TarArchiveEntry
The meta-data about the current entryprivate final LinkedList<TarArchiveEntry>
private final List<TarArchiveStructSparse>
private boolean
private final boolean
private final ByteBuffer
private final int
private static final int
private final byte[]
private final Map<String,
List<InputStream>> private final ZipEncoding
The encoding of the tar file -
Constructor Summary
ConstructorsConstructorDescriptionTarFile
(byte[] content) Constructor for TarFile.TarFile
(byte[] content, boolean lenient) Constructor for TarFile.Constructor for TarFile.Constructor for TarFile.Constructor for TarFile.Constructor for TarFile.TarFile
(SeekableByteChannel content) Constructor for TarFile.TarFile
(SeekableByteChannel archive, int blockSize, int recordSize, String encoding, boolean lenient) Constructor for TarFile.Constructor for TarFile.Constructor for TarFile.Constructor for TarFile. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
applyPaxHeadersToCurrentEntry
(Map<String, String> headers, List<TarArchiveStructSparse> sparseHeaders) Update the current entry with the read pax headersprivate void
Build the input streams consisting of all-zero input streams and non-zero input streams.void
close()
private void
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.Get all TAR Archive Entries from the TarFilegetInputStream
(TarArchiveEntry entry) Gets the input stream for the provided Tar Archive Entry.private byte[]
Get the next entry in this tar archive as longname data.private TarArchiveEntry
Get the next entry in this tar archive.private ByteBuffer
Get the next record in this tar archive.protected final boolean
isAtEOF()
private boolean
private boolean
isEOFRecord
(ByteBuffer headerBuf) private void
For PAX Format 0.0, the sparse headers(GNU.sparse.offset and GNU.sparse.numbytes) may appear multi times, and they look like:private void
private void
Adds the sparse chunks from the current entry to the sparse chunks, including any additional sparse entries following the current entry.private ByteBuffer
Read a record from the input stream and return the data.private void
repositionForwardBy
(long offset) private void
repositionForwardTo
(long newPosition) protected final void
setAtEOF
(boolean b) private void
The last record block should be written at the full size, so skip any additional space used to fill a record after an entryprivate void
Checks if the current position of the SeekableByteChannel is in the archive.private void
Tries to read the next record resetting the position in the archive if it is not a EOF record.
-
Field Details
-
SMALL_BUFFER_SIZE
private static final int SMALL_BUFFER_SIZE- See Also:
-
smallBuf
private final byte[] smallBuf -
archive
-
zipEncoding
The encoding of the tar file -
entries
-
blockSize
private final int blockSize -
lenient
private final boolean lenient -
recordSize
private final int recordSize -
recordBuffer
-
globalSparseHeaders
-
hasHitEOF
private boolean hasHitEOF -
currEntry
The meta-data about the current entry -
globalPaxHeaders
-
sparseInputStreams
-
-
Constructor Details
-
TarFile
Constructor for TarFile.- Parameters:
content
- the content to use- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
content
- the content to useencoding
- the encoding to use- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
content
- the content to uselenient
- when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set toTarArchiveEntry.UNKNOWN
. When set to false such illegal fields cause an exception instead.- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
archive
- the file of the archive to use- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
archive
- the file of the archive to useencoding
- the encoding to use- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
archive
- the file of the archive to uselenient
- when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set toTarArchiveEntry.UNKNOWN
. When set to false such illegal fields cause an exception instead.- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
archivePath
- the path of the archive to use- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
archivePath
- the path of the archive to useencoding
- the encoding to use- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
archivePath
- the path of the archive to uselenient
- when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set toTarArchiveEntry.UNKNOWN
. When set to false such illegal fields cause an exception instead.- Throws:
IOException
- when reading the tar archive fails
-
TarFile
Constructor for TarFile.- Parameters:
content
- the content to use- Throws:
IOException
- when reading the tar archive fails
-
TarFile
public TarFile(SeekableByteChannel archive, int blockSize, int recordSize, String encoding, boolean lenient) throws IOException Constructor for TarFile.- Parameters:
archive
- the seekable byte channel to useblockSize
- the blocks size to userecordSize
- the record size to useencoding
- the encoding to uselenient
- when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set toTarArchiveEntry.UNKNOWN
. When set to false such illegal fields cause an exception instead.- Throws:
IOException
- when reading the tar archive fails
-
-
Method Details
-
getNextTarEntry
Get the next entry in this tar archive. This will skip to the end of the current entry, if there is one, and place the position of the channel 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 if there is no next entry.
- Throws:
IOException
- when reading the next TarEntry fails
-
readOldGNUSparse
Adds the sparse chunks from the current entry to the sparse chunks, including any additional sparse entries following the current entry.- Throws:
IOException
- when reading the sparse entry fails
-
buildSparseInputStreams
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.- Throws:
IOException
-
applyPaxHeadersToCurrentEntry
private void applyPaxHeadersToCurrentEntry(Map<String, String> headers, List<TarArchiveStructSparse> sparseHeaders) throws IOExceptionUpdate the current entry with the read pax headers- Parameters:
headers
- Headers read from the pax headersparseHeaders
- Sparse headers read from pax header- Throws:
IOException
-
paxHeaders
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
-
readGlobalPaxHeaders
- Throws:
IOException
-
getLongNameData
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
-
skipRecordPadding
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
- when skipping the padding of the record fails
-
repositionForwardTo
- Throws:
IOException
-
repositionForwardBy
- Throws:
IOException
-
throwExceptionIfPositionIsNotInArchive
Checks if the current position of the SeekableByteChannel is in the archive.- Throws:
IOException
- If the position is not in the archive
-
getRecord
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 TarEntry in the archive, or null if there is no next entry.
- Throws:
IOException
- when reading the next TarEntry fails
-
tryToConsumeSecondEOFRecord
Tries to read the next record resetting the position in the archive 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
- if reading the record of resetting the position in the archive fails
-
consumeRemainderOfLastBlock
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
-
readRecord
Read a record from the input stream and return the data.- Returns:
- The record data or null if EOF has been hit.
- Throws:
IOException
- if reading from the archive fails
-
getEntries
Get all TAR Archive Entries from the TarFile- Returns:
- All entries from the tar file
-
isEOFRecord
-
isAtEOF
protected final boolean isAtEOF() -
setAtEOF
protected final void setAtEOF(boolean b) -
isDirectory
private boolean isDirectory() -
getInputStream
Gets the input stream for the provided Tar Archive Entry.- Parameters:
entry
- Entry to get the input stream from- Returns:
- Input stream of the provided entry
- Throws:
IOException
- Corrupted TAR archive. Can't read entry.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-