Class TarArchiveEntry

java.lang.Object
org.apache.commons.compress.archivers.tar.TarArchiveEntry
All Implemented Interfaces:
ArchiveEntry, EntryStreamOffsets, TarConstants

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(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(File) or 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.
 

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"
 };
 

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

  • Field Details

  • Constructor Details

    • TarArchiveEntry

      public TarArchiveEntry(String name)
      Construct an entry with only a name. This allows the programmer to construct the entry's header "by hand". File is set to null.

      The entry's name will be the value of the name argument with all file separators replaced by forward slashes and leading slashes as well as Windows drive letters stripped.

      Parameters:
      name - the entry name
    • TarArchiveEntry

      public TarArchiveEntry(String name, boolean preserveAbsolutePath)
      Construct an entry with only a name. This allows the programmer to construct the entry's header "by hand". File is set to null.

      The entry's name will be the value of the name argument with all file separators replaced by forward slashes. Leading slashes and Windows drive letters are stripped if preserveAbsolutePath is false.

      Parameters:
      name - the entry name
      preserveAbsolutePath - whether to allow leading slashes or drive letters in the name.
      Since:
      1.1
    • TarArchiveEntry

      public TarArchiveEntry(String name, byte linkFlag)
      Construct an entry with a name and a link flag.

      The entry's name will be the value of the name argument with all file separators replaced by forward slashes and leading slashes as well as Windows drive letters stripped.

      Parameters:
      name - the entry name
      linkFlag - the entry link flag.
    • TarArchiveEntry

      public TarArchiveEntry(String name, byte linkFlag, boolean preserveAbsolutePath)
      Construct an entry with a name and a link flag.

      The entry's name will be the value of the name argument with all file separators replaced by forward slashes. Leading slashes and Windows drive letters are stripped if preserveAbsolutePath is false.

      Parameters:
      name - the entry name
      linkFlag - the entry link flag.
      preserveAbsolutePath - whether to allow leading slashes or drive letters in the name.
      Since:
      1.5
    • TarArchiveEntry

      public TarArchiveEntry(File file)
      Construct an entry for a file. File is set to file, and the header is constructed from information from the file. The name is set from the normalized file path.

      The entry's name will be the value of the file's path with all file separators replaced by forward slashes and leading slashes as well as Windows drive letters stripped. The name will end in a slash if the file represents a directory.

      Note: Since 1.21 this internally uses the same code as the TarArchiveEntry constructors with a Path as parameter. But all thrown exceptions are ignored. If handling those exceptions is needed consider switching to the path constructors.

      Parameters:
      file - The file that the entry represents.
    • TarArchiveEntry

      public TarArchiveEntry(Path file) throws IOException
      Construct an entry for a file. File is set to file, and the header is constructed from information from the file. The name is set from the normalized file path.

      The entry's name will be the value of the file's path with all file separators replaced by forward slashes and leading slashes as well as Windows drive letters stripped. The name will end in a slash if the file represents a directory.

      Parameters:
      file - The file that the entry represents.
      Throws:
      IOException - if an I/O error occurs
      Since:
      1.21
    • TarArchiveEntry

      public TarArchiveEntry(File file, String fileName)
      Construct an entry for a file. File is set to file, and the header is constructed from information from the file.

      The entry's name will be the value of the fileName argument with all file separators replaced by forward slashes and leading slashes as well as Windows drive letters stripped. The name will end in a slash if the file represents a directory.

      Note: Since 1.21 this internally uses the same code as the TarArchiveEntry constructors with a Path as parameter. But all thrown exceptions are ignored. If handling those exceptions is needed consider switching to the path constructors.

      Parameters:
      file - The file that the entry represents.
      fileName - the name to be used for the entry.
    • TarArchiveEntry

      public TarArchiveEntry(Path file, String fileName, LinkOption... linkOptions) throws IOException
      Construct an entry for a file. File is set to file, and the header is constructed from information from the file.

      The entry's name will be the value of the fileName argument with all file separators replaced by forward slashes and leading slashes as well as Windows drive letters stripped. The name will end in a slash if the file represents a directory.

      Parameters:
      file - The file that the entry represents.
      fileName - the name to be used for the entry.
      linkOptions - options indicating how symbolic links are handled.
      Throws:
      IOException - if an I/O error occurs
      Since:
      1.21
    • TarArchiveEntry

      public TarArchiveEntry(byte[] headerBuf)
      Construct an entry from an archive's header bytes. File is set to null.
      Parameters:
      headerBuf - The header bytes from a tar archive entry.
      Throws:
      IllegalArgumentException - if any of the numeric fields have an invalid format
    • TarArchiveEntry

      public TarArchiveEntry(byte[] headerBuf, ZipEncoding encoding) throws IOException
      Construct an entry from an archive's header bytes. File is set to null.
      Parameters:
      headerBuf - The header bytes from a tar archive entry.
      encoding - encoding to use for file names
      Throws:
      IllegalArgumentException - if any of the numeric fields have an invalid format
      IOException - on error
      Since:
      1.4
    • TarArchiveEntry

      public TarArchiveEntry(byte[] headerBuf, ZipEncoding encoding, boolean lenient) throws IOException
      Construct an entry from an archive's header bytes. File is set to null.
      Parameters:
      headerBuf - The header bytes from a tar archive entry.
      encoding - 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 UNKNOWN. When set to false such illegal fields cause an exception instead.
      Throws:
      IllegalArgumentException - if any of the numeric fields have an invalid format
      IOException - on error
      Since:
      1.19
    • TarArchiveEntry

      public TarArchiveEntry(byte[] headerBuf, ZipEncoding encoding, boolean lenient, long dataOffset) throws IOException
      Construct an entry from an archive's header bytes for random access tar. File is set to null.
      Parameters:
      headerBuf - the header bytes from a tar archive entry.
      encoding - 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 UNKNOWN. When set to false such illegal fields cause an exception instead.
      dataOffset - position of the entry data in the random access file.
      Throws:
      IllegalArgumentException - if any of the numeric fields have an invalid format.
      IOException - on error.
      Since:
      1.21
  • Method Details

    • equals

      public boolean equals(TarArchiveEntry it)
      Determine if the two entries are equal. Equality is determined by the header names being equal.
      Parameters:
      it - Entry to be checked for equality.
      Returns:
      True if the entries are equal.
    • equals

      public boolean equals(Object it)
      Determine if the two entries are equal. Equality is determined by the header names being equal.
      Overrides:
      equals in class Object
      Parameters:
      it - Entry to be checked for equality.
      Returns:
      True if the entries are equal.
    • hashCode

      public int hashCode()
      Hashcodes are based on entry names.
      Overrides:
      hashCode in class Object
      Returns:
      the entry hashcode
    • isDescendent

      public boolean isDescendent(TarArchiveEntry desc)
      Determine if the given entry is a descendant of this entry. Descendancy is determined by the name of the descendant starting with this entry's name.
      Parameters:
      desc - Entry to be checked as a descendent of this.
      Returns:
      True if entry is a descendant of this.
    • getName

      public String getName()
      Get this entry's name.

      This method returns the raw name as it is stored inside of the archive.

      Specified by:
      getName in interface ArchiveEntry
      Returns:
      This entry's name.
    • setName

      public void setName(String name)
      Set this entry's name.
      Parameters:
      name - This entry's new name.
    • setMode

      public void setMode(int mode)
      Set the mode for this entry
      Parameters:
      mode - the mode for this entry
    • getLinkName

      public String getLinkName()
      Get this entry's link name.
      Returns:
      This entry's link name.
    • setLinkName

      public void setLinkName(String link)
      Set this entry's link name.
      Parameters:
      link - the link name to use.
      Since:
      1.1
    • getUserId

      @Deprecated public int getUserId()
      Deprecated.
      use #getLongUserId instead as user ids can be bigger than Integer.MAX_VALUE
      Get this entry's user id.
      Returns:
      This entry's user id.
    • setUserId

      public void setUserId(int userId)
      Set this entry's user id.
      Parameters:
      userId - This entry's new user id.
    • getLongUserId

      public long getLongUserId()
      Get this entry's user id.
      Returns:
      This entry's user id.
      Since:
      1.10
    • setUserId

      public void setUserId(long userId)
      Set this entry's user id.
      Parameters:
      userId - This entry's new user id.
      Since:
      1.10
    • getGroupId

      @Deprecated public int getGroupId()
      Deprecated.
      use #getLongGroupId instead as group ids can be bigger than Integer.MAX_VALUE
      Get this entry's group id.
      Returns:
      This entry's group id.
    • setGroupId

      public void setGroupId(int groupId)
      Set this entry's group id.
      Parameters:
      groupId - This entry's new group id.
    • getLongGroupId

      public long getLongGroupId()
      Get this entry's group id.
      Returns:
      This entry's group id.
      Since:
      1.10
    • setGroupId

      public void setGroupId(long groupId)
      Set this entry's group id.
      Parameters:
      groupId - This entry's new group id.
      Since:
      1.10
    • getUserName

      public String getUserName()
      Get this entry's user name.
      Returns:
      This entry's user name.
    • setUserName

      public void setUserName(String userName)
      Set this entry's user name.
      Parameters:
      userName - This entry's new user name.
    • getGroupName

      public String getGroupName()
      Get this entry's group name.
      Returns:
      This entry's group name.
    • setGroupName

      public void setGroupName(String groupName)
      Set this entry's group name.
      Parameters:
      groupName - This entry's new group name.
    • setIds

      public void setIds(int userId, int groupId)
      Convenience method to set this entry's group and user ids.
      Parameters:
      userId - This entry's new user id.
      groupId - This entry's new group id.
    • setNames

      public void setNames(String userName, String groupName)
      Convenience method to set this entry's group and user names.
      Parameters:
      userName - This entry's new user name.
      groupName - This entry's new group name.
    • setModTime

      public void setModTime(long time)
      Set this entry's modification time. The parameter passed to this method is in "Java time".
      Parameters:
      time - This entry's new modification time.
    • setModTime

      public void setModTime(Date time)
      Set this entry's modification time.
      Parameters:
      time - This entry's new modification time.
    • setModTime

      public void setModTime(FileTime time)
      Set this entry's modification time.
      Parameters:
      time - This entry's new modification time.
      Since:
      1.21
    • getModTime

      public Date getModTime()
      Get this entry's modification time.
      Returns:
      This entry's modification time.
    • getLastModifiedDate

      Description copied from interface: ArchiveEntry
      Gets the last modified date of this entry.
      Specified by:
      getLastModifiedDate in interface ArchiveEntry
      Returns:
      the last modified date of this entry.
    • isCheckSumOK

      public boolean isCheckSumOK()
      Get this entry's checksum status.
      Returns:
      if the header checksum is reasonably correct
      Since:
      1.5
      See Also:
    • getFile

      public File getFile()
      Get this entry's file.

      This method is only useful for entries created from a File or Path but not for entries read from an archive.

      Returns:
      this entry's file or null if the entry was not created from a file.
    • getPath

      public Path getPath()
      Get this entry's file.

      This method is only useful for entries created from a File or Path but not for entries read from an archive.

      Returns:
      this entry's file or null if the entry was not created from a file.
      Since:
      1.21
    • getMode

      public int getMode()
      Get this entry's mode.
      Returns:
      This entry's mode.
    • getSize

      public long getSize()
      Get this entry's file size.

      This is the size the entry's data uses inside of the archive. Usually this is the same as getRealSize(), but it doesn't take the "holes" into account when the entry represents a sparse file.

      Specified by:
      getSize in interface ArchiveEntry
      Returns:
      This entry's file size.
    • setSparseHeaders

      public void setSparseHeaders(List<TarArchiveStructSparse> sparseHeaders)
      Set this entry's sparse headers
      Parameters:
      sparseHeaders - The new sparse headers
      Since:
      1.20
    • getSparseHeaders

      Get this entry's sparse headers
      Returns:
      This entry's sparse headers
      Since:
      1.20
    • getOrderedSparseHeaders

      Get this entry's sparse headers ordered by offset with all empty sparse sections at the start filtered out.
      Returns:
      immutable list of this entry's sparse headers, never null
      Throws:
      IOException - if the list of sparse headers contains blocks that overlap
      Since:
      1.21
    • isPaxGNU1XSparse

      public boolean isPaxGNU1XSparse()
      Get if this entry is a sparse file with 1.X PAX Format or not
      Returns:
      True if this entry is a sparse file with 1.X PAX Format
      Since:
      1.20
    • setSize

      public void setSize(long size)
      Set this entry's file size.
      Parameters:
      size - This entry's new file size.
      Throws:
      IllegalArgumentException - if the size is < 0.
    • getDevMajor

      public int getDevMajor()
      Get this entry's major device number.
      Returns:
      This entry's major device number.
      Since:
      1.4
    • setDevMajor

      public void setDevMajor(int devNo)
      Set this entry's major device number.
      Parameters:
      devNo - This entry's major device number.
      Throws:
      IllegalArgumentException - if the devNo is < 0.
      Since:
      1.4
    • getDevMinor

      public int getDevMinor()
      Get this entry's minor device number.
      Returns:
      This entry's minor device number.
      Since:
      1.4
    • setDevMinor

      public void setDevMinor(int devNo)
      Set this entry's minor device number.
      Parameters:
      devNo - This entry's minor device number.
      Throws:
      IllegalArgumentException - if the devNo is < 0.
      Since:
      1.4
    • isExtended

      public boolean isExtended()
      Indicates in case of an oldgnu sparse file if an extension sparse header follows.
      Returns:
      true if an extension oldgnu sparse header follows.
    • getRealSize

      public long getRealSize()
      Get this entry's real file size in case of a sparse file.

      This is the size a file would take on disk if the entry was expanded.

      If the file is not a sparse file, return size instead of realSize.

      Returns:
      This entry's real file size, if the file is not a sparse file, return size instead of realSize.
    • isGNUSparse

      public boolean isGNUSparse()
      Indicate if this entry is a GNU sparse block.
      Returns:
      true if this is a sparse extension provided by GNU tar
    • isOldGNUSparse

      public boolean isOldGNUSparse()
      Indicate if this entry is a GNU or star sparse block using the oldgnu format.
      Returns:
      true if this is a sparse extension provided by GNU tar or star
      Since:
      1.11
    • isPaxGNUSparse

      public boolean isPaxGNUSparse()
      Indicate if this entry is a GNU sparse block using one of the PAX formats.
      Returns:
      true if this is a sparse extension provided by GNU tar
      Since:
      1.11
    • isStarSparse

      public boolean isStarSparse()
      Indicate if this entry is a star sparse block using PAX headers.
      Returns:
      true if this is a sparse extension provided by star
      Since:
      1.11
    • isGNULongLinkEntry

      public boolean isGNULongLinkEntry()
      Indicate if this entry is a GNU long linkname block
      Returns:
      true if this is a long name extension provided by GNU tar
    • isGNULongNameEntry

      public boolean isGNULongNameEntry()
      Indicate if this entry is a GNU long name block
      Returns:
      true if this is a long name extension provided by GNU tar
    • isPaxHeader

      public boolean isPaxHeader()
      Check if this is a Pax header.
      Returns:
      true if this is a Pax header.
      Since:
      1.1
    • isGlobalPaxHeader

      public boolean isGlobalPaxHeader()
      Check if this is a Pax header.
      Returns:
      true if this is a Pax header.
      Since:
      1.1
    • isDirectory

      public boolean isDirectory()
      Return whether or not this entry represents a directory.
      Specified by:
      isDirectory in interface ArchiveEntry
      Returns:
      True if this entry is a directory.
    • isFile

      public boolean isFile()
      Check if this is a "normal file"
      Returns:
      whether this is a "normal file"
      Since:
      1.2
    • isSymbolicLink

      public boolean isSymbolicLink()
      Check if this is a symbolic link entry.
      Returns:
      whether this is a symbolic link
      Since:
      1.2
    • isLink

      public boolean isLink()
      Check if this is a link entry.
      Returns:
      whether this is a link entry
      Since:
      1.2
    • isCharacterDevice

      public boolean isCharacterDevice()
      Check if this is a character device entry.
      Returns:
      whether this is a character device
      Since:
      1.2
    • isBlockDevice

      public boolean isBlockDevice()
      Check if this is a block device entry.
      Returns:
      whether this is a block device
      Since:
      1.2
    • isFIFO

      public boolean isFIFO()
      Check if this is a FIFO (pipe) entry.
      Returns:
      whether this is a FIFO entry
      Since:
      1.2
    • isSparse

      public boolean isSparse()
      Check whether this is a sparse entry.
      Returns:
      whether this is a sparse entry
      Since:
      1.11
    • getDataOffset

      public long getDataOffset()
      Gets the offset of data stream within the archive file,
      Specified by:
      getDataOffset in interface EntryStreamOffsets
      Returns:
      the offset of entry data stream, OFFSET_UNKNOWN if not known.
      Since:
      1.21
    • setDataOffset

      public void setDataOffset(long dataOffset)
      Set the offset of the data for the tar entry.
      Parameters:
      dataOffset - the position of the data in the tar.
      Since:
      1.21
    • isStreamContiguous

      public boolean isStreamContiguous()
      Indicates whether the stream is contiguous, i.e. not split among several archive parts, interspersed with control blocks, etc.
      Specified by:
      isStreamContiguous in interface EntryStreamOffsets
      Returns:
      true if stream is contiguous, false otherwise.
      Since:
      1.21
    • getExtraPaxHeaders

      get extra PAX Headers
      Returns:
      read-only map containing any extra PAX Headers
      Since:
      1.15
    • clearExtraPaxHeaders

      public void clearExtraPaxHeaders()
      clear all extra PAX headers.
      Since:
      1.15
    • addPaxHeader

      public void addPaxHeader(String name, String value)
      add a PAX header to this entry. If the header corresponds to an existing field in the entry, that field will be set; otherwise the header will be added to the extraPaxHeaders Map
      Parameters:
      name - The full name of the header to set.
      value - value of header.
      Since:
      1.15
    • getExtraPaxHeader

      get named extra PAX header
      Parameters:
      name - The full name of an extended PAX header to retrieve
      Returns:
      The value of the header, if any.
      Since:
      1.15
    • getDirectoryEntries

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

      This method is only useful for entries created from a File or Path but not for entries read from an archive.

      Returns:
      An array of TarEntry's for this entry's children.
    • writeEntryHeader

      public void writeEntryHeader(byte[] outbuf)
      Write an entry's header information to a header buffer.

      This method does not use the star/GNU tar/BSD tar extensions.

      Parameters:
      outbuf - The tar entry header buffer to fill in.
    • writeEntryHeader

      public void writeEntryHeader(byte[] outbuf, ZipEncoding encoding, boolean starMode) throws IOException
      Write an entry's header information to a header buffer.
      Parameters:
      outbuf - The tar entry header buffer to fill in.
      encoding - encoding to use when writing the file name.
      starMode - 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
      Throws:
      IOException - on error
      Since:
      1.4
    • parseTarHeader

      public void parseTarHeader(byte[] header)
      Parse an entry's header information from a header buffer.
      Parameters:
      header - The tar entry header buffer to get information from.
      Throws:
      IllegalArgumentException - if any of the numeric fields have an invalid format
    • parseTarHeader

      public void parseTarHeader(byte[] header, ZipEncoding encoding) throws IOException
      Parse an entry's header information from a header buffer.
      Parameters:
      header - The tar entry header buffer to get information from.
      encoding - encoding to use for file names
      Throws:
      IllegalArgumentException - if any of the numeric fields have an invalid format
      IOException - on error
      Since:
      1.4