Class TarFile
- All Implemented Interfaces:
ArchiveFile
- Direct Known Subclasses:
BZip2TarFile
,GZipTarFile
,XZTarFile
Implementation of ArchiveFile
for tar files.
Compared to
ZipFile
, this one should be used with some care, due to the
nature of a tar file: While a zip file contains a catalog, a tar
file does not. In other words, the only way to read a tar file in
a performant manner is by iterating over it from the beginning to
the end. If you try to open another entry than the "next" entry,
then you force to skip entries, until the requested entry is found.
This may require to reread the entire file!
In other words, the recommended use of this class is to use
getEntries()
and invoke getInputStream(TarArchiveEntry)
only for the current entry. Basically, this is to handle it like
TarArchiveInputStream
.
The advantage of this class is that you may write code for the
ArchiveFile
, which is valid for both tar files and zip files.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate org.apache.commons.compress.archivers.tar.TarArchiveEntry
private final File
private org.apache.commons.compress.archivers.tar.TarArchiveInputStream
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
private boolean
findEntry
(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry, org.apache.commons.compress.archivers.tar.TarArchiveEntry currentEntry) Enumeration
<org.apache.commons.compress.archivers.ArchiveEntry> Implementation ofArchiveFile.getEntries()
.protected InputStream
getInputStream
(File file) getInputStream
(org.apache.commons.compress.archivers.ArchiveEntry entry) Returns anInputStream
with the given entries contents.getInputStream
(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry) Returns anInputStream
with the given entries contents.private InputStream
getInputStream
(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry, org.apache.commons.compress.archivers.tar.TarArchiveEntry currentEntry) private void
open()
-
Field Details
-
file
-
inputStream
private org.apache.commons.compress.archivers.tar.TarArchiveInputStream inputStream -
currentEntry
private org.apache.commons.compress.archivers.tar.TarArchiveEntry currentEntry
-
-
Constructor Details
-
TarFile
Creates a new instance with the given file.
-
-
Method Details
-
getEntries
public Enumeration<org.apache.commons.compress.archivers.ArchiveEntry> getEntries() throws IOExceptionImplementation ofArchiveFile.getEntries()
. Note, that there is an interaction between this method andgetInputStream(TarArchiveEntry)
, orgetInputStream(org.apache.commons.compress.archivers.ArchiveEntry)
: If an input stream is opened for any other entry than the enumerations current entry, then entries may be skipped.- Specified by:
getEntries
in interfaceArchiveFile
- Throws:
IOException
-
close
- Throws:
IOException
-
getInputStream
public InputStream getInputStream(org.apache.commons.compress.archivers.ArchiveEntry entry) throws IOException Description copied from interface:ArchiveFile
Returns anInputStream
with the given entries contents. org.apache.commons.compress.archivers.ArchiveEntry- Specified by:
getInputStream
in interfaceArchiveFile
- Throws:
IOException
-
getInputStream
public InputStream getInputStream(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry) throws IOException Returns anInputStream
with the given entries contents. ThisInputStream
may be closed: Nothing happens in that case, because an actual close would invalidate the underlyingTarArchiveInputStream
.- Throws:
IOException
-
getInputStream
- Throws:
IOException
-
getInputStream
private InputStream getInputStream(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry, org.apache.commons.compress.archivers.tar.TarArchiveEntry currentEntry) throws IOException - Throws:
IOException
-
open
- Throws:
IOException
-
findEntry
private boolean findEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry, org.apache.commons.compress.archivers.tar.TarArchiveEntry currentEntry) throws IOException - Throws:
IOException
-