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

public Class TarUtils

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .IOException, .InputStream, .UncheckedIOException, java.math.BigInteger, java.nio.ByteBuffer, java.nio.charset.Charset, .StandardCharsets, java.util.ArrayList, .Collections, .HashMap, .List, .Map, org.apache.commons.compress.archivers.zip.ZipEncoding, .ZipEncodingHelper, org.apache.commons.compress.utils.CharsetNames, .IOUtils, .ParsingUtils

This class provides static utility methods to work with byte streams.

Field Summary

Modifier and TypeField and Description
private static final int
pack-priv static final ZipEncoding
pack-priv static final ZipEncoding
FALLBACK_ENCODING

Encapsulates the algorithms used up to Commons Compress 1.3 as ZipEncoding.

Constructor Summary

AccessConstructor and Description
private
TarUtils()

Prevents instantiation.

Method Summary

Modifier and TypeMethod and Description
public static long

Returns:

The computed checksum.
computeCheckSum
(final byte[]
The tar entry's header buffer.
buf
)

Computes the checksum of a tar entry header.

private static String
exceptionMessage(final byte[] buffer, final int offset, final int length, final int current, final byte currentByte)

private static void
formatBigIntegerBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative)

public static int

Returns:

The updated value of offset, i.e. offset+length
formatCheckSumOctalBytes
(final long
The value to convert
value
,
final byte[]
The destination buffer
buf
,
final int
The starting offset into the buffer.
offset
,
final int
The size of the buffer.
length
)

Writes an octal value into a buffer.

private static void
formatLongBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative)

public static int

Returns:

The updated offset
formatLongOctalBytes
(final long
The value to write as octal
value
,
final byte[]
The destinationbuffer.
buf
,
final int
The starting offset into the buffer.
offset
,
final int
The length of the buffer
length
)

Writes an octal long integer into a buffer.

public static int

Returns:

The updated offset.
formatLongOctalOrBinaryBytes
(final long
The value to write into the buffer.
value
,
final byte[]
The destination buffer.
buf
,
final int
The starting offset into the buffer.
offset
,
final int
The length of the buffer.
length
)

Writes a long integer into a buffer as an octal string if this will fit, or as a binary number otherwise.

public static int

Returns:

The updated offset, i.e. offset + length
formatNameBytes
(final String
The header name from which to copy the characters.
name
,
final byte[]
The buffer where the name is to be stored.
buf
,
final int
The starting offset into the buffer
offset
,
final int
The maximum number of header bytes to copy.
length
)

Copies a name into a buffer.

public static int

Returns:

The updated offset, i.e. offset + length
formatNameBytes
(final String
The header name from which to copy the characters.
name
,
final byte[]
The buffer where the name is to be stored.
buf
,
final int
The starting offset into the buffer
offset
,
final int
The maximum number of header bytes to copy.
length
,
final ZipEncoding
name of the encoding to use for file names
encoding
)

Copies a name into a buffer.

public static int

Returns:

The updated offset, i.e. offset+length
formatOctalBytes
(final long
The value to write
value
,
final byte[]
The buffer to receive the output
buf
,
final int
The starting offset into the buffer
offset
,
final int
The size of the output buffer
length
)

Writes an octal integer into a buffer.

public static void
formatUnsignedOctalString(final long
number to convert to octal - treated as unsigned
value
,
final byte[]
destination buffer
buffer
,
final int
starting offset in buffer
offset
,
final int
length of buffer to fill
length
)

Fills a buffer with unsigned octal number, padded with leading zeroes.

private static long
parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative)

private static long
parseBinaryLong(final byte[] buffer, final int offset, final int length, final boolean negative)

public static boolean

Returns:

The boolean value of the bytes.
parseBoolean
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
)

Parses a boolean byte from a buffer.

protected static List<TarArchiveStructSparse>

Returns:

unmodifiable list of sparse headers parsed from sparse map
parseFromPAX01SparseHeaders
(final String
the sparse map string consisting of comma-separated values "offset,size[,offset-1,size-1...]"
sparseMap
)

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.

public static String

Returns:

The entry name.
parseName
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
,
final int
The maximum number of bytes to parse.
length
)

Parses an entry name from a buffer.

public static String

Returns:

The entry name.
parseName
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
,
final int
The maximum number of bytes to parse.
length
,
final ZipEncoding
name of the encoding to use for file names
encoding
)

Parses an entry name from a buffer.

public static long

Returns:

The long value of the octal string.
parseOctal
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
,
final int
The maximum number of bytes to parse - must be at least 2 bytes.
length
)

Parses an octal string from a buffer.

public static long

Returns:

The long value of the octal or binary string.
parseOctalOrBinary
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
,
final int
The maximum number of bytes to parse.
length
)

Computes the value contained in a byte buffer.

protected static List<TarArchiveStructSparse>

Returns:

sparse headers parsed from sparse map
parsePAX01SparseHeaders
(final String
the sparse map string consisting of comma-separated values "offset,size[,offset-1,size-1...]"
sparseMap
)

Deprecated use #parseFromPAX01SparseHeaders instead
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.

protected static List<TarArchiveStructSparse>

Returns:

sparse headers
parsePAX1XSparseHeaders
(final InputStream
parsing source.
inputStream
,
final int
The size the TAR header
recordSize
)

For PAX Format 1.X: The sparse map itself is stored in the file data block, preceding the actual file data.

protected static Map<String, String>

Returns:

map of PAX headers values found inside the current (local or global) PAX headers tar entry.
parsePaxHeaders
(final InputStream
input stream to read keys and values
inputStream
,
final List<TarArchiveStructSparse>
used in PAX Format 0.0 & 0.1, as it may appear multiple times, the sparse headers need to be stored in an array, not a map
sparseHeaders
,
final Map<String, String>
global PAX headers of the tar archive
globalPaxHeaders
)

Deprecated use the four-arg version instead
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
protected static Map<String, String>

Returns:

map of PAX headers values found inside the current (local or global) PAX headers tar entry.
parsePaxHeaders
(final InputStream
input stream to read keys and values
inputStream
,
final List<TarArchiveStructSparse>
used in PAX Format 0.0 & 0.1, as it may appear multiple times, the sparse headers need to be stored in an array, not a map
sparseHeaders
,
final Map<String, String>
global PAX headers of the tar archive
globalPaxHeaders
,
final long
total size of the PAX header, will be ignored if negative
headerSize
)

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
public static TarArchiveStructSparse

Returns:

a parsed sparse struct
parseSparse
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
)

Parses the content of a PAX 1.0 sparse block.

private static long[]

Returns:

the decimal number delimited by '\n', and the bytes read from input stream
readLineOfNumberForPax1X
(final InputStream
the input stream of the tar file
inputStream
)

For 1.X PAX Format, the sparse headers are stored in the file data block, preceding the actual file data.

pack-priv static List<TarArchiveStructSparse>
readSparseStructs(final byte[] buffer, final int offset, final int entries)

public static boolean

Returns:

whether the checksum is reasonably good
verifyCheckSum
(final byte[]
tar header
header
)

Wikipedia says:

The checksum is calculated by taking the sum of the unsigned byte values of the header block with the eight checksum bytes taken to be ASCII spaces (decimal value 32). It is stored as a six digit octal number with leading zeroes followed by a NUL and then a space. Various implementations do not adhere to this format. For better compatibility, ignore leading and trailing whitespace, and get the first six digits. In addition, some historic tar implementations treated bytes as signed. Implementations typically calculate the checksum both ways, and treat it as good if either the signed or unsigned sum matches the included checksum.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BYTE_MASKback to summary
private static final int BYTE_MASK
DEFAULT_ENCODINGback to summary
pack-priv static final ZipEncoding DEFAULT_ENCODING
FALLBACK_ENCODINGback to summary
pack-priv static final ZipEncoding FALLBACK_ENCODING

Encapsulates the algorithms used up to Commons Compress 1.3 as ZipEncoding.

Constructor Detail

TarUtilsback to summary
private TarUtils()

Prevents instantiation.

Method Detail

computeCheckSumback to summary
public static long computeCheckSum(final byte[] buf)

Computes the checksum of a tar entry header.

Parameters
buf:byte[]

The tar entry's header buffer.

Returns:long

The computed checksum.

exceptionMessageback to summary
private static String exceptionMessage(final byte[] buffer, final int offset, final int length, final int current, final byte currentByte)
formatBigIntegerBinaryback to summary
private static void formatBigIntegerBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative)
formatCheckSumOctalBytesback to summary
public static int formatCheckSumOctalBytes(final long value, final byte[] buf, final int offset, final int length)

Writes an octal value into a buffer. Uses formatUnsignedOctalString to format the value as an octal string with leading zeros. The converted number is followed by NUL and then space.

Parameters
value:long

The value to convert

buf:byte[]

The destination buffer

offset:int

The starting offset into the buffer.

length:int

The size of the buffer.

Returns:int

The updated value of offset, i.e. offset+length

Exceptions
IllegalArgumentException:
if the value (and trailer) will not fit in the buffer
formatLongBinaryback to summary
private static void formatLongBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative)
formatLongOctalBytesback to summary
public static int formatLongOctalBytes(final long value, final byte[] buf, final int offset, final int length)

Writes an octal long integer into a buffer. Uses formatUnsignedOctalString to format the value as an octal string with leading zeros. The converted number is followed by a space.

Parameters
value:long

The value to write as octal

buf:byte[]

The destinationbuffer.

offset:int

The starting offset into the buffer.

length:int

The length of the buffer

Returns:int

The updated offset

Exceptions
IllegalArgumentException:
if the value (and trailer) will not fit in the buffer
formatLongOctalOrBinaryBytesback to summary
public static int formatLongOctalOrBinaryBytes(final long value, final byte[] buf, final int offset, final int length)

Writes a long integer into a buffer as an octal string if this will fit, or as a binary number otherwise. Uses formatUnsignedOctalString to format the value as an octal string with leading zeros. The converted number is followed by a space.

Parameters
value:long

The value to write into the buffer.

buf:byte[]

The destination buffer.

offset:int

The starting offset into the buffer.

length:int

The length of the buffer.

Returns:int

The updated offset.

Exceptions
IllegalArgumentException:
if the value (and trailer) will not fit in the buffer.
Since
1.4
formatNameBytesback to summary
public static int formatNameBytes(final String name, final byte[] buf, final int offset, final int length)

Copies a name into a buffer. Copies characters from the name into the buffer starting at the specified offset. If the buffer is longer than the name, the buffer is filled with trailing NULs. If the name is longer than the buffer, the output is truncated.

Parameters
name:String

The header name from which to copy the characters.

buf:byte[]

The buffer where the name is to be stored.

offset:int

The starting offset into the buffer

length:int

The maximum number of header bytes to copy.

Returns:int

The updated offset, i.e. offset + length

formatNameBytesback to summary
public static int formatNameBytes(final String name, final byte[] buf, final int offset, final int length, final ZipEncoding encoding) throws IOException

Copies a name into a buffer. Copies characters from the name into the buffer starting at the specified offset. If the buffer is longer than the name, the buffer is filled with trailing NULs. If the name is longer than the buffer, the output is truncated.

Parameters
name:String

The header name from which to copy the characters.

buf:byte[]

The buffer where the name is to be stored.

offset:int

The starting offset into the buffer

length:int

The maximum number of header bytes to copy.

encoding:ZipEncoding

name of the encoding to use for file names

Returns:int

The updated offset, i.e. offset + length

Exceptions
IOException:
on error
Since
1.4
formatOctalBytesback to summary
public static int formatOctalBytes(final long value, final byte[] buf, final int offset, final int length)

Writes an octal integer into a buffer. Uses formatUnsignedOctalString to format the value as an octal string with leading zeros. The converted number is followed by space and NUL

Parameters
value:long

The value to write

buf:byte[]

The buffer to receive the output

offset:int

The starting offset into the buffer

length:int

The size of the output buffer

Returns:int

The updated offset, i.e. offset+length

Exceptions
IllegalArgumentException:
if the value (and trailer) will not fit in the buffer
formatUnsignedOctalStringback to summary
public static void formatUnsignedOctalString(final long value, final byte[] buffer, final int offset, final int length)

Fills a buffer with unsigned octal number, padded with leading zeroes.

Parameters
value:long

number to convert to octal - treated as unsigned

buffer:byte[]

destination buffer

offset:int

starting offset in buffer

length:int

length of buffer to fill

Exceptions
IllegalArgumentException:
if the value will not fit in the buffer
parseBinaryBigIntegerback to summary
private static long parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative)
parseBinaryLongback to summary
private static long parseBinaryLong(final byte[] buffer, final int offset, final int length, final boolean negative)
parseBooleanback to summary
public static boolean parseBoolean(final byte[] buffer, final int offset)

Parses a boolean byte from a buffer. Leading spaces and NUL are ignored. The buffer may contain trailing spaces or NULs.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

Returns:boolean

The boolean value of the bytes.

Exceptions
IllegalArgumentException:
if an invalid byte is detected.
parseFromPAX01SparseHeadersback to summary
protected static List<TarArchiveStructSparse> parseFromPAX01SparseHeaders(final String sparseMap) throws IOException

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...]"

Parameters
sparseMap:String

the sparse map string consisting of comma-separated values "offset,size[,offset-1,size-1...]"

Returns:List<TarArchiveStructSparse>

unmodifiable list of sparse headers parsed from sparse map

Exceptions
IOException:
Corrupted TAR archive.
Since
1.21
parseNameback to summary
public static String parseName(final byte[] buffer, final int offset, final int length)

Parses an entry name from a buffer. Parsing stops when a NUL is found or the buffer length is reached.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

length:int

The maximum number of bytes to parse.

Returns:String

The entry name.

parseNameback to summary
public static String parseName(final byte[] buffer, final int offset, final int length, final ZipEncoding encoding) throws IOException

Parses an entry name from a buffer. Parsing stops when a NUL is found or the buffer length is reached.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

length:int

The maximum number of bytes to parse.

encoding:ZipEncoding

name of the encoding to use for file names

Returns:String

The entry name.

Exceptions
IOException:
on error
Since
1.4
parseOctalback to summary
public static long parseOctal(final byte[] buffer, final int offset, final int length)

Parses an octal string from a buffer.

Leading spaces are ignored. The buffer must contain a trailing space or NUL, and may contain an additional trailing space or NUL.

The input buffer is allowed to contain all NULs, in which case the method returns 0L (this allows for missing fields).

To work-around some tar implementations that insert a leading NUL this method returns 0 if it detects a leading NUL since Commons Compress 1.4.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

length:int

The maximum number of bytes to parse - must be at least 2 bytes.

Returns:long

The long value of the octal string.

Exceptions
IllegalArgumentException:
if the trailing space/NUL is missing or if an invalid byte is detected.
parseOctalOrBinaryback to summary
public static long parseOctalOrBinary(final byte[] buffer, final int offset, final int length)

Computes the value contained in a byte buffer. If the most significant bit of the first byte in the buffer is set, this bit is ignored and the rest of the buffer is interpreted as a binary number. Otherwise, the buffer is interpreted as an octal number as per the parseOctal function above.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

length:int

The maximum number of bytes to parse.

Returns:long

The long value of the octal or binary string.

Exceptions
IllegalArgumentException:
if the trailing space/NUL is missing or an invalid byte is detected in an octal number, or if a binary number would exceed the size of a signed long 64-bit integer.
Since
1.4
parsePAX01SparseHeadersback to summary
protected static List<TarArchiveStructSparse> parsePAX01SparseHeaders(final String sparseMap)

Deprecated

use #parseFromPAX01SparseHeaders instead

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...]"

Will internally invoke parseFromPAX01SparseHeaders and map IOExceptions to a RzuntimeException, You should use parseFromPAX01SparseHeaders directly instead.

Parameters
sparseMap:String

the sparse map string consisting of comma-separated values "offset,size[,offset-1,size-1...]"

Returns:List<TarArchiveStructSparse>

sparse headers parsed from sparse map

Annotations
@Deprecated
parsePAX1XSparseHeadersback to summary
protected static List<TarArchiveStructSparse> parsePAX1XSparseHeaders(final InputStream inputStream, final int recordSize) throws IOException

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.

Parameters
inputStream:InputStream

parsing source.

recordSize:int

The size the TAR header

Returns:List<TarArchiveStructSparse>

sparse headers

Exceptions
IOException:
if an I/O error occurs.
parsePaxHeadersback to summary
protected static Map<String, String> parsePaxHeaders(final InputStream inputStream, final List<TarArchiveStructSparse> sparseHeaders, final Map<String, String> globalPaxHeaders) throws IOException

Deprecated

use the four-arg version instead

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...]"

Parameters
inputStream:InputStream

input stream to read keys and values

sparseHeaders:List<TarArchiveStructSparse>

used in PAX Format 0.0 & 0.1, as it may appear multiple times, the sparse headers need to be stored in an array, not a map

globalPaxHeaders:Map<String, String>

global PAX headers of the tar archive

Returns:Map<String, String>

map of PAX headers values found inside the current (local or global) PAX headers tar entry.

Annotations
@Deprecated
Exceptions
IOException:
if an I/O error occurs.
parsePaxHeadersback to summary
protected static Map<String, String> parsePaxHeaders(final InputStream inputStream, final List<TarArchiveStructSparse> sparseHeaders, final Map<String, String> globalPaxHeaders, final long headerSize) 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...]"

Parameters
inputStream:InputStream

input stream to read keys and values

sparseHeaders:List<TarArchiveStructSparse>

used in PAX Format 0.0 & 0.1, as it may appear multiple times, the sparse headers need to be stored in an array, not a map

globalPaxHeaders:Map<String, String>

global PAX headers of the tar archive

headerSize:long

total size of the PAX header, will be ignored if negative

Returns:Map<String, String>

map of PAX headers values found inside the current (local or global) PAX headers tar entry.

Exceptions
IOException:
if an I/O error occurs.
Since
1.21
parseSparseback to summary
public static TarArchiveStructSparse parseSparse(final byte[] buffer, final int offset)

Parses the content of a PAX 1.0 sparse block.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

Returns:TarArchiveStructSparse

a parsed sparse struct

Since
1.20
readLineOfNumberForPax1Xback to summary
private static long[] readLineOfNumberForPax1X(final InputStream inputStream) throws IOException

For 1.X PAX Format, the sparse headers are stored in the file data block, preceding the actual file data. It consists of a series of decimal numbers delimited by newlines.

Parameters
inputStream:InputStream

the input stream of the tar file

Returns:long[]

the decimal number delimited by '\n', and the bytes read from input stream

readSparseStructsback to summary
pack-priv static List<TarArchiveStructSparse> readSparseStructs(final byte[] buffer, final int offset, final int entries) throws IOException
Since
1.21
verifyCheckSumback to summary
public static boolean verifyCheckSum(final byte[] header)

Wikipedia says:

The checksum is calculated by taking the sum of the unsigned byte values of the header block with the eight checksum bytes taken to be ASCII spaces (decimal value 32). It is stored as a six digit octal number with leading zeroes followed by a NUL and then a space. Various implementations do not adhere to this format. For better compatibility, ignore leading and trailing whitespace, and get the first six digits. In addition, some historic tar implementations treated bytes as signed. Implementations typically calculate the checksum both ways, and treat it as good if either the signed or unsigned sum matches the included checksum.

The return value of this method should be treated as a best-effort heuristic rather than an absolute and final truth. The checksum verification logic may well evolve over time as more special cases are encountered.

Parameters
header:byte[]

tar header

Returns:boolean

whether the checksum is reasonably good

Since
1.5
See Also
COMPRESS-191