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

public Class TarArchiveOutputStream

extends ArchiveOutputStream<TarArchiveEntry>
Class Inheritance
Static Imports
java.nio.charset.StandardCharsets.UTF_8

The TarOutputStream writes a UNIX tar archive as an OutputStream. Methods are provided to put entries, and then write their contents by writing to this stream using write().

tar archives consist of a sequence of records of 512 bytes each that are grouped into blocks. Prior to Apache Commons Compress 1.14 it has been possible to configure a record size different from 512 bytes and arbitrary block sizes. Starting with Compress 1.15 512 is the only valid option for the record size and the block size must be a multiple of 512. Also the default block size changed from 10240 bytes prior to Compress 1.15 to 512 bytes with Compress 1.15.

Field Summary

Modifier and TypeField and Description
private boolean
private static final ZipEncoding
public static final int
BIGNUMBER_ERROR

Fail if a big number (e.g. size > 8GiB) is required in the archive.

public static final int
BIGNUMBER_POSIX

POSIX/PAX extensions are used to store big numbers in the archive.

public static final int
BIGNUMBER_STAR

star/GNU tar/BSD tar extensions are used to store big number in the archive.

private int
private static final int
pack-priv final String
charsetName

The provided encoding (for unit tests).

private boolean
private final CountingOutputStream
private long
private String
private long
private boolean
finished

indicates if this archive is finished

private boolean
haveUnclosedEntry

Indicates if putArchiveEntry has been called without closeArchiveEntry

public static final int
LONGFILE_ERROR

Fail if a long file name is required in the archive.

public static final int
LONGFILE_GNU

GNU tar extensions are used to store long file names in the archive.

public static final int
LONGFILE_POSIX

POSIX/PAX extensions are used to store long file names in the archive.

public static final int
LONGFILE_TRUNCATE

Long paths will be truncated in the archive.

private int
private final FixedLengthBlockOutputStream
private static final int
private final byte[]
private final int
private long
private final ZipEncoding

Constructor Summary

AccessConstructor and Description
public
TarArchiveOutputStream(final OutputStream
the output stream to use
os
)

Constructs a new instance.

public
TarArchiveOutputStream(final OutputStream
the output stream to use
os
,
final int
the block size to use. Must be a multiple of 512 bytes.
blockSize
)

Constructs a new instance.

public
TarArchiveOutputStream(final OutputStream
the output stream to use
os
,
final int
the block size to use
blockSize
,
final int
the record size to use. Must be 512 bytes.
recordSize
)

Deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used
Constructs a new instance.
public
TarArchiveOutputStream(final OutputStream
the output stream to use
os
,
final int
the block size to use . Must be a multiple of 512 bytes.
blockSize
,
final int
the record size to use. Must be 512 bytes.
recordSize
,
final String
name of the encoding to use for file names
encoding
)

Deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used.
Constructs a new instance.
public
TarArchiveOutputStream(final OutputStream
the output stream to use
os
,
final int
the block size to use. Must be a multiple of 512 bytes.
blockSize
,
final String
name of the encoding to use for file names
encoding
)

Constructs a new instance.

public
TarArchiveOutputStream(final OutputStream
the output stream to use
os
,
final String
name of the encoding to use for file names
encoding
)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
private void
addFileTimePaxHeader(final Map<String, String> paxHeaders, final String header, final FileTime value)

private void
addFileTimePaxHeaderForBigNumber(final Map<String, String> paxHeaders, final String header, final FileTime value, final long maxValue)

private void
addInstantPaxHeader(final Map<String, String> paxHeaders, final String header, final long seconds, final int nanos)

private void
addPaxHeaderForBigNumber(final Map<String, String> paxHeaders, final String header, final long value, final long maxValue)

private void
addPaxHeadersForBigNumbers(final Map<String, String> paxHeaders, final TarArchiveEntry entry)

public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes the underlying OutputStream.
public void
public TarArchiveEntry
createArchiveEntry(final File
the file to create the entry from
inputFile
,
final String
name to use for the entry
entryName
)

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates an archive entry using the inputFile and entryName provided.
public TarArchiveEntry
createArchiveEntry(final Path
the file to create the entry from
inputPath
,
final String
name to use for the entry
entryName
,
final LinkOption...
options indicating how symbolic links are handled.
options
)

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates an archive entry using the inputPath and entryName provided.
private byte[]
private void
failForBigNumber(final String field, final long value, final long maxValue)

private void
failForBigNumber(final String field, final long value, final long maxValue, final String additionalMsg)

private void
private void
failForBigNumberWithPosixMessage(final String field, final long value, final long maxValue)

public void
finish()

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.finish.

Finishes the TAR archive without closing the underlying OutputStream.
public void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.
public long
getBytesWritten()

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.getBytesWritten.

Gets the current number of bytes written to this stream.
public int
getCount()

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.getCount.

Deprecated
Gets the current number of bytes written to this stream.
public int

Returns:

The TarBuffer record size.
getRecordSize
()

Deprecated
Gets the record size being used by this stream's TarBuffer.
private boolean

Returns:

whether a pax header has been written.
handleLongName
(final TarArchiveEntry
entry the name belongs to
entry
,
final String
the name to write
name
,
final Map<String, String>
current map of pax headers
paxHeaders
,
final String
name of the pax header to write
paxHeaderName
,
final byte
type of the GNU entry to write
linkType
,
final String
the name of the field
fieldName
)

Handles long file or link names according to the longFileMode setting.

private void
public void
putArchiveEntry(final TarArchiveEntry
The TarEntry to be written to the archive.
archiveEntry
)

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.putArchiveEntry.

Puts an entry on the output stream.
public void
setAddPaxHeadersForNonAsciiNames(final boolean
whether to add a PAX extension header for non-ASCII file names.
b
)

Sets whether to add a PAX extension header for non-ASCII file names.

public void
setBigNumberMode(final int
the mode to use
bigNumberMode
)

Sets the big number mode.

public void
setLongFileMode(final int
the mode to use
longFileMode
)

Sets the long file mode.

private boolean

Returns:

true if the character could lead to problems when used inside a TarArchiveEntry name for a PAX header.
shouldBeReplaced
(final char c)

Tests whether the character could lead to problems when used inside a TarArchiveEntry name for a PAX header.

private String
stripTo7Bits(final String name)

private void
public void
write(final byte[]
The buffer to write to the archive.
wBuf
,
final int
The offset in the buffer from which to get bytes.
wOffset
,
final int
The number of bytes to write.
numToWrite
)

Overrides java.io.OutputStream.write.

Writes bytes to the current tar archive entry.
private void
writeEOFRecord()

Writes an EOF (end of archive) record to the tar archive.

pack-priv void
writePaxHeaders(final TarArchiveEntry entry, final String entryName, final Map<String, String> headers)

Writes a PAX extended header with the given map as contents.

private void
writeRecord(final byte[]
The record data to write to the archive.
record
)

Writes an archive record to the archive.

Inherited from org.apache.commons.compress.archivers.ArchiveOutputStream:
canWriteEntryDatacountcountwrite

Field Detail

addPaxHeadersForNonAsciiNamesback to summary
private boolean addPaxHeadersForNonAsciiNames
ASCIIback to summary
private static final ZipEncoding ASCII
BIGNUMBER_ERRORback to summary
public static final int BIGNUMBER_ERROR

Fail if a big number (e.g. size > 8GiB) is required in the archive.

BIGNUMBER_POSIXback to summary
public static final int BIGNUMBER_POSIX

POSIX/PAX extensions are used to store big numbers in the archive.

BIGNUMBER_STARback to summary
public static final int BIGNUMBER_STAR

star/GNU tar/BSD tar extensions are used to store big number in the archive.

bigNumberModeback to summary
private int bigNumberMode
BLOCK_SIZE_UNSPECIFIEDback to summary
private static final int BLOCK_SIZE_UNSPECIFIED
charsetNameback to summary
pack-priv final String charsetName

The provided encoding (for unit tests).

closedback to summary
private boolean closed
countingOutback to summary
private final CountingOutputStream countingOut
currBytesback to summary
private long currBytes
currNameback to summary
private String currName
currSizeback to summary
private long currSize
finishedback to summary
private boolean finished

indicates if this archive is finished

haveUnclosedEntryback to summary
private boolean haveUnclosedEntry

Indicates if putArchiveEntry has been called without closeArchiveEntry

LONGFILE_ERRORback to summary
public static final int LONGFILE_ERROR

Fail if a long file name is required in the archive.

LONGFILE_GNUback to summary
public static final int LONGFILE_GNU

GNU tar extensions are used to store long file names in the archive.

LONGFILE_POSIXback to summary
public static final int LONGFILE_POSIX

POSIX/PAX extensions are used to store long file names in the archive.

LONGFILE_TRUNCATEback to summary
public static final int LONGFILE_TRUNCATE

Long paths will be truncated in the archive.

longFileModeback to summary
private int longFileMode
outback to summary
private final FixedLengthBlockOutputStream out
RECORD_SIZEback to summary
private static final int RECORD_SIZE
recordBufback to summary
private final byte[] recordBuf
recordsPerBlockback to summary
private final int recordsPerBlock
recordsWrittenback to summary
private long recordsWritten
zipEncodingback to summary
private final ZipEncoding zipEncoding

Constructor Detail

TarArchiveOutputStreamback to summary
public TarArchiveOutputStream(final OutputStream os)

Constructs a new instance.

Uses a block size of 512 bytes.

Parameters
os:OutputStream

the output stream to use

TarArchiveOutputStreamback to summary
public TarArchiveOutputStream(final OutputStream os, final int blockSize)

Constructs a new instance.

Parameters
os:OutputStream

the output stream to use

blockSize:int

the block size to use. Must be a multiple of 512 bytes.

TarArchiveOutputStreamback to summary
public TarArchiveOutputStream(final OutputStream os, final int blockSize, final int recordSize)

Deprecated

recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used

Constructs a new instance.

Parameters
os:OutputStream

the output stream to use

blockSize:int

the block size to use

recordSize:int

the record size to use. Must be 512 bytes.

Annotations
@Deprecated
TarArchiveOutputStreamback to summary
public TarArchiveOutputStream(final OutputStream os, final int blockSize, final int recordSize, final String encoding)

Deprecated

recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used.

Constructs a new instance.

Parameters
os:OutputStream

the output stream to use

blockSize:int

the block size to use . Must be a multiple of 512 bytes.

recordSize:int

the record size to use. Must be 512 bytes.

encoding:String

name of the encoding to use for file names

Annotations
@Deprecated
Since
1.4
TarArchiveOutputStreamback to summary
public TarArchiveOutputStream(final OutputStream os, final int blockSize, final String encoding)

Constructs a new instance.

Parameters
os:OutputStream

the output stream to use

blockSize:int

the block size to use. Must be a multiple of 512 bytes.

encoding:String

name of the encoding to use for file names

Since
1.4
TarArchiveOutputStreamback to summary
public TarArchiveOutputStream(final OutputStream os, final String encoding)

Constructs a new instance.

Uses a block size of 512 bytes.

Parameters
os:OutputStream

the output stream to use

encoding:String

name of the encoding to use for file names

Since
1.4

Method Detail

addFileTimePaxHeaderback to summary
private void addFileTimePaxHeader(final Map<String, String> paxHeaders, final String header, final FileTime value)
addFileTimePaxHeaderForBigNumberback to summary
private void addFileTimePaxHeaderForBigNumber(final Map<String, String> paxHeaders, final String header, final FileTime value, final long maxValue)
addInstantPaxHeaderback to summary
private void addInstantPaxHeader(final Map<String, String> paxHeaders, final String header, final long seconds, final int nanos)
addPaxHeaderForBigNumberback to summary
private void addPaxHeaderForBigNumber(final Map<String, String> paxHeaders, final String header, final long value, final long maxValue)
addPaxHeadersForBigNumbersback to summary
private void addPaxHeadersForBigNumbers(final Map<String, String> paxHeaders, final TarArchiveEntry entry)
closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes the underlying OutputStream.

Annotations
@Override
Exceptions
IOException:
on error
closeArchiveEntryback to summary
public void closeArchiveEntry() throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.closeArchiveEntry.

Closes an entry. This method MUST be called for all file entries that contain data. The reason is that we must buffer data written to the stream in order to satisfy the buffer's record based writes. Thus, there may be data fragments still being assembled that must be written to the output stream before this entry is closed and the next entry written.

Annotations
@Override
Exceptions
IOException:
on error
createArchiveEntryback to summary
public TarArchiveEntry createArchiveEntry(final File inputFile, final String entryName) throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates an archive entry using the inputFile and entryName provided.

Parameters
inputFile:File

the file to create the entry from

entryName:String

name to use for the entry

Returns:TarArchiveEntry

the ArchiveEntry set up with details from the file

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
createArchiveEntryback to summary
public TarArchiveEntry createArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates an archive entry using the inputPath and entryName provided.

The default implementation calls simply delegates as:

return createArchiveEntry(inputFile.toFile(), entryName);

Subclasses should override this method.

Parameters
inputPath:Path

the file to create the entry from

entryName:String

name to use for the entry

options:LinkOption[]

options indicating how symbolic links are handled.

Returns:TarArchiveEntry

the ArchiveEntry set up with details from the file

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
encodeExtendedPaxHeadersContentsback to summary
private byte[] encodeExtendedPaxHeadersContents(final Map<String, String> headers)
failForBigNumberback to summary
private void failForBigNumber(final String field, final long value, final long maxValue)
failForBigNumberback to summary
private void failForBigNumber(final String field, final long value, final long maxValue, final String additionalMsg)
failForBigNumbersback to summary
private void failForBigNumbers(final TarArchiveEntry entry)
failForBigNumberWithPosixMessageback to summary
private void failForBigNumberWithPosixMessage(final String field, final long value, final long maxValue)
finishback to summary
public void finish() throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.finish.

Finishes the TAR archive without closing the underlying OutputStream. An archive consists of a series of file entries terminated by an end-of-archive entry, which consists of two 512 blocks of zero bytes. POSIX.1 requires two EOF records, like some other implementations.

Annotations
@Override
Exceptions
IOException:
on error
flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
getBytesWrittenback to summary
public long getBytesWritten()

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.getBytesWritten.

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.getBytesWritten.

Gets the current number of bytes written to this stream.

Returns:long

the number of written bytes

Annotations
@Override
getCountback to summary
public int getCount()

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.getCount.

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.getCount.

Deprecated

Gets the current number of bytes written to this stream.

Returns:int

the number of written bytes

Annotations
@Deprecated
@Override
getRecordSizeback to summary
public int getRecordSize()

Deprecated

Gets the record size being used by this stream's TarBuffer.

Returns:int

The TarBuffer record size.

Annotations
@Deprecated
handleLongNameback to summary
private boolean handleLongName(final TarArchiveEntry entry, final String name, final Map<String, String> paxHeaders, final String paxHeaderName, final byte linkType, final String fieldName) throws IOException

Handles long file or link names according to the longFileMode setting.

I.e. if the given name is too long to be written to a plain tar header then

  • it creates a pax header who's name is given by the paxHeaderName parameter if longFileMode is POSIX
  • it creates a GNU longlink entry who's type is given by the linkType parameter if longFileMode is GNU
  • it throws an exception if longFileMode is ERROR
  • it truncates the name if longFileMode is TRUNCATE

Parameters
entry:TarArchiveEntry

entry the name belongs to

name:String

the name to write

paxHeaders:Map<String, String>

current map of pax headers

paxHeaderName:String

name of the pax header to write

linkType:byte

type of the GNU entry to write

fieldName:String

the name of the field

Returns:boolean

whether a pax header has been written.

Exceptions
IllegalArgumentException:
if the TarArchiveOutputStream#longFileMode equals TarArchiveOutputStream#LONGFILE_ERROR and the file name is too long
padAsNeededback to summary
private void padAsNeeded() throws IOException
putArchiveEntryback to summary
public void putArchiveEntry(final TarArchiveEntry archiveEntry) throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.putArchiveEntry.

Puts an entry on the output stream. This writes the entry's header record and positions the output stream for writing the contents of the entry. Once this method is called, the stream is ready for calls to write() to write the entry's contents. Once the contents are written, closeArchiveEntry() MUST be called to ensure that all buffered data is completely written to the output stream.

Parameters
archiveEntry:TarArchiveEntry

The TarEntry to be written to the archive.

Annotations
@Override
Exceptions
IOException:
on error
ClassCastException:
if archiveEntry is not an instance of TarArchiveEntry
IllegalArgumentException:
setAddPaxHeadersForNonAsciiNamesback to summary
public void setAddPaxHeadersForNonAsciiNames(final boolean b)

Sets whether to add a PAX extension header for non-ASCII file names.

Parameters
b:boolean

whether to add a PAX extension header for non-ASCII file names.

Since
1.4
setBigNumberModeback to summary
public void setBigNumberMode(final int bigNumberMode)

Sets the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_STAR(1) or BIGNUMBER_POSIX(2). This specifies the treatment of big files (sizes > TarConstants.MAXSIZE) and other numeric values too big to fit into a traditional tar header. Default is BIGNUMBER_ERROR.

Parameters
bigNumberMode:int

the mode to use

Since
1.4
setLongFileModeback to summary
public void setLongFileMode(final int longFileMode)

Sets the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1), LONGFILE_GNU(2) or LONGFILE_POSIX(3). This specifies the treatment of long file names (names >= TarConstants.NAMELEN). Default is LONGFILE_ERROR.

Parameters
longFileMode:int

the mode to use

shouldBeReplacedback to summary
private boolean shouldBeReplaced(final char c)

Tests whether the character could lead to problems when used inside a TarArchiveEntry name for a PAX header.

Returns:boolean

true if the character could lead to problems when used inside a TarArchiveEntry name for a PAX header.

stripTo7Bitsback to summary
private String stripTo7Bits(final String name)
transferModTimeback to summary
private void transferModTime(final TarArchiveEntry from, final TarArchiveEntry to)
writeback to summary
public void write(final byte[] wBuf, final int wOffset, final int numToWrite) throws IOException

Overrides java.io.OutputStream.write.

Writes bytes to the current tar archive entry. This method is aware of the current entry and will throw an exception if you attempt to write bytes past the length specified for the current entry.

Parameters
wBuf:byte[]

The buffer to write to the archive.

wOffset:int

The offset in the buffer from which to get bytes.

numToWrite:int

The number of bytes to write.

Annotations
@Override
Exceptions
IOException:
on error
writeEOFRecordback to summary
private void writeEOFRecord() throws IOException

Writes an EOF (end of archive) record to the tar archive. An EOF record consists of a record of all zeros.

writePaxHeadersback to summary
pack-priv void writePaxHeaders(final TarArchiveEntry entry, final String entryName, final Map<String, String> headers) throws IOException

Writes a PAX extended header with the given map as contents.

Since
1.4
writeRecordback to summary
private void writeRecord(final byte[] record) throws IOException

Writes an archive record to the archive.

Parameters
record:byte[]

The record data to write to the archive.

Exceptions
IOException:
on error