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

public Class CompressorStreamFactory

extends Object
implements CompressorStreamProvider
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.compressors.CompressorStreamProvider
Imports
java.io.IOException, .InputStream, .OutputStream, java.security.AccessController, .PrivilegedAction, java.util.Collections, .Locale, .ServiceLoader, .Set, .SortedMap, .TreeMap, org.apache.commons.compress.compressors.brotli.BrotliCompressorInputStream, .BrotliUtils, org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream, .BZip2CompressorOutputStream, org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStream, .DeflateCompressorOutputStream, org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream, org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream, .GzipCompressorOutputStream, org.apache.commons.compress.compressors.lz4.BlockLZ4CompressorInputStream, .BlockLZ4CompressorOutputStream, .FramedLZ4CompressorInputStream, .FramedLZ4CompressorOutputStream, org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream, .LZMACompressorOutputStream, .LZMAUtils, org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream, .Pack200CompressorOutputStream, org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream, .FramedSnappyCompressorOutputStream, .SnappyCompressorInputStream, org.apache.commons.compress.compressors.xz.XZCompressorInputStream, .XZCompressorOutputStream, .XZUtils, org.apache.commons.compress.compressors.z.ZCompressorInputStream, org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream, .ZstdCompressorOutputStream, .ZstdUtils, org.apache.commons.compress.utils.IOUtils, .Sets

Factory to create Compressor[In|Out]putStreams from names. To add other implementations you should extend CompressorStreamFactory and override the appropriate methods (and call their implementation from super of course).

Example (Compressing a file):
final OutputStream out = Files.newOutputStream(output.toPath());
CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.BZIP2, out);
IOUtils.copy(Files.newInputStream(input.toPath()), cos);
cos.close();
Example (Decompressing a file):
final InputStream is = Files.newInputStream(input.toPath());
CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.BZIP2, is);
IOUtils.copy(in, Files.newOutputStream(output.toPath()));
in.close();

Field Summary

Modifier and TypeField and Description
private static final Set<String>
public static final String
BROTLI

Constant (value br) used to identify the BROTLI compression algorithm.

public static final String
BZIP2

Constant (value bzip2) used to identify the BZIP2 compression algorithm.

private SortedMap<String, CompressorStreamProvider>
private SortedMap<String, CompressorStreamProvider>
private volatile boolean
decompressConcatenated

If true, decompress until the end of the input.

private final Boolean
decompressUntilEOF

If true, decompress until the end of the input.

public static final String
DEFLATE

Constant (value deflate) used to identify the Deflate compress method.

public static final String
DEFLATE64

Constant (value deflate64) used to identify the Deflate64 compress method.

public static final String
GZIP

Constant (value gz) used to identify the GZIP compression algorithm.

public static final String
LZ4_BLOCK

Constant (value lz4-block) used to identify the block LZ4 compression method.

public static final String
LZ4_FRAMED

Constant (value lz4-framed) used to identify the frame LZ4 compression method.

public static final String
LZMA

Constant (value lzma) used to identify the LZMA compression method.

private final int
public static final String
PACK200

Constant (value pack200) used to identify the PACK200 compression algorithm.

private static final CompressorStreamFactory
public static final String
SNAPPY_FRAMED

Constant (value snappy-framed) used to identify the "framed" Snappy compression method.

public static final String
SNAPPY_RAW

Constant (value snappy-raw) used to identify the "raw" Snappy compression method.

public static final String
XZ

Constant (value xz) used to identify the XZ compression method.

private static final String
private static final String
private static final String
public static final String
Z

Constant (value z) used to identify the traditional Unix compress method.

public static final String
ZSTANDARD

Constant (value zstd) used to identify the Zstandard compression algorithm.

Constructor Summary

AccessConstructor and Description
public
CompressorStreamFactory()

Constructs an instance with the decompress Concatenated option set to false.

public
CompressorStreamFactory(final boolean
if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.
decompressUntilEOF
)

Constructs an instance with the provided decompress Concatenated option.

public
CompressorStreamFactory(final boolean
if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.
decompressUntilEOF
,
final int
Some streams require allocation of potentially significant byte arrays/tables, and they can offer checks to prevent OOMs on corrupt files. Set the maximum allowed memory allocation in KBs.
memoryLimitInKb
)

Constructs an instance with the provided decompress Concatenated option.

Method Summary

Modifier and TypeMethod and Description
private static Iterable<CompressorStreamProvider>
public CompressorInputStream

Returns:

the compressor input stream
createCompressorInputStream
(final InputStream
the input stream
in
)

Creates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream.

public CompressorInputStream

Returns:

the compressor input stream
createCompressorInputStream
(final InputStream
the input stream
in
,
final Set<String>
compressor names to limit autodetection
compressorNames
)

Creates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream while limiting the detected type to the provided set of compressor names.

public CompressorInputStream

Returns:

compressor input stream
createCompressorInputStream
(final String
of the compressor, i.e. gz, bzip2, xz, lzma, pack200, snappy-raw, snappy-framed, z, lz4-block, lz4-framed, zstd, deflate64 or deflate
name
,
final InputStream
the input stream
in
)

Creates a compressor input stream from a compressor name and an input stream.

public CompressorInputStream
createCompressorInputStream(final String
of the compressor, i.e. gz, bzip2, xz, lzma, pack200, snappy-raw, snappy-framed, z or deflate
name
,
final InputStream
the input stream
in
,
final boolean
if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.
actualDecompressConcatenated
)

Implements org.apache.commons.compress.compressors.CompressorStreamProvider.createCompressorInputStream.

Creates a compressor input stream from a compressor name and an input stream.
public CompressorOutputStream

Returns:

the compressor output stream
createCompressorOutputStream
(final String
the compressor name, i.e. gz, bzip2, xz, pack200, snappy-framed, lz4-block, lz4-framed, zstd or deflate
name
,
final OutputStream
the output stream
out
)

Implements org.apache.commons.compress.compressors.CompressorStreamProvider.createCompressorOutputStream.

Creates a compressor output stream from a compressor name and an output stream.
public static String

Returns:

type of compressor stream detected
detect
(final InputStream
input stream
inputStream
)

Detects the type of compressor stream.

pack-priv static String

Returns:

type of compressor stream detected
detect
(final InputStream
input stream
inputStream
,
final Set<String>
compressor names to limit autodetection
compressorNames
)

Detects the type of compressor stream while limiting the type to the provided set of compressor names.

public static SortedMap<String, CompressorStreamProvider>

Returns:

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

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

public static SortedMap<String, CompressorStreamProvider>

Returns:

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

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

public static String
public static String
public SortedMap<String, CompressorStreamProvider>
public SortedMap<String, CompressorStreamProvider>
pack-priv boolean
public Boolean
public static String
public static String

Returns:

the constant DEFLATE64
getDeflate64
()

public static String
public Set<String>
public static String
public static String
public static String
public Set<String>
public static String
public static CompressorStreamFactory
public static String
public static String
public static String
public static String
getZ()

public static String
pack-priv static void
public void
setDecompressConcatenated(final boolean
if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream
decompressConcatenated
)

Deprecated 1.10 use the CompressorStreamFactory(boolean) constructor instead
Sets whether to decompress the full input or only the first stream in formats supporting multiple concatenated input streams.
private static String
toKey(final String name)

private static String
youNeed(final String name, final String url)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALL_NAMESback to summary
private static final Set<String> ALL_NAMES
BROTLIback to summary
public static final String BROTLI

Constant (value br) used to identify the BROTLI compression algorithm.

Since
1.14
BZIP2back to summary
public static final String BZIP2

Constant (value bzip2) used to identify the BZIP2 compression algorithm.

Since
1.1
compressorInputStreamProvidersback to summary
private SortedMap<String, CompressorStreamProvider> compressorInputStreamProviders
compressorOutputStreamProvidersback to summary
private SortedMap<String, CompressorStreamProvider> compressorOutputStreamProviders
decompressConcatenatedback to summary
private volatile boolean decompressConcatenated

If true, decompress until the end of the input. If false, stop after the first stream and leave the input position to point to the next byte after the stream

decompressUntilEOFback to summary
private final Boolean decompressUntilEOF

If true, decompress until the end of the input. If false, stop after the first stream and leave the input position to point to the next byte after the stream

DEFLATEback to summary
public static final String DEFLATE

Constant (value deflate) used to identify the Deflate compress method.

Since
1.9
DEFLATE64back to summary
public static final String DEFLATE64

Constant (value deflate64) used to identify the Deflate64 compress method.

Since
1.16
GZIPback to summary
public static final String GZIP

Constant (value gz) used to identify the GZIP compression algorithm.

Since
1.1
LZ4_BLOCKback to summary
public static final String LZ4_BLOCK

Constant (value lz4-block) used to identify the block LZ4 compression method.

Since
1.14
LZ4_FRAMEDback to summary
public static final String LZ4_FRAMED

Constant (value lz4-framed) used to identify the frame LZ4 compression method.

Since
1.14
LZMAback to summary
public static final String LZMA

Constant (value lzma) used to identify the LZMA compression method.

Since
1.6
memoryLimitInKbback to summary
private final int memoryLimitInKb
PACK200back to summary
public static final String PACK200

Constant (value pack200) used to identify the PACK200 compression algorithm.

Since
1.3
SINGLETONback to summary
private static final CompressorStreamFactory SINGLETON
SNAPPY_FRAMEDback to summary
public static final String SNAPPY_FRAMED

Constant (value snappy-framed) used to identify the "framed" Snappy compression method.

Since
1.7
SNAPPY_RAWback to summary
public static final String SNAPPY_RAW

Constant (value snappy-raw) used to identify the "raw" Snappy compression method. Not supported as an output stream type.

Since
1.7
XZback to summary
public static final String XZ

Constant (value xz) used to identify the XZ compression method.

Since
1.4
YOU_NEED_BROTLI_DECback to summary
private static final String YOU_NEED_BROTLI_DEC
YOU_NEED_XZ_JAVAback to summary
private static final String YOU_NEED_XZ_JAVA
YOU_NEED_ZSTD_JNIback to summary
private static final String YOU_NEED_ZSTD_JNI
Zback to summary
public static final String Z

Constant (value z) used to identify the traditional Unix compress method. Not supported as an output stream type.

Since
1.7
ZSTANDARDback to summary
public static final String ZSTANDARD

Constant (value zstd) used to identify the Zstandard compression algorithm. Not supported as an output stream type.

Since
1.16

Constructor Detail

CompressorStreamFactoryback to summary
public CompressorStreamFactory()

Constructs an instance with the decompress Concatenated option set to false.

CompressorStreamFactoryback to summary
public CompressorStreamFactory(final boolean decompressUntilEOF)

Constructs an instance with the provided decompress Concatenated option.

Parameters
decompressUntilEOF:boolean

if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.

Since
1.10
CompressorStreamFactoryback to summary
public CompressorStreamFactory(final boolean decompressUntilEOF, final int memoryLimitInKb)

Constructs an instance with the provided decompress Concatenated option.

Parameters
decompressUntilEOF:boolean

if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.

memoryLimitInKb:int

Some streams require allocation of potentially significant byte arrays/tables, and they can offer checks to prevent OOMs on corrupt files. Set the maximum allowed memory allocation in KBs.

Since
1.14

Method Detail

archiveStreamProviderIterableback to summary
private static Iterable<CompressorStreamProvider> archiveStreamProviderIterable()
createCompressorInputStreamback to summary
public CompressorInputStream createCompressorInputStream(final InputStream in) throws CompressorException

Creates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream. The InputStream must support marks, like BufferedInputStream.

Parameters
in:InputStream

the input stream

Returns:CompressorInputStream

the compressor input stream

Exceptions
CompressorException:
if the compressor name is not known
IllegalArgumentException:
if the stream is null or does not support mark
Since
1.1
createCompressorInputStreamback to summary
public CompressorInputStream createCompressorInputStream(final InputStream in, final Set<String> compressorNames) throws CompressorException

Creates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream while limiting the detected type to the provided set of compressor names. The InputStream must support marks, like BufferedInputStream.

Parameters
in:InputStream

the input stream

compressorNames:Set<String>

compressor names to limit autodetection

Returns:CompressorInputStream

the compressor input stream

Exceptions
CompressorException:
if the autodetected compressor is not in the provided set of compressor names
IllegalArgumentException:
if the stream is null or does not support mark
Since
1.25.0
createCompressorInputStreamback to summary
public CompressorInputStream createCompressorInputStream(final String name, final InputStream in) throws CompressorException

Creates a compressor input stream from a compressor name and an input stream.

Parameters
name:String

of the compressor, i.e. gz, bzip2, xz, lzma, pack200, snappy-raw, snappy-framed, z, lz4-block, lz4-framed, zstd, deflate64 or deflate

in:InputStream

the input stream

Returns:CompressorInputStream

compressor input stream

Exceptions
CompressorException:
if the compressor name is not known or not available, or if there's an IOException or MemoryLimitException thrown during initialization
IllegalArgumentException:
if the name or input stream is null
createCompressorInputStreamback to summary
public CompressorInputStream createCompressorInputStream(final String name, final InputStream in, final boolean actualDecompressConcatenated) throws CompressorException

Implements org.apache.commons.compress.compressors.CompressorStreamProvider.createCompressorInputStream.

Doc from org.apache.commons.compress.compressors.CompressorStreamProvider.createCompressorInputStream.

Creates a compressor input stream from a compressor name and an input stream.

Parameters
name:String

of the compressor, i.e. gz, bzip2, xz, lzma, pack200, snappy-raw, snappy-framed, z or deflate

in:InputStream

the input stream

actualDecompressConcatenated:boolean

if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.

Returns:CompressorInputStream

compressor input stream

Annotations
@Override
Exceptions
CompressorException:
if the compressor name is not known
createCompressorOutputStreamback to summary
public CompressorOutputStream createCompressorOutputStream(final String name, final OutputStream out) throws CompressorException

Implements org.apache.commons.compress.compressors.CompressorStreamProvider.createCompressorOutputStream.

Creates a compressor output stream from a compressor name and an output stream.

Parameters
name:String

the compressor name, i.e. gz, bzip2, xz, pack200, snappy-framed, lz4-block, lz4-framed, zstd or deflate

out:OutputStream

the output stream

Returns:CompressorOutputStream

the compressor output stream

Annotations
@Override
Exceptions
CompressorException:
if the archiver name is not known
IllegalArgumentException:
if the archiver name or stream is null
detectback to summary
public static String detect(final InputStream inputStream) throws CompressorException

Detects the type of compressor stream.

Parameters
inputStream:InputStream

input stream

Returns:String

type of compressor stream detected

Exceptions
CompressorException:
if no compressor stream type was detected or if something else went wrong
IllegalArgumentException:
if stream is null or does not support mark
Since
1.14
detectback to summary
pack-priv static String detect(final InputStream inputStream, final Set<String> compressorNames) throws CompressorException

Detects the type of compressor stream while limiting the type to the provided set of compressor names.

Parameters
inputStream:InputStream

input stream

compressorNames:Set<String>

compressor names to limit autodetection

Returns:String

type of compressor stream detected

Exceptions
CompressorException:
if no compressor stream type was detected or if something else went wrong
IllegalArgumentException:
if stream is null or does not support mark
findAvailableCompressorInputStreamProvidersback to summary
public static SortedMap<String, CompressorStreamProvider> findAvailableCompressorInputStreamProviders()

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, CompressorStreamProvider>

An immutable, map from names to provider objects

Since
1.13
findAvailableCompressorOutputStreamProvidersback to summary
public static SortedMap<String, CompressorStreamProvider> findAvailableCompressorOutputStreamProviders()

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, CompressorStreamProvider>

An immutable, map from names to provider objects

Since
1.13
getBrotliback to summary
public static String getBrotli()
getBzip2back to summary
public static String getBzip2()
getCompressorInputStreamProvidersback to summary
public SortedMap<String, CompressorStreamProvider> getCompressorInputStreamProviders()
getCompressorOutputStreamProvidersback to summary
public SortedMap<String, CompressorStreamProvider> getCompressorOutputStreamProviders()
getDecompressConcatenatedback to summary
pack-priv boolean getDecompressConcatenated()

For tests.

getDecompressUntilEOFback to summary
public Boolean getDecompressUntilEOF()
getDeflateback to summary
public static String getDeflate()
getDeflate64back to summary
public static String getDeflate64()
Returns:String

the constant DEFLATE64

Since
1.16
getGzipback to summary
public static String getGzip()
getInputStreamCompressorNamesback to summary
public Set<String> getInputStreamCompressorNames()

Implements org.apache.commons.compress.compressors.CompressorStreamProvider.getInputStreamCompressorNames.

Doc from org.apache.commons.compress.compressors.CompressorStreamProvider.getInputStreamCompressorNames.

Gets all the input stream compressor names for this provider

Returns:Set<String>

all the input compressor names for this provider

Annotations
@Override
getLZ4Blockback to summary
public static String getLZ4Block()
getLZ4Framedback to summary
public static String getLZ4Framed()
getLzmaback to summary
public static String getLzma()
getOutputStreamCompressorNamesback to summary
public Set<String> getOutputStreamCompressorNames()

Implements org.apache.commons.compress.compressors.CompressorStreamProvider.getOutputStreamCompressorNames.

Doc from org.apache.commons.compress.compressors.CompressorStreamProvider.getOutputStreamCompressorNames.

Gets all the output stream compressor names for this provider

Returns:Set<String>

all the output compressor names for this provider

Annotations
@Override
getPack200back to summary
public static String getPack200()
getSingletonback to summary
public static CompressorStreamFactory getSingleton()
getSnappyFramedback to summary
public static String getSnappyFramed()
getSnappyRawback to summary
public static String getSnappyRaw()
getXzback to summary
public static String getXz()
getZback to summary
public static String getZ()
getZstandardback to summary
public static String getZstandard()
putAllback to summary
pack-priv static void putAll(final Set<String> names, final CompressorStreamProvider provider, final TreeMap<String, CompressorStreamProvider> map)
setDecompressConcatenatedback to summary
public void setDecompressConcatenated(final boolean decompressConcatenated)

Deprecated

1.10 use the CompressorStreamFactory(boolean) constructor instead

Sets whether to decompress the full input or only the first stream in formats supporting multiple concatenated input streams.

This setting applies to the gzip, bzip2 and XZ formats only.

Parameters
decompressConcatenated:boolean

if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream

Annotations
@Deprecated
Exceptions
IllegalStateException:
if the constructor CompressorStreamFactory(boolean) was used to create the factory
Since
1.5
toKeyback to summary
private static String toKey(final String name)
youNeedback to summary
private static String youNeed(final String name, final String url)