Class PosixTarHeader
This class represents the 512-byte header that precedes each file within a tar file archive.
It's in the POSIX ustar format which is equivalent to using the following GNU tar flags:
tar --format=ustar
. It's called ustar because you're a star!
Warning: This class is not a complete tar implementation. It also offers no abstractions beyond the header format and has only been tested against very simple archives created in the ustar format and the default format for gnu and bsd tar. If your goal is to be able to read generic tar files, you should use a more mature tar implementation like Apache Commons Compress.
This class is only really useful in situations where the following statements are true:
- You want to create tar archives.
- You don't need to be able to read tar files from external sources.
- You don't want additional dependencies.
- You don't need fancy features like symbolic links.
- You want something that's a step above writing out the bytes by hand.
To create a tar archive using this class, you must do the following: For each file in the
archive, output a header and the file contents (null
-padded to the nearest 512-byte
boundary). Then output another 1024 null
bytes to indicate end of archive.
The ustar tar header contains the following fields:
- name
- [Offset: 0, Length: 100]
C String which we'll assume is UTF-8 (Offset: 0) - mode
- [Offset: 100, Length: 8]
ASCII 7-digit zero-padded octal file mode andnull
byte. - uid
- [Offset: 108, Length: 8]
ASCII 7-digit zero-padded octal UNIX user ID andnull
byte. - gid
- [Offset: 116, Length: 8]
ASCII 7-digit zero-padded octal UNIX group ID andnull
byte. - size
- [Offset: 124, Length: 12]
ASCII 11-digit zero-padded octal file size andnull
byte. - mtime
- [Offset: 136, Length: 12]
ASCII octal UNIX timestamp modified time andnull
byte. - chksum
- [Offset: 148, Length: 8]
ASCII octal sum of all header bytes where chksum are 0's. - typeflag
- [Offset: 156]
Always'0'
(zero character) for regular type of file. - linkname
- [Offset: 157, Length: 100]
Allnull
bytes because we don't support symbolic links. - magic
- [Offset: 257, Length: 6]
Always the C string "ustar". - version
- [Offset: 263, Length: 2]
Always "00" without anull
or blank on GNU systems. - uname
- [Offset: 265, Length: 32]
The C string UNIX user name corresponding touid
. - gname
- [Offset: 297, Length: 32]
The C string UNIX group name corresponding togid
. - devmajor
- [Offset: 329, Length: 8]
Not supported; set to zero. - devminor
- [Offset: 337, Length: 8]
Not supported; set to zero. - prefix
- [Offset: 345, Length: 155]
Not supported; set tonull
.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder forPosixTarHeader
.static enum
Type of file stored in the archive. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
static PosixTarHeader
from
(byte[] header) Create a new header from the bytes of an existing tar file header (Safe).byte[]
getBytes()
Returns 512-byte tar header (safe copy).int
Returns the checksum value stored in the .Returns thedevmajor
field.Returns thedevminor
field.int
getGid()
Returns the UNIX group id.getGname()
Returns the UNIX group name associated with the file.Returns the UNIX symbolic link name.getMagic()
Returns themagic
field.int
getMode()
Returns the octal UNIX mode aka permissions.org.joda.time.DateTime
getMtime()
Returns the modified time as a UTCDateTime
object.getName()
Returns the filename.Returns theprefix
field.int
getSize()
Returns the file size in bytes.getType()
Returns thePosixTarHeader.Type
of file.int
getUid()
Returns the UNIX owner/user id.getUname()
Returns the UNIX user name (or owner) of the file.Returns themagic
field.int
hashCode()
toString()
-
Field Details
-
HEADER_LENGTH
public static final int HEADER_LENGTH- See Also:
-
-
Method Details
-
from
Create a new header from the bytes of an existing tar file header (Safe).This routine validates the checksum to ensure the data header is correct and supported.
- Parameters:
header
- the existing and assumed correct header. Value is defensively copied.- Throws:
IllegalArgumentException
- if header isn't ustar or has a bad checksum.NullPointerException
- ifheader
isnull
.
-
getBytes
public byte[] getBytes()Returns 512-byte tar header (safe copy). -
getName
Returns the filename. -
getMode
public int getMode()Returns the octal UNIX mode aka permissions. -
getUid
public int getUid()Returns the UNIX owner/user id. -
getGid
public int getGid()Returns the UNIX group id. -
getSize
public int getSize()Returns the file size in bytes. -
getMtime
public org.joda.time.DateTime getMtime()Returns the modified time as a UTCDateTime
object. -
getChksum
public int getChksum()Returns the checksum value stored in the . -
getType
Returns thePosixTarHeader.Type
of file. -
getLinkName
Returns the UNIX symbolic link name.This feature is unsupported but the getter is included for completeness.
-
getMagic
Returns themagic
field. Only"ustar"
is supported. -
getVersion
Returns themagic
field. Only"00"
is supported. -
getUname
Returns the UNIX user name (or owner) of the file. -
getGname
Returns the UNIX group name associated with the file. -
getDevMajor
Returns thedevmajor
field.This feature is unsupported but the getter is included for completeness.
-
getDevMinor
Returns thedevminor
field.This feature is unsupported but the getter is included for completeness.
-
getPrefix
Returns theprefix
field.This feature is unsupported but the getter is included for completeness.
-
hashCode
public int hashCode() -
equals
-
toString
-