Top Description Fields Constructors Methods
org.apache.commons.compress.archivers.tar

public Class TarArchiveEntry

extends Object
implements ArchiveEntry, TarConstants, EntryStreamOffsets
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.archivers.EntryStreamOffsets, org.apache.commons.compress.archivers.tar.TarConstants, org.apache.commons.compress.archivers.ArchiveEntry
Imports
java.io.File, .IOException, .UncheckedIOException, java.math.BigDecimal, java.nio.file.DirectoryStream, .Files, .LinkOption, .Path, java.nio.file.attribute.BasicFileAttributes, .DosFileAttributes, .FileTime, .PosixFileAttributes, java.time.DateTimeException, .Instant, java.util.ArrayList, .Collections, .Comparator, .Date, .HashMap, .List, .Locale, .Map, .Objects, .Set, java.util.regex.Pattern, java.util.stream.Collectors, org.apache.commons.compress.archivers.ArchiveEntry, .EntryStreamOffsets, org.apache.commons.compress.archivers.zip.ZipEncoding, org.apache.commons.compress.utils.ArchiveUtils, .IOUtils, .ParsingUtils, .TimeUtils, org.apache.commons.io.file.attribute.FileTimes

This class represents an entry in a Tar archive. It consists of the entry's header, as well as the entry's File. Entries can be instantiated in one of three ways, depending on how they are to be used.

TarEntries that are created from the header bytes read from an archive are instantiated with the TarArchiveEntry#TarArchiveEntry(byte[]) constructor. These entries will be used when extracting from or listing the contents of an archive. These entries have their header filled in using the header bytes. They also set the File to null, since they reference an archive entry not a file.

TarEntries that are created from Files that are to be written into an archive are instantiated with the TarArchiveEntry#TarArchiveEntry(File) or TarArchiveEntry#TarArchiveEntry(Path) constructor. These entries have their header filled in using the File's information. They also keep a reference to the File for convenience when writing entries.

Finally, TarEntries can be constructed from nothing but a name. This allows the programmer to construct the entry by hand, for instance when only an InputStream is available for writing to the archive, and the header information is constructed from other information. In this case the header fields are set to defaults and the File is set to null.

The C structure for a Tar Entry's header is:

struct header {
  char name[100];     // TarConstants.NAMELEN    - offset   0
  char mode[8];       // TarConstants.MODELEN    - offset 100
  char uid[8];        // TarConstants.UIDLEN     - offset 108
  char gid[8];        // TarConstants.GIDLEN     - offset 116
  char size[12];      // TarConstants.SIZELEN    - offset 124
  char mtime[12];     // TarConstants.MODTIMELEN - offset 136
  char chksum[8];     // TarConstants.CHKSUMLEN  - offset 148
  char linkflag[1];   //                         - offset 156
  char linkname[100]; // TarConstants.NAMELEN    - offset 157
  // The following fields are only present in new-style POSIX tar archives:
  char magic[6];      // TarConstants.MAGICLEN   - offset 257
  char version[2];    // TarConstants.VERSIONLEN - offset 263
  char uname[32];     // TarConstants.UNAMELEN   - offset 265
  char gname[32];     // TarConstants.GNAMELEN   - offset 297
  char devmajor[8];   // TarConstants.DEVLEN     - offset 329
  char devminor[8];   // TarConstants.DEVLEN     - offset 337
  char prefix[155];   // TarConstants.PREFIXLEN  - offset 345
  // Used if "name" field is not long enough to hold the path
  char pad[12];       // NULs                    - offset 500
} header;

All unused bytes are set to null. New-style GNU tar files are slightly different from the above. For values of size larger than 077777777777L (11 7s) or uid and gid larger than 07777777L (7 7s) the sign bit of the first byte is set, and the rest of the field is the binary representation of the number. See TarUtils#parseOctalOrBinary(byte[], int, int).

The C structure for a old GNU Tar Entry's header is:

struct oldgnu_header {
  char unused_pad1[345]; // TarConstants.PAD1LEN_GNU       - offset 0
  char atime[12];        // TarConstants.ATIMELEN_GNU      - offset 345
  char ctime[12];        // TarConstants.CTIMELEN_GNU      - offset 357
  char offset[12];       // TarConstants.OFFSETLEN_GNU     - offset 369
  char longnames[4];     // TarConstants.LONGNAMESLEN_GNU  - offset 381
  char unused_pad2;      // TarConstants.PAD2LEN_GNU       - offset 385
  struct sparse sp[4];   // TarConstants.SPARSELEN_GNU     - offset 386
  char isextended;       // TarConstants.ISEXTENDEDLEN_GNU - offset 482
  char realsize[12];     // TarConstants.REALSIZELEN_GNU   - offset 483
  char unused_pad[17];   // TarConstants.PAD3LEN_GNU       - offset 495
};

Whereas, "struct sparse" is:

struct sparse {
  char offset[12];   // offset 0
  char numbytes[12]; // offset 12
};

The C structure for a xstar (Jörg Schilling star) Tar Entry's header is:

struct star_header {
  char name[100];     // offset   0
  char mode[8];       // offset 100
  char uid[8];        // offset 108
  char gid[8];        // offset 116
  char size[12];      // offset 124
  char mtime[12];     // offset 136
  char chksum[8];     // offset 148
  char typeflag;      // offset 156
  char linkname[100]; // offset 157
  char magic[6];      // offset 257
  char version[2];    // offset 263
  char uname[32];     // offset 265
  char gname[32];     // offset 297
  char devmajor[8];   // offset 329
  char devminor[8];   // offset 337
  char prefix[131];   // offset 345
  char atime[12];     // offset 476
  char ctime[12];     // offset 488
  char mfill[8];      // offset 500
  char xmagic[4];     // offset 508  "tar\0"
};

which is identical to new-style POSIX up to the first 130 bytes of the prefix.

The C structure for the xstar-specific parts of a xstar Tar Entry's header is:

struct xstar_in_header {
  char fill[345];         // offset 0     Everything before t_prefix
  char prefix[1];         // offset 345   Prefix for t_name
  char fill2;             // offset 346
  char fill3[8];          // offset 347
  char isextended;        // offset 355
  struct sparse sp[SIH];  // offset 356   8 x 12
  char realsize[12];      // offset 452   Real size for sparse data
  char offset[12];        // offset 464   Offset for multivolume data
  char atime[12];         // offset 476
  char ctime[12];         // offset 488
  char mfill[8];          // offset 500
  char xmagic[4];         // offset 508   "tar\0"
};

Field Summary

Modifier and TypeField and Description
private FileTime
aTime

The entry's last access time.

private FileTime
birthTime

The entry's creation time.

private boolean
checkSumOK

If the header checksum is reasonably correct.

private FileTime
cTime

The entry's status change time.

private long
public static final int
DEFAULT_DIR_MODE

Default permissions bits for directories

public static final int
DEFAULT_FILE_MODE

Default permissions bits for files

private int
devMajor

The entry's major device number.

private int
devMinor

The entry's minor device number.

private static final TarArchiveEntry[]
private final Map<String, String>
extraPaxHeaders

Extra, user supplied pax headers

private final Path
file

The entry's file reference

private long
groupId

The entry's group id.

private String
groupName

The entry's group name.

private boolean
isExtended

If an extension sparse header follows.

private byte
linkFlag

The entry's link flag.

private String
linkName

The entry's link name.

private final LinkOption[]
linkOptions

The entry's file linkOptions

private String
magic

The entry's magic tag.

public static final int
MAX_NAMELEN

Maximum length of a user's name in the tar file

public static final int
MILLIS_PER_SECOND

Convert millis to seconds

private int
mode

The entry's permission mode.

private FileTime
mTime

The entry's modification time.

private String
name

The entry's name.

private static final Pattern
PAX_EXTENDED_HEADER_FILE_TIMES_PATTERN

Regular expression pattern for validating values in pax extended header file time fields.

private boolean
paxGNU1XSparse

is this entry a GNU sparse entry using 1.X PAX formats?

private boolean
paxGNUSparse

Is this entry a GNU sparse entry using one of the PAX formats?

private final boolean
preserveAbsolutePath

Whether to allow leading slashes or drive names inside the name

private long
realSize

The entry's real size in case of a sparse file.

private long
size

The entry's size.

private List<TarArchiveStructSparse>
sparseHeaders

The sparse headers in tar

private boolean
starSparse

Is this entry a star sparse entry using the PAX header?

public static final long
UNKNOWN

Value used to indicate unknown mode, user/groupids, device numbers and modTime when parsing a file in lenient mode and the archive contains illegal fields.

private long
userId

The entry's user id.

private String
userName

The entry's user name.

private String
version

The version of the format

Constructor Summary

AccessConstructor and Description
private
TarArchiveEntry(final boolean preserveAbsolutePath)

Constructs an empty entry and prepares the header values.

public
TarArchiveEntry(final byte[]
The header bytes from a tar archive entry.
headerBuf
)

Constructs an entry from an archive's header bytes.

public
TarArchiveEntry(final byte[]
The header bytes from a tar archive entry.
headerBuf
,
final ZipEncoding
encoding to use for file names
encoding
)

Constructs an entry from an archive's header bytes.

public
TarArchiveEntry(final byte[]
The header bytes from a tar archive entry.
headerBuf
,
final ZipEncoding
encoding to use for file names
encoding
,
final boolean
when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to UNKNOWN. When set to false such illegal fields cause an exception instead.
lenient
)

Constructs an entry from an archive's header bytes.

public
TarArchiveEntry(final byte[]
the header bytes from a tar archive entry.
headerBuf
,
final ZipEncoding
encoding to use for file names.
encoding
,
final boolean
when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to UNKNOWN. When set to false such illegal fields cause an exception instead.
lenient
,
final long
position of the entry data in the random access file.
dataOffset
)

Constructs an entry from an archive's header bytes for random access tar.

public
TarArchiveEntry(final File
The file that the entry represents.
file
)

Constructs an entry for a file.

public
TarArchiveEntry(final File
The file that the entry represents.
file
,
final String
the name to be used for the entry.
fileName
)

Constructs an entry for a file.

public
TarArchiveEntry(final Map<String, String>
the parsed global PAX headers, or null if this is the first one.
globalPaxHeaders
,
final byte[]
The header bytes from a tar archive entry.
headerBuf
,
final ZipEncoding
encoding to use for file names
encoding
,
final boolean
when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to UNKNOWN. When set to false such illegal fields cause an exception instead.
lenient
)

Constructs an entry from an archive's header bytes.

public
TarArchiveEntry(final Map<String, String>
the parsed global PAX headers, or null if this is the first one.
globalPaxHeaders
,
final byte[]
the header bytes from a tar archive entry.
headerBuf
,
final ZipEncoding
encoding to use for file names.
encoding
,
final boolean
when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to UNKNOWN. When set to false such illegal fields cause an exception instead.
lenient
,
final long
position of the entry data in the random access file.
dataOffset
)

Constructs an entry from an archive's header bytes for random access tar.

public
TarArchiveEntry(final Path
The file that the entry represents.
file
)

Constructs an entry for a file.

public
TarArchiveEntry(final Path
The file that the entry represents.
file
,
final String
the name to be used for the entry.
fileName
,
final LinkOption...
options indicating how symbolic links are handled.
linkOptions
)

Constructs an entry for a file.

public
TarArchiveEntry(final String
the entry name
name
)

Constructs an entry with only a name.

public
TarArchiveEntry(String
the entry name
name
,
final boolean
whether to allow leading slashes or drive letters in the name.
preserveAbsolutePath
)

Constructs an entry with only a name.

public
TarArchiveEntry(final String
the entry name
name
,
final byte
the entry link flag.
linkFlag
)

Constructs an entry with a name and a link flag.

public
TarArchiveEntry(final String
the entry name
name
,
final byte
the entry link flag.
linkFlag
,
final boolean
whether to allow leading slashes or drive letters in the name.
preserveAbsolutePath
)

Constructs an entry with a name and a link flag.

Method Summary

Modifier and TypeMethod and Description
public void
addPaxHeader(final String
The full name of the header to set.
name
,
final String
value of header.
value
)

Adds a PAX header to this entry.

public void
clearExtraPaxHeaders()

Clears all extra PAX headers.

public boolean

Returns:

True if the entries are equal.
equals
(final Object
Entry to be checked for equality.
it
)

Overrides java.lang.Object.equals.

Determine if the two entries are equal.
public boolean

Returns:

True if the entries are equal.
equals
(final TarArchiveEntry
Entry to be checked for equality.
it
)

Determine if the two entries are equal.

private int

Returns:

format type
evaluateType
(final Map<String, String> globalPaxHeaders, final byte[]
The tar entry header buffer to evaluate the format for.
header
)

Evaluates an entry's header format from a header buffer.

private static FileTime
fileTimeFromOptionalSeconds(final long seconds)

private int
fill(final byte value, final int offset, final byte[] outbuf, final int length)

private int
fill(final int value, final int offset, final byte[] outbuf, final int length)

pack-priv void
pack-priv void
pack-priv void
public FileTime

Returns:

This entry's computed creation time.
getCreationTime
()

Gets this entry's creation time.

public long
getDataOffset()

Implements org.apache.commons.compress.archivers.EntryStreamOffsets.getDataOffset.

Gets the offset of data stream within the archive file,
public int

Returns:

This entry's major device number.
getDevMajor
()

Gets this entry's major device number.

public int

Returns:

This entry's minor device number.
getDevMinor
()

Gets this entry's minor device number.

public TarArchiveEntry[]

Returns:

An array of TarEntry's for this entry's children.
getDirectoryEntries
()

If this entry represents a file, and the file is a directory, return an array of TarEntries for this entry's children.

public String

Returns:

The value of the header, if any.
getExtraPaxHeader
(final String
The full name of an extended PAX header to retrieve
name
)

Gets named extra PAX header

public Map<String, String>

Returns:

read-only map containing any extra PAX Headers
getExtraPaxHeaders
()

Gets extra PAX Headers

public File

Returns:

this entry's file or null if the entry was not created from a file.
getFile
()

Gets this entry's file.

public int

Returns:

This entry's group id.
getGroupId
()

Deprecated use #getLongGroupId instead as group ids can be bigger than Integer#MAX_VALUE
Gets this entry's group id.
public String

Returns:

This entry's group name.
getGroupName
()

Gets this entry's group name.

public FileTime

Returns:

This entry's last access time.
getLastAccessTime
()

Gets this entry's last access time.

public Date

Returns:

This entry's modification time.
getLastModifiedDate
()

Implements org.apache.commons.compress.archivers.ArchiveEntry.getLastModifiedDate.

Gets this entry's modification time.
public FileTime

Returns:

This entry's modification time.
getLastModifiedTime
()

Gets this entry's modification time.

public byte

Returns:

this entry's link flag.
getLinkFlag
()

Gets this entry's link flag.

public String

Returns:

This entry's link name.
getLinkName
()

Gets this entry's link name.

public long

Returns:

This entry's group id.
getLongGroupId
()

Gets this entry's group id.

public long

Returns:

This entry's user id.
getLongUserId
()

Gets this entry's user id.

public int

Returns:

This entry's mode.
getMode
()

Gets this entry's mode.

public Date

Returns:

This entry's modification time.
getModTime
()

Gets this entry's modification time.

public String

Returns:

This entry's name.
getName
()

Implements org.apache.commons.compress.archivers.ArchiveEntry.getName.

Gets this entry's name.
public List<TarArchiveStructSparse>

Returns:

immutable list of this entry's sparse headers, never null
getOrderedSparseHeaders
()

Gets this entry's sparse headers ordered by offset with all empty sparse sections at the start filtered out.

public Path

Returns:

this entry's file or null if the entry was not created from a file.
getPath
()

Gets this entry's file.

public long

Returns:

This entry's real file size, if the file is not a sparse file, return size instead of realSize.
getRealSize
()

Gets this entry's real file size in case of a sparse file.

public long

Returns:

This entry's file size.
getSize
()

Implements org.apache.commons.compress.archivers.ArchiveEntry.getSize.

Gets this entry's file size.
public List<TarArchiveStructSparse>

Returns:

This entry's sparse headers
getSparseHeaders
()

Gets this entry's sparse headers

public FileTime

Returns:

This entry's status change time.
getStatusChangeTime
()

Gets this entry's status change time.

public int

Returns:

This entry's user id.
getUserId
()

Deprecated use #getLongUserId instead as user ids can be bigger than Integer#MAX_VALUE
Gets this entry's user id.
public String

Returns:

This entry's user name.
getUserName
()

Gets this entry's user name.

public int

Returns:

the entry hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Hash codes are based on entry names.
public boolean

Returns:

whether this is a block device
isBlockDevice
()

Tests whether this is a block device entry.

public boolean

Returns:

whether this is a character device
isCharacterDevice
()

Tests whether this is a character device entry.

public boolean

Returns:

if the header checksum is reasonably correct
isCheckSumOK
()

Tests whether this entry's checksum status.

public boolean

Returns:

True if entry is a descendant of this.
isDescendent
(final TarArchiveEntry
Entry to be checked as a descendent of this.
desc
)

Tests whether the given entry is a descendant of this entry.

public boolean

Returns:

True if this entry is a directory.
isDirectory
()

Implements org.apache.commons.compress.archivers.ArchiveEntry.isDirectory.

Tests whether or not this entry represents a directory.
public boolean

Returns:

true if an extension oldgnu sparse header follows.
isExtended
()

Tests whether in case of an oldgnu sparse file if an extension sparse header follows.

public boolean

Returns:

whether this is a FIFO entry
isFIFO
()

Tests whether this is a FIFO (pipe) entry.

public boolean

Returns:

whether this is a "normal file"
isFile
()

Tests whether this is a "normal file"

public boolean

Returns:

true if this is a Pax header.
isGlobalPaxHeader
()

Tests whether this is a Pax header.

public boolean

Returns:

true if this is a long name extension provided by GNU tar
isGNULongLinkEntry
()

Tests whether this entry is a GNU long linkname block

public boolean

Returns:

true if this is a long name extension provided by GNU tar
isGNULongNameEntry
()

Tests whether this entry is a GNU long name block

public boolean

Returns:

true if this is a sparse extension provided by GNU tar
isGNUSparse
()

Tests whether this entry is a GNU sparse block.

private boolean
isInvalidPrefix(final byte[] header)

private boolean
isInvalidXtarTime(final byte[] buffer, final int offset, final int length)

public boolean

Returns:

true if this is a sparse extension provided by GNU tar or star
isOldGNUSparse
()

Tests whether this entry is a GNU or star sparse block using the oldgnu format.

public boolean

Returns:

True if this entry is a sparse file with 1.X PAX Format
isPaxGNU1XSparse
()

Tests whether this entry is a sparse file with 1.X PAX Format or not

public boolean

Returns:

true if this is a sparse extension provided by GNU tar
isPaxGNUSparse
()

Tests whether this entry is a GNU sparse block using one of the PAX formats.

public boolean

Returns:

true if this is a Pax header.
isPaxHeader
()

Tests whether this is a Pax header.

public boolean

Returns:

whether this is a sparse entry
isSparse
()

Tests whether this is a sparse entry.

public boolean

Returns:

true if this is a sparse extension provided by star
isStarSparse
()

Tests whether this entry is a star sparse block using PAX headers.

public boolean
isStreamContiguous()

Implements org.apache.commons.compress.archivers.EntryStreamOffsets.isStreamContiguous.

Indicates whether the stream is contiguous, i.e. not split among several archive parts, interspersed with control blocks, etc.
private boolean
isXstar(final Map<String, String> globalPaxHeaders, final byte[] header)

Tests whether the given header is in XSTAR / XUSTAR format.

private static String
normalizeFileName(String fileName, final boolean preserveAbsolutePath)

Strips Windows' drive letter as well as any leading slashes, turns path separators into forward slashes.

private static Instant
private long
parseOctalOrBinary(final byte[] header, final int offset, final int length, final boolean lenient)

public void
parseTarHeader(final byte[]
The tar entry header buffer to get information from.
header
)

Parses an entry's header information from a header buffer.

public void
parseTarHeader(final byte[]
The tar entry header buffer to get information from.
header
,
final ZipEncoding
encoding to use for file names
encoding
)

Parse an entry's header information from a header buffer.

private void
parseTarHeader(final byte[] header, final ZipEncoding encoding, final boolean oldStyle, final boolean lenient)

private void
parseTarHeader(final Map<String, String> globalPaxHeaders, final byte[] header, final ZipEncoding encoding, final boolean oldStyle, final boolean lenient)

private void
parseTarHeaderUnwrapped(final Map<String, String> globalPaxHeaders, final byte[] header, final ZipEncoding encoding, final boolean oldStyle, final boolean lenient)

private void
processPaxHeader(final String key, final String val)

Processes one pax header, using the entries extraPaxHeaders map as source for extra headers used when handling entries for sparse files.

private void
processPaxHeader(final String
the header name.
key
,
final String
the header value.
val
,
final Map<String, String>
map of headers used for dealing with sparse file.
headers
)

Processes one pax header, using the supplied map as source for extra headers to be used when handling entries for sparse files

private void
readFileMode(final Path file, final String normalizedName, final LinkOption... options)

private void
readOsSpecificProperties(final Path file, final LinkOption... options)

public void
setCreationTime(final FileTime
This entry's new creation time.
time
)

Sets this entry's creation time.

public void
setDataOffset(final long
the position of the data in the tar.
dataOffset
)

Sets the offset of the data for the tar entry.

public void
setDevMajor(final int
This entry's major device number.
devNo
)

Sets this entry's major device number.

public void
setDevMinor(final int
This entry's minor device number.
devNo
)

Sets this entry's minor device number.

public void
setGroupId(final int
This entry's new group id.
groupId
)

Sets this entry's group id.

public void
setGroupId(final long
This entry's new group id.
groupId
)

Sets this entry's group id.

public void
setGroupName(final String
This entry's new group name.
groupName
)

Sets this entry's group name.

public void
setIds(final int
This entry's new user id.
userId
,
final int
This entry's new group id.
groupId
)

Convenience method to set this entry's group and user ids.

public void
setLastAccessTime(final FileTime
This entry's new last access time.
time
)

Sets this entry's last access time.

public void
setLastModifiedTime(final FileTime
This entry's new modification time.
time
)

Sets this entry's modification time.

public void
setLinkName(final String
the link name to use.
link
)

Sets this entry's link name.

public void
setMode(final int
the mode for this entry
mode
)

Sets the mode for this entry

public void
setModTime(final Date
This entry's new modification time.
time
)

Sets this entry's modification time.

public void
setModTime(final FileTime
This entry's new modification time.
time
)

Sets this entry's modification time.

public void
setModTime(final long
This entry's new modification time.
time
)

Sets this entry's modification time.

public void
setName(final String
This entry's new name.
name
)

Sets this entry's name.

public void
setNames(final String
This entry's new user name.
userName
,
final String
This entry's new group name.
groupName
)

Convenience method to set this entry's group and user names.

public void
setSize(final long
This entry's new file size.
size
)

Sets this entry's file size.

public void
setSparseHeaders(final List<TarArchiveStructSparse>
The new sparse headers
sparseHeaders
)

Sets this entry's sparse headers

public void
setStatusChangeTime(final FileTime
This entry's new status change time.
time
)

Sets this entry's status change time.

public void
setUserId(final int
This entry's new user id.
userId
)

Sets this entry's user id.

public void
setUserId(final long
This entry's new user id.
userId
)

Sets this entry's user id.

public void
setUserName(final String
This entry's new user name.
userName
)

Sets this entry's user name.

pack-priv void
updateEntryFromPaxHeaders(final Map<String, String> headers)

Update the entry using a map of pax headers.

public void
writeEntryHeader(final byte[]
The tar entry header buffer to fill in.
outbuf
)

Writes an entry's header information to a header buffer.

public void
writeEntryHeader(final byte[]
The tar entry header buffer to fill in.
outbuf
,
final ZipEncoding
encoding to use when writing the file name.
encoding
,
final boolean
whether to use the star/GNU tar/BSD tar extension for numeric fields if their value doesn't fit in the maximum size of standard tar archives
starMode
)

Writes an entry's header information to a header buffer.

private int
writeEntryHeaderField(final long value, final byte[] outbuf, final int offset, final int length, final boolean starMode)

private int
writeEntryHeaderOptionalTimeField(final FileTime time, int offset, final byte[] outbuf, final int fieldLength)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait