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

public Class ArchiveStreamFactory

extends Object
implements ArchiveStreamProvider
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.archivers.ArchiveStreamProvider
Imports
java.io.ByteArrayInputStream, .IOException, .InputStream, .OutputStream, java.security.AccessController, .PrivilegedAction, java.util.Collections, .Locale, .ServiceLoader, .Set, .SortedMap, .TreeMap, org.apache.commons.compress.archivers.ar.ArArchiveInputStream, .ArArchiveOutputStream, org.apache.commons.compress.archivers.arj.ArjArchiveInputStream, org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream, .CpioArchiveOutputStream, org.apache.commons.compress.archivers.dump.DumpArchiveInputStream, org.apache.commons.compress.archivers.jar.JarArchiveInputStream, .JarArchiveOutputStream, org.apache.commons.compress.archivers.sevenz.SevenZFile, org.apache.commons.compress.archivers.tar.TarArchiveInputStream, .TarArchiveOutputStream, org.apache.commons.compress.archivers.zip.ZipArchiveInputStream, .ZipArchiveOutputStream, org.apache.commons.compress.utils.IOUtils, .Sets

Factory to create Archive[In|Out]putStreams from names or the first bytes of the InputStream. In order to add other implementations, you should extend ArchiveStreamFactory and override the appropriate methods (and call their implementation from super of course). Compressing a ZIP-File:
final OutputStream out = Files.newOutputStream(output.toPath());
ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);

os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
IOUtils.copy(Files.newInputStream(file1.toPath()), os);
os.closeArchiveEntry();

os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
IOUtils.copy(Files.newInputStream(file2.toPath()), os);
os.closeArchiveEntry();
os.close();
Decompressing a ZIP-File:
final InputStream is = Files.newInputStream(input.toPath());
ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is);
ZipArchiveEntry entry = (ZipArchiveEntry) in.getNextEntry();
OutputStream out = Files.newOutputStream(dir.toPath().resolve(entry.getName()));
IOUtils.copy(in, out);
out.close();
in.close();

Field Summary

Modifier and TypeField and Description
public static final String
APK

Constant (value apk) used to identify the APK archive format.

public static final String
APKM

Constant (value apkm) used to identify the APKM archive format.

public static final String
APKS

Constant (value apks) used to identify the APKS archive format.

public static final String
AR

Constant (value ar) used to identify the AR archive format.

private SortedMap<String, ArchiveStreamProvider>
private SortedMap<String, ArchiveStreamProvider>
public static final String
ARJ

Constant (value arj) used to identify the ARJ archive format.

public static final String
CPIO

Constant (value cpio) used to identify the CPIO archive format.

public static final ArchiveStreamFactory
DEFAULT

The singleton instance using the platform default encoding.

public static final String
DUMP

Constant (value dump) used to identify the Unix DUMP archive format.

private static final int
private final String
encoding

Entry encoding, null for the platform default.

private volatile String
entryEncoding

Entry encoding, null for the default.

public static final String
JAR

Constant (value jar) used to identify the JAR archive format.

public static final String
SEVEN_Z

Constant (value 7z) used to identify the 7z archive format.

private static final int
public static final String
TAR

Constant used to identify the TAR archive format.

private static final int
public static final String
XAPK

Constant (value xapk) used to identify the XAPK archive format.

public static final String
ZIP

Constant (value zip) used to identify the ZIP archive format.

Constructor Summary

AccessConstructor and Description
public
ArchiveStreamFactory()

Constructs an instance using the platform default encoding.

public
ArchiveStreamFactory(final String
the encoding to be used.
encoding
)

Constructs an instance using the specified encoding.

Method Summary

Modifier and TypeMethod and Description
private static Iterable<ArchiveStreamProvider>
public <I extends ArchiveInputStream<? extends ArchiveEntry>> I

Returns:

the archive input stream
createArchiveInputStream
(final InputStream
the input stream
in
)

Creates an archive input stream from an input stream, autodetecting the archive type from the first few bytes of the stream.

public <I extends ArchiveInputStream<? extends ArchiveEntry>> I

Returns:

the archive input stream
createArchiveInputStream
(final String
the archive name, i.e. ar, arj, zip, tar, jar, cpio, dump or 7z
archiverName
,
final InputStream
the input stream
in
)

Creates an archive input stream from an archiver name and an input stream.

public <I extends ArchiveInputStream<? extends ArchiveEntry>> I
createArchiveInputStream(final String
the archiver name, i.e. ar, arj, zip, tar, jar, cpio, dump or 7z
archiverName
,
final InputStream
the input stream
in
,
final String
encoding name or null for the default
actualEncoding
)

Implements org.apache.commons.compress.archivers.ArchiveStreamProvider.createArchiveInputStream.

Creates an archive input stream from an archiver name and an input stream.
public <O extends ArchiveOutputStream<? extends ArchiveEntry>> O

Returns:

the archive output stream
createArchiveOutputStream
(final String
the archive name, i.e. ar, zip, tar, jar or cpio
archiverName
,
final OutputStream
the output stream
out
)

Creates an archive output stream from an archiver name and an output stream.

public <O extends ArchiveOutputStream<? extends ArchiveEntry>> O
createArchiveOutputStream(final String
the archiver name, i.e. ar, zip, tar, jar or cpio
archiverName
,
final OutputStream
the output stream
out
,
final String
encoding name or null for the default
actualEncoding
)

Implements org.apache.commons.compress.archivers.ArchiveStreamProvider.createArchiveOutputStream.

Creates an archive output stream from an archiver name and an output stream.
public static String

Returns:

type of archiver if found
detect
(final InputStream
input stream
in
)

Try to determine the type of Archiver

public static SortedMap<String, ArchiveStreamProvider>

Returns:

An immutable, map from names to provider objects
findAvailableArchiveInputStreamProviders
()

Constructs a new sorted map from input stream provider names to provider objects.

public static SortedMap<String, ArchiveStreamProvider>

Returns:

An immutable, map from names to provider objects
findAvailableArchiveOutputStreamProviders
()

Constructs a new sorted map from output stream provider names to provider objects.

public SortedMap<String, ArchiveStreamProvider>
public SortedMap<String, ArchiveStreamProvider>
public String

Returns:

entry encoding, or null for the archiver default
getEntryEncoding
()

Gets the encoding to use for arj, jar, ZIP, dump, cpio and tar files, or null for the archiver default.

public Set<String>
public Set<String>
pack-priv static void
putAll(final Set<String> names, final ArchiveStreamProvider provider, final TreeMap<String, ArchiveStreamProvider> map)

public void
setEntryEncoding(final String
the entry encoding, null uses the archiver default.
entryEncoding
)

Deprecated 1.10 use ArchiveStreamFactory(String) to specify the encoding
Sets the encoding to use for arj, jar, ZIP, dump, cpio and tar files.
private static String
toKey(final String name)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

APKback to summary
public static final String APK

Constant (value apk) used to identify the APK archive format.

APK file extensions are .apk, .xapk, .apks, .apkm

Since
1.22
APKMback to summary
public static final String APKM

Constant (value apkm) used to identify the APKM archive format.

APK file extensions are .apk, .xapk, .apks, .apkm

Since
1.22
APKSback to summary
public static final String APKS

Constant (value apks) used to identify the APKS archive format.

APK file extensions are .apk, .xapk, .apks, .apkm

Since
1.22
ARback to summary
public static final String AR

Constant (value ar) used to identify the AR archive format.

Since
1.1
archiveInputStreamProvidersback to summary
private SortedMap<String, ArchiveStreamProvider> archiveInputStreamProviders
archiveOutputStreamProvidersback to summary
private SortedMap<String, ArchiveStreamProvider> archiveOutputStreamProviders
ARJback to summary
public static final String ARJ

Constant (value arj) used to identify the ARJ archive format. Not supported as an output stream type.

Since
1.6
CPIOback to summary
public static final String CPIO

Constant (value cpio) used to identify the CPIO archive format.

Since
1.1
DEFAULTback to summary
public static final ArchiveStreamFactory DEFAULT

The singleton instance using the platform default encoding.

Since
1.21
DUMPback to summary
public static final String DUMP

Constant (value dump) used to identify the Unix DUMP archive format. Not supported as an output stream type.

Since
1.3
DUMP_SIGNATURE_SIZEback to summary
private static final int DUMP_SIGNATURE_SIZE
encodingback to summary
private final String encoding

Entry encoding, null for the platform default.

entryEncodingback to summary
private volatile String entryEncoding

Entry encoding, null for the default.

JARback to summary
public static final String JAR

Constant (value jar) used to identify the JAR archive format.

Since
1.1
SEVEN_Zback to summary
public static final String SEVEN_Z

Constant (value 7z) used to identify the 7z archive format.

Since
1.8
SIGNATURE_SIZEback to summary
private static final int SIGNATURE_SIZE
TARback to summary
public static final String TAR

Constant used to identify the TAR archive format.

Since
1.1
TAR_HEADER_SIZEback to summary
private static final int TAR_HEADER_SIZE
XAPKback to summary
public static final String XAPK

Constant (value xapk) used to identify the XAPK archive format.

APK file extensions are .apk, .xapk, .apks, .apkm

Since
1.22
ZIPback to summary
public static final String ZIP

Constant (value zip) used to identify the ZIP archive format.

Since
1.1

Constructor Detail

ArchiveStreamFactoryback to summary
public ArchiveStreamFactory()

Constructs an instance using the platform default encoding.

ArchiveStreamFactoryback to summary
public ArchiveStreamFactory(final String encoding)

Constructs an instance using the specified encoding.

Parameters
encoding:String

the encoding to be used.

Since
1.10

Method Detail

archiveStreamProviderIterableback to summary
private static Iterable<ArchiveStreamProvider> archiveStreamProviderIterable()
createArchiveInputStreamback to summary
public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInputStream(final InputStream in) throws ArchiveException

Creates an archive input stream from an input stream, autodetecting the archive type from the first few bytes of the stream. The InputStream must support marks, like BufferedInputStream.

Parameters
<I>
The ArchiveInputStream type.
in:InputStream

the input stream

Returns:I

the archive input stream

Exceptions
ArchiveException:
if the archiver name is not known
StreamingNotSupportedException:
if the format cannot be read from a stream
IllegalArgumentException:
if the stream is null or does not support mark
createArchiveInputStreamback to summary
public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInputStream(final String archiverName, final InputStream in) throws ArchiveException

Creates an archive input stream from an archiver name and an input stream.

Parameters
<I>
The ArchiveInputStream type.
archiverName:String

the archive name, i.e. ar, arj, zip, tar, jar, cpio, dump or 7z

in:InputStream

the input stream

Returns:I

the archive input stream

Exceptions
ArchiveException:
if the archiver name is not known
StreamingNotSupportedException:
if the format cannot be read from a stream
IllegalArgumentException:
if the archiver name or stream is null
createArchiveInputStreamback to summary
public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInputStream(final String archiverName, final InputStream in, final String actualEncoding) throws ArchiveException

Implements org.apache.commons.compress.archivers.ArchiveStreamProvider.createArchiveInputStream.

Doc from org.apache.commons.compress.archivers.ArchiveStreamProvider.createArchiveInputStream.

Creates an archive input stream from an archiver name and an input stream.

Parameters
<I>
The ArchiveInputStream type.
archiverName:String

the archiver name, i.e. ar, arj, zip, tar, jar, cpio, dump or 7z

in:InputStream

the input stream

actualEncoding:String

encoding name or null for the default

Returns:I

the archive input stream

Annotations
@SuppressWarnings:unchecked
@Override
Exceptions
ArchiveException:
if the archiver name is not known
createArchiveOutputStreamback to summary
public <O extends ArchiveOutputStream<? extends ArchiveEntry>> O createArchiveOutputStream(final String archiverName, final OutputStream out) throws ArchiveException

Creates an archive output stream from an archiver name and an output stream.

Parameters
<O>
The ArchiveOutputStream type.
archiverName:String

the archive name, i.e. ar, zip, tar, jar or cpio

out:OutputStream

the output stream

Returns:O

the archive output stream

Exceptions
ArchiveException:
if the archiver name is not known
StreamingNotSupportedException:
if the format cannot be written to a stream
IllegalArgumentException:
if the archiver name or stream is null
createArchiveOutputStreamback to summary
public <O extends ArchiveOutputStream<? extends ArchiveEntry>> O createArchiveOutputStream(final String archiverName, final OutputStream out, final String actualEncoding) throws ArchiveException

Implements org.apache.commons.compress.archivers.ArchiveStreamProvider.createArchiveOutputStream.

Doc from org.apache.commons.compress.archivers.ArchiveStreamProvider.createArchiveOutputStream.

Creates an archive output stream from an archiver name and an output stream.

Parameters
<O>
The ArchiveInputStream type.
archiverName:String

the archiver name, i.e. ar, zip, tar, jar or cpio

out:OutputStream

the output stream

actualEncoding:String

encoding name or null for the default

Returns:O

the archive output stream

Annotations
@SuppressWarnings:unchecked
@Override
Exceptions
ArchiveException:
if the archiver name is not known
detectback to summary
public static String detect(final InputStream in) throws ArchiveException

Try to determine the type of Archiver

Parameters
in:InputStream

input stream

Returns:String

type of archiver if found

Exceptions
ArchiveException:
if an archiver cannot be detected in the stream
Since
1.14
findAvailableArchiveInputStreamProvidersback to summary
public static SortedMap<String, ArchiveStreamProvider> findAvailableArchiveInputStreamProviders()

Constructs a new sorted map from input stream provider names to provider objects.

The map returned by this method will have one entry for each provider for which support is available in the current Java virtual machine. If two or more supported provider have the same name then the resulting map will contain just one of them; which one it will contain is not specified.

The invocation of this method, and the subsequent use of the resulting map, may cause time-consuming disk or network I/O operations to occur. This method is provided for applications that need to enumerate all of the available providers, for example to allow user provider selection.

This method may return different results at different times if new providers are dynamically made available to the current Java virtual machine.

Returns:SortedMap<String, ArchiveStreamProvider>

An immutable, map from names to provider objects

Since
1.13
findAvailableArchiveOutputStreamProvidersback to summary
public static SortedMap<String, ArchiveStreamProvider> findAvailableArchiveOutputStreamProviders()

Constructs a new sorted map from output stream provider names to provider objects.

The map returned by this method will have one entry for each provider for which support is available in the current Java virtual machine. If two or more supported provider have the same name then the resulting map will contain just one of them; which one it will contain is not specified.

The invocation of this method, and the subsequent use of the resulting map, may cause time-consuming disk or network I/O operations to occur. This method is provided for applications that need to enumerate all of the available providers, for example to allow user provider selection.

This method may return different results at different times if new providers are dynamically made available to the current Java virtual machine.

Returns:SortedMap<String, ArchiveStreamProvider>

An immutable, map from names to provider objects

Since
1.13
getArchiveInputStreamProvidersback to summary
public SortedMap<String, ArchiveStreamProvider> getArchiveInputStreamProviders()
getArchiveOutputStreamProvidersback to summary
public SortedMap<String, ArchiveStreamProvider> getArchiveOutputStreamProviders()
getEntryEncodingback to summary
public String getEntryEncoding()

Gets the encoding to use for arj, jar, ZIP, dump, cpio and tar files, or null for the archiver default.

Returns:String

entry encoding, or null for the archiver default

Since
1.5
getInputStreamArchiveNamesback to summary
public Set<String> getInputStreamArchiveNames()

Implements org.apache.commons.compress.archivers.ArchiveStreamProvider.getInputStreamArchiveNames.

Doc from org.apache.commons.compress.archivers.ArchiveStreamProvider.getInputStreamArchiveNames.

Gets all the input stream archive names for this provider

Returns:Set<String>

all the input archive names for this provider

Annotations
@Override
getOutputStreamArchiveNamesback to summary
public Set<String> getOutputStreamArchiveNames()

Implements org.apache.commons.compress.archivers.ArchiveStreamProvider.getOutputStreamArchiveNames.

Doc from org.apache.commons.compress.archivers.ArchiveStreamProvider.getOutputStreamArchiveNames.

Gets all the output stream archive names for this provider

Returns:Set<String>

all the output archive names for this provider

Annotations
@Override
putAllback to summary
pack-priv static void putAll(final Set<String> names, final ArchiveStreamProvider provider, final TreeMap<String, ArchiveStreamProvider> map)
setEntryEncodingback to summary
public void setEntryEncoding(final String entryEncoding)

Deprecated

1.10 use ArchiveStreamFactory(String) to specify the encoding

Sets the encoding to use for arj, jar, ZIP, dump, cpio and tar files. Use null for the archiver default.

Parameters
entryEncoding:String

the entry encoding, null uses the archiver default.

Annotations
@Deprecated
Exceptions
IllegalStateException:
if the constructor ArchiveStreamFactory(String) was used to specify the factory encoding.
Since
1.5
toKeyback to summary
private static String toKey(final String name)