public class TarArchiveOutputStream extends ArchiveOutputStream
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.
Modifier and Type | Field and Description |
---|---|
private boolean |
addPaxHeadersForNonAsciiNames |
private static ZipEncoding |
ASCII |
static int |
BIGNUMBER_ERROR
Fail if a big number (e.g.
|
static int |
BIGNUMBER_POSIX
POSIX/PAX extensions are used to store big numbers in the archive.
|
static int |
BIGNUMBER_STAR
star/GNU tar/BSD tar extensions are used to store big number in the archive.
|
private int |
bigNumberMode |
private static int |
BLOCK_SIZE_UNSPECIFIED |
private boolean |
closed |
private CountingOutputStream |
countingOut |
private long |
currBytes |
private java.lang.String |
currName |
private long |
currSize |
(package private) java.lang.String |
encoding |
private boolean |
finished
indicates if this archive is finished
|
private boolean |
haveUnclosedEntry
Indicates if putArchiveEntry has been called without closeArchiveEntry
|
static int |
LONGFILE_ERROR
Fail if a long file name is required in the archive.
|
static int |
LONGFILE_GNU
GNU tar extensions are used to store long file names in the archive.
|
static int |
LONGFILE_POSIX
POSIX/PAX extensions are used to store long file names in the archive.
|
static int |
LONGFILE_TRUNCATE
Long paths will be truncated in the archive.
|
private int |
longFileMode |
private FixedLengthBlockOutputStream |
out |
private static int |
RECORD_SIZE |
private byte[] |
recordBuf |
private int |
recordsPerBlock |
private int |
recordsWritten |
private ZipEncoding |
zipEncoding |
Constructor and Description |
---|
TarArchiveOutputStream(java.io.OutputStream os)
Constructor for TarArchiveOutputStream.
|
TarArchiveOutputStream(java.io.OutputStream os,
int blockSize)
Constructor for TarArchiveOutputStream.
|
TarArchiveOutputStream(java.io.OutputStream os,
int blockSize,
int recordSize)
Deprecated.
recordSize must always be 512 bytes. An IllegalArgumentException will be thrown
if any other value is used
|
TarArchiveOutputStream(java.io.OutputStream os,
int blockSize,
int recordSize,
java.lang.String encoding)
Deprecated.
recordSize must always be 512 bytes. An IllegalArgumentException will be thrown
if any other value is used.
|
TarArchiveOutputStream(java.io.OutputStream os,
int blockSize,
java.lang.String encoding)
Constructor for TarArchiveOutputStream.
|
TarArchiveOutputStream(java.io.OutputStream os,
java.lang.String encoding)
Constructor for TarArchiveOutputStream.
|
Modifier and Type | Method and Description |
---|---|
private void |
addPaxHeaderForBigNumber(java.util.Map<java.lang.String,java.lang.String> paxHeaders,
java.lang.String header,
long value,
long maxValue) |
private void |
addPaxHeadersForBigNumbers(java.util.Map<java.lang.String,java.lang.String> paxHeaders,
TarArchiveEntry entry) |
void |
close()
Closes the underlying OutputStream.
|
void |
closeArchiveEntry()
Close an entry.
|
ArchiveEntry |
createArchiveEntry(java.io.File inputFile,
java.lang.String entryName)
Create an archive entry using the inputFile and entryName provided.
|
private byte[] |
encodeExtendedPaxHeadersContents(java.util.Map<java.lang.String,java.lang.String> headers) |
private void |
failForBigNumber(java.lang.String field,
long value,
long maxValue) |
private void |
failForBigNumber(java.lang.String field,
long value,
long maxValue,
java.lang.String additionalMsg) |
private void |
failForBigNumbers(TarArchiveEntry entry) |
private void |
failForBigNumberWithPosixMessage(java.lang.String field,
long value,
long maxValue) |
void |
finish()
Ends the TAR archive without closing the underlying OutputStream.
|
void |
flush() |
long |
getBytesWritten()
Returns the current number of bytes written to this stream.
|
int |
getCount()
Deprecated.
|
int |
getRecordSize()
Deprecated.
|
private boolean |
handleLongName(TarArchiveEntry entry,
java.lang.String name,
java.util.Map<java.lang.String,java.lang.String> paxHeaders,
java.lang.String paxHeaderName,
byte linkType,
java.lang.String fieldName)
Handles long file or link names according to the longFileMode setting.
|
private void |
padAsNeeded() |
void |
putArchiveEntry(ArchiveEntry archiveEntry)
Put an entry on the output stream.
|
void |
setAddPaxHeadersForNonAsciiNames(boolean b)
Whether to add a PAX extension header for non-ASCII file names.
|
void |
setBigNumberMode(int bigNumberMode)
Set the big number mode.
|
void |
setLongFileMode(int longFileMode)
Set the long file mode.
|
private boolean |
shouldBeReplaced(char c) |
private java.lang.String |
stripTo7Bits(java.lang.String name) |
private void |
transferModTime(TarArchiveEntry from,
TarArchiveEntry to) |
void |
write(byte[] wBuf,
int wOffset,
int numToWrite)
Writes bytes to the current tar archive entry.
|
private void |
writeEOFRecord()
Write an EOF (end of archive) record to the tar archive.
|
(package private) void |
writePaxHeaders(TarArchiveEntry entry,
java.lang.String entryName,
java.util.Map<java.lang.String,java.lang.String> headers)
Writes a PAX extended header with the given map as contents.
|
private void |
writeRecord(byte[] record)
Write an archive record to the archive.
|
canWriteEntryData, count, count, write
public static final int LONGFILE_ERROR
public static final int LONGFILE_TRUNCATE
public static final int LONGFILE_GNU
public static final int LONGFILE_POSIX
public static final int BIGNUMBER_ERROR
public static final int BIGNUMBER_STAR
public static final int BIGNUMBER_POSIX
private static final int RECORD_SIZE
private long currSize
private java.lang.String currName
private long currBytes
private final byte[] recordBuf
private int longFileMode
private int bigNumberMode
private int recordsWritten
private final int recordsPerBlock
private boolean closed
private boolean haveUnclosedEntry
private boolean finished
private final FixedLengthBlockOutputStream out
private final CountingOutputStream countingOut
private final ZipEncoding zipEncoding
final java.lang.String encoding
private boolean addPaxHeadersForNonAsciiNames
private static final ZipEncoding ASCII
private static final int BLOCK_SIZE_UNSPECIFIED
public TarArchiveOutputStream(java.io.OutputStream os)
Uses a block size of 512 bytes.
os
- the output stream to usepublic TarArchiveOutputStream(java.io.OutputStream os, java.lang.String encoding)
Uses a block size of 512 bytes.
os
- the output stream to useencoding
- name of the encoding to use for file namespublic TarArchiveOutputStream(java.io.OutputStream os, int blockSize)
os
- the output stream to useblockSize
- the block size to use. Must be a multiple of 512 bytes.@Deprecated public TarArchiveOutputStream(java.io.OutputStream os, int blockSize, int recordSize)
os
- the output stream to useblockSize
- the block size to userecordSize
- the record size to use. Must be 512 bytes.@Deprecated public TarArchiveOutputStream(java.io.OutputStream os, int blockSize, int recordSize, java.lang.String encoding)
os
- the output stream to useblockSize
- the block size to use . Must be a multiple of 512 bytes.recordSize
- the record size to use. Must be 512 bytes.encoding
- name of the encoding to use for file namespublic TarArchiveOutputStream(java.io.OutputStream os, int blockSize, java.lang.String encoding)
os
- the output stream to useblockSize
- the block size to use. Must be a multiple of 512 bytes.encoding
- name of the encoding to use for file namespublic void setLongFileMode(int longFileMode)
longFileMode
- the mode to usepublic void setBigNumberMode(int bigNumberMode)
bigNumberMode
- the mode to usepublic void setAddPaxHeadersForNonAsciiNames(boolean b)
b
- whether to add a PAX extension header for non-ASCII file names.@Deprecated public int getCount()
ArchiveOutputStream
getCount
in class ArchiveOutputStream
public long getBytesWritten()
ArchiveOutputStream
getBytesWritten
in class ArchiveOutputStream
public void finish() throws java.io.IOException
finish
in class ArchiveOutputStream
java.io.IOException
- on errorpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
- on error@Deprecated public int getRecordSize()
public void putArchiveEntry(ArchiveEntry archiveEntry) throws java.io.IOException
putArchiveEntry
in class ArchiveOutputStream
archiveEntry
- The TarEntry to be written to the archive.java.io.IOException
- on errorjava.lang.ClassCastException
- if archiveEntry is not an instance of TarArchiveEntrypublic void closeArchiveEntry() throws java.io.IOException
closeArchiveEntry
in class ArchiveOutputStream
java.io.IOException
- on errorpublic void write(byte[] wBuf, int wOffset, int numToWrite) throws java.io.IOException
write
in class java.io.OutputStream
wBuf
- The buffer to write to the archive.wOffset
- The offset in the buffer from which to get bytes.numToWrite
- The number of bytes to write.java.io.IOException
- on errorvoid writePaxHeaders(TarArchiveEntry entry, java.lang.String entryName, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.IOException
java.io.IOException
private byte[] encodeExtendedPaxHeadersContents(java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
private java.lang.String stripTo7Bits(java.lang.String name)
private boolean shouldBeReplaced(char c)
private void writeEOFRecord() throws java.io.IOException
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
public ArchiveEntry createArchiveEntry(java.io.File inputFile, java.lang.String entryName) throws java.io.IOException
ArchiveOutputStream
createArchiveEntry
in class ArchiveOutputStream
inputFile
- the file to create the entry fromentryName
- name to use for the entryjava.io.IOException
- if an I/O error occursprivate void writeRecord(byte[] record) throws java.io.IOException
record
- The record data to write to the archive.java.io.IOException
- on errorprivate void padAsNeeded() throws java.io.IOException
java.io.IOException
private void addPaxHeadersForBigNumbers(java.util.Map<java.lang.String,java.lang.String> paxHeaders, TarArchiveEntry entry)
private void addPaxHeaderForBigNumber(java.util.Map<java.lang.String,java.lang.String> paxHeaders, java.lang.String header, long value, long maxValue)
private void failForBigNumbers(TarArchiveEntry entry)
private void failForBigNumber(java.lang.String field, long value, long maxValue)
private void failForBigNumberWithPosixMessage(java.lang.String field, long value, long maxValue)
private void failForBigNumber(java.lang.String field, long value, long maxValue, java.lang.String additionalMsg)
private boolean handleLongName(TarArchiveEntry entry, java.lang.String name, java.util.Map<java.lang.String,java.lang.String> paxHeaders, java.lang.String paxHeaderName, byte linkType, java.lang.String fieldName) throws java.io.IOException
I.e. if the given name is too long to be written to a plain tar header then
entry
- entry the name belongs toname
- the name to writepaxHeaders
- current map of pax headerspaxHeaderName
- name of the pax header to writelinkType
- type of the GNU entry to writefieldName
- the name of the fieldjava.io.IOException
private void transferModTime(TarArchiveEntry from, TarArchiveEntry to)