Class ArchiveOutputStream<E extends ArchiveEntry>
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.apache.commons.compress.archivers.ArchiveOutputStream<E>
- Type Parameters:
E
- The type ofArchiveEntry
consumed.
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
- Direct Known Subclasses:
ArArchiveOutputStream
,CpioArchiveOutputStream
,TarArchiveOutputStream
,ZipArchiveOutputStream
Archive output stream implementations are expected to override the
FilterOutputStream.write(byte[], int, int)
method to improve performance. They should also override
close()
to ensure that any necessary trailers are added.
The normal sequence of calls when working with ArchiveOutputStreams is:
- Create ArchiveOutputStream object,
- optionally write SFX header (Zip only),
- repeat as needed:
putArchiveEntry(ArchiveEntry)
(writes entry header),FilterOutputStream.write(byte[])
(writes entry data, as often as needed),closeArchiveEntry()
(closes entry),
finish()
(ends the addition of entries),- optionally write additional data, provided format supports it,
close()
.
-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out
-
Constructor Summary
ConstructorDescriptionConstructs a new instance without a backing OutputStream.Constructs a new instance with the given backing OutputStream. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canWriteEntryData
(ArchiveEntry archiveEntry) Whether this stream is able to write the given entry.protected void
Throws anIOException
if this instance is already finished.protected void
Check to make sure that this stream has not been closedvoid
close()
abstract void
Closes the archive entry, writing any trailer information that may be required.protected void
count
(int written) Increments the counter of already written bytes.protected void
count
(long written) Increments the counter of already written bytes.abstract E
createArchiveEntry
(File inputFile, String entryName) Creates an archive entry using the inputFile and entryName provided.createArchiveEntry
(Path inputPath, String entryName, LinkOption... options) Creates an archive entry using the inputPath and entryName provided.void
finish()
Finishes the addition of entries to this stream, without closing it.long
Gets the current number of bytes written to this stream.int
getCount()
Deprecated.this method may yield wrong results for large archives, use #getBytesWritten insteadprotected boolean
isClosed()
Tests whether this instance was successfully closed.protected boolean
Tests whether this instance was successfully finished.abstract void
putArchiveEntry
(E entry) Writes the headers for an archive entry to the output stream.void
write
(int b) Writes a byte to the current archive entry.Methods inherited from class java.io.FilterOutputStream
flush, write, write
-
Constructor Details
-
ArchiveOutputStream
public ArchiveOutputStream()Constructs a new instance without a backing OutputStream.You must initialize
this.out
after construction. -
ArchiveOutputStream
Constructs a new instance with the given backing OutputStream.- Parameters:
out
- the underlying output stream to be assigned to the fieldthis.out
for later use, ornull
if this instance is to be created without an underlying stream.- Since:
- 1.27.0.
-
-
Method Details
-
canWriteEntryData
Whether this stream is able to write the given entry.Some archive formats support variants or details that are not supported (yet).
- Parameters:
archiveEntry
- the entry to test- Returns:
- This implementation always returns true.
- Since:
- 1.1
-
checkFinished
Throws anIOException
if this instance is already finished.- Throws:
IOException
- if this instance is already finished.- Since:
- 1.27.0
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterOutputStream
- Throws:
IOException
-
closeArchiveEntry
Closes the archive entry, writing any trailer information that may be required.- Throws:
IOException
- if an I/O error occurs
-
count
Increments the counter of already written bytes. Doesn't increment if EOF has been hit (written == -1
).- Parameters:
written
- the number of bytes written
-
count
Increments the counter of already written bytes. Doesn't increment if EOF has been hit (written == -1
).- Parameters:
written
- the number of bytes written- Since:
- 1.1
-
createArchiveEntry
Creates an archive entry using the inputFile and entryName provided.- Parameters:
inputFile
- the file to create the entry fromentryName
- name to use for the entry- Returns:
- the ArchiveEntry set up with details from the file
- Throws:
IOException
- if an I/O error occurs
-
createArchiveEntry
public E createArchiveEntry(Path inputPath, String entryName, LinkOption... options) throws IOException 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
- the file to create the entry fromentryName
- name to use for the entryoptions
- options indicating how symbolic links are handled.- Returns:
- the ArchiveEntry set up with details from the file
- Throws:
IOException
- if an I/O error occurs- Since:
- 1.21
-
finish
Finishes the addition of entries to this stream, without closing it. Additional data can be written, if the format supports it.- Throws:
IOException
- Maybe thrown by subclasses if the user forgets to close the entry.
-
getBytesWritten
Gets the current number of bytes written to this stream.- Returns:
- the number of written bytes
- Since:
- 1.1
-
getCount
Deprecated.this method may yield wrong results for large archives, use #getBytesWritten insteadGets the current number of bytes written to this stream.- Returns:
- the number of written bytes
-
isClosed
Tests whether this instance was successfully closed.- Returns:
- whether this instance was successfully closed.
- Since:
- 1.27.0
-
isFinished
Tests whether this instance was successfully finished.- Returns:
- whether this instance was successfully finished.
- Since:
- 1.27.0
-
putArchiveEntry
Writes the headers for an archive entry to the output stream. The caller must then write the content to the stream and callcloseArchiveEntry()
to complete the process.- Parameters:
entry
- describes the entry- Throws:
IOException
- if an I/O error occurs
-
write
Writes a byte to the current archive entry.This method simply calls
write( byte[], 0, 1 )
.MUST be overridden if the
FilterOutputStream.write(byte[], int, int)
method is not overridden; may be overridden otherwise.- Overrides:
write
in classFilterOutputStream
- Parameters:
b
- The byte to be written.- Throws:
IOException
- on error
-
checkOpen
Check to make sure that this stream has not been closed- Throws:
IOException
- if the stream is already closed- Since:
- 1.27.0
-