- java.lang.Object
-
- kala.compress.archivers.ArchiveStreamFactory
-
- All Implemented Interfaces:
ArchiveStreamProvider
public class ArchiveStreamFactory extends java.lang.Object implements ArchiveStreamProvider
Creates an 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();
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classArchiveStreamFactory.BuiltinArchiverThis is an internal class and should not be used directly.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Set<java.lang.String>ALL_NAMESstatic java.lang.StringAPKConstant (value "apk") used to identify the APK archive format.static java.lang.StringAPKMConstant (value "apkm") used to identify the APKM archive format.static java.lang.StringAPKSConstant (value "apks") used to identify the APKS archive format.static java.lang.StringARConstant (value "ar") used to identify the AR archive format.private java.util.SortedMap<java.lang.String,ArchiveStreamProvider>archiveInputStreamProvidersprivate java.util.SortedMap<java.lang.String,ArchiveStreamProvider>archiveOutputStreamProvidersstatic java.lang.StringARJConstant (value "arj") used to identify the ARJ archive format.private static java.util.Map<java.lang.String,ArchiveStreamFactory.BuiltinArchiver>BUILTIN_ARCHIVERSstatic java.lang.StringCPIOConstant (value "cpio") used to identify the CPIO archive format.static ArchiveStreamFactoryDEFAULTThe singleton instance using the UTF-8.static java.lang.StringDUMPConstant (value "dump") used to identify the UNIX DUMP archive format.private static ArchiveStreamFactory.BuiltinArchiverDUMP_ARCHIVERprivate static intDUMP_SIGNATURE_SIZEprivate java.nio.charset.CharsetentryEncodingEntry encoding, null for the default.private java.util.Set<java.lang.String>inputStreamArchiveNamesstatic java.lang.StringJARConstant (value "jar") used to identify the JAR archive format.private static java.util.Set<java.lang.String>OUTPUT_NAMESprivate java.util.Set<java.lang.String>outputStreamArchiveNamesstatic java.lang.StringSEVEN_ZConstant (value "7z") used to identify the 7z archive format.private static intSIGNATURE_SIZEstatic java.lang.StringTARConstant used to identify the TAR archive format.private static ArchiveStreamFactory.BuiltinArchiverTAR_ARCHIVERprivate static intTAR_HEADER_SIZEstatic java.lang.StringXAPKConstant (value "xapk") used to identify the XAPK archive format.static java.lang.StringZIPConstant (value "zip") used to identify the ZIP archive format.
-
Constructor Summary
Constructors Constructor Description ArchiveStreamFactory()Constructs an instance using the archiver default encoding.ArchiveStreamFactory(@Nullable java.nio.charset.Charset entryEncoding)Constructs an instance using the specified encoding.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <I extends ArchiveInputStream<? extends ArchiveEntry>>
IcreateArchiveInputStream(java.io.InputStream in)Creates an archive input stream from an input stream, autodetecting the archive type from the first few bytes of the stream.<I extends ArchiveInputStream<? extends ArchiveEntry>>
IcreateArchiveInputStream(java.lang.String archiverName, java.io.InputStream in)Creates an archive input stream from an archiver name and an input stream.<I extends ArchiveInputStream<? extends ArchiveEntry>>
IcreateArchiveInputStream(java.lang.String archiverName, java.io.InputStream in, java.nio.charset.Charset actualEncoding)Creates an archive input stream from an archiver name and an input stream.<O extends ArchiveOutputStream<? extends ArchiveEntry>>
OcreateArchiveOutputStream(java.lang.String archiverName, java.io.OutputStream out)Creates an archive output stream from an archiver name and an output stream.<O extends ArchiveOutputStream<? extends ArchiveEntry>>
OcreateArchiveOutputStream(java.lang.String archiverName, java.io.OutputStream out, java.nio.charset.Charset actualEncoding)Creates an archive output stream from an archiver name and an output stream.static java.lang.Stringdetect(java.io.InputStream in)Try to determine the type of Archiverjava.util.SortedMap<java.lang.String,ArchiveStreamProvider>getArchiveInputStreamProviders()Gets an unmodifiable sorted map from input stream provider names to provider objects.java.util.SortedMap<java.lang.String,ArchiveStreamProvider>getArchiveOutputStreamProviders()Gets an unmodifiable sorted map from output stream provider names to provider objects.java.nio.charset.CharsetgetEntryEncoding()Gets the encoding to use for arj, jar, ZIP, dump, cpio and tar files, or null for the archiver default.java.util.Set<java.lang.String>getInputStreamArchiveNames()Gets all the input stream archive names for this providerjava.util.Set<java.lang.String>getOutputStreamArchiveNames()Gets all the output stream archive names for this providerprivate static ArchiveStreamFactory.BuiltinArchiverloadArchiver(java.util.List<ArchiveStreamFactory.BuiltinArchiver> archivers, java.lang.String className)private static java.lang.StringtoKey(java.lang.String name)ArchiveStreamFactorywithInstalledProviders()ArchiveStreamFactorywithInstalledProviders(java.lang.ClassLoader classLoader)ArchiveStreamFactorywithProviders(java.lang.Iterable<? extends ArchiveStreamProvider> providers)
-
-
-
Field Detail
-
TAR_HEADER_SIZE
private static final int TAR_HEADER_SIZE
- See Also:
- Constant Field Values
-
DUMP_SIGNATURE_SIZE
private static final int DUMP_SIGNATURE_SIZE
- See Also:
- Constant Field Values
-
SIGNATURE_SIZE
private static final int SIGNATURE_SIZE
- See Also:
- Constant Field Values
-
APK
public static final java.lang.String APK
Constant (value "apk") used to identify the APK archive format.APK file extensions are .apk, .xapk, .apks, .apkm
- Since:
- 1.22
- See Also:
- Constant Field Values
-
XAPK
public static final java.lang.String XAPK
Constant (value "xapk") used to identify the XAPK archive format.APK file extensions are .apk, .xapk, .apks, .apkm
- Since:
- 1.22
- See Also:
- Constant Field Values
-
APKS
public static final java.lang.String APKS
Constant (value "apks") used to identify the APKS archive format.APK file extensions are .apk, .xapk, .apks, .apkm
- Since:
- 1.22
- See Also:
- Constant Field Values
-
APKM
public static final java.lang.String APKM
Constant (value "apkm") used to identify the APKM archive format.APK file extensions are .apk, .xapk, .apks, .apkm
- Since:
- 1.22
- See Also:
- Constant Field Values
-
AR
public static final java.lang.String AR
Constant (value "ar") used to identify the AR archive format.- Since:
- 1.1
- See Also:
- Constant Field Values
-
ARJ
public static final java.lang.String ARJ
Constant (value "arj") used to identify the ARJ archive format. Not supported as an output stream type.- Since:
- 1.6
- See Also:
- Constant Field Values
-
CPIO
public static final java.lang.String CPIO
Constant (value "cpio") used to identify the CPIO archive format.- Since:
- 1.1
- See Also:
- Constant Field Values
-
DUMP
public static final java.lang.String DUMP
Constant (value "dump") used to identify the UNIX DUMP archive format. Not supported as an output stream type.- Since:
- 1.3
- See Also:
- Constant Field Values
-
JAR
public static final java.lang.String JAR
Constant (value "jar") used to identify the JAR archive format.- Since:
- 1.1
- See Also:
- Constant Field Values
-
TAR
public static final java.lang.String TAR
Constant used to identify the TAR archive format.- Since:
- 1.1
- See Also:
- Constant Field Values
-
ZIP
public static final java.lang.String ZIP
Constant (value "zip") used to identify the ZIP archive format.- Since:
- 1.1
- See Also:
- Constant Field Values
-
SEVEN_Z
public static final java.lang.String SEVEN_Z
Constant (value "7z") used to identify the 7z archive format.- Since:
- 1.8
- See Also:
- Constant Field Values
-
BUILTIN_ARCHIVERS
private static final java.util.Map<java.lang.String,ArchiveStreamFactory.BuiltinArchiver> BUILTIN_ARCHIVERS
-
ALL_NAMES
private static final java.util.Set<java.lang.String> ALL_NAMES
-
OUTPUT_NAMES
private static final java.util.Set<java.lang.String> OUTPUT_NAMES
-
TAR_ARCHIVER
private static final ArchiveStreamFactory.BuiltinArchiver TAR_ARCHIVER
-
DUMP_ARCHIVER
private static final ArchiveStreamFactory.BuiltinArchiver DUMP_ARCHIVER
-
DEFAULT
public static final ArchiveStreamFactory DEFAULT
The singleton instance using the UTF-8.- Since:
- 1.21
-
entryEncoding
private final java.nio.charset.Charset entryEncoding
Entry encoding, null for the default.
-
archiveInputStreamProviders
private final java.util.SortedMap<java.lang.String,ArchiveStreamProvider> archiveInputStreamProviders
-
archiveOutputStreamProviders
private final java.util.SortedMap<java.lang.String,ArchiveStreamProvider> archiveOutputStreamProviders
-
inputStreamArchiveNames
private final java.util.Set<java.lang.String> inputStreamArchiveNames
-
outputStreamArchiveNames
private final java.util.Set<java.lang.String> outputStreamArchiveNames
-
-
Constructor Detail
-
ArchiveStreamFactory
public ArchiveStreamFactory()
Constructs an instance using the archiver default encoding.
-
ArchiveStreamFactory
public ArchiveStreamFactory(@Nullable @Nullable java.nio.charset.Charset entryEncoding)Constructs an instance using the specified encoding.- Parameters:
entryEncoding- the encoding to be used.- Since:
- 1.27.1-0
-
-
Method Detail
-
loadArchiver
private static ArchiveStreamFactory.BuiltinArchiver loadArchiver(java.util.List<ArchiveStreamFactory.BuiltinArchiver> archivers, java.lang.String className)
-
detect
public static java.lang.String detect(java.io.InputStream in) throws ArchiveExceptionTry to determine the type of Archiver- Parameters:
in- input stream- Returns:
- type of archiver if found
- Throws:
ArchiveException- if an archiver cannot be detected in the stream- Since:
- 1.14
-
toKey
private static java.lang.String toKey(java.lang.String name)
-
createArchiveInputStream
public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInputStream(java.io.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.- Type Parameters:
I- TheArchiveInputStreamtype.- Parameters:
in- the input stream- Returns:
- the archive input stream
- Throws:
ArchiveException- if the archiver name is not knownStreamingNotSupportedException- if the format cannot be read from a streamjava.lang.IllegalArgumentException- if the stream is null or does not support mark
-
createArchiveInputStream
public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInputStream(java.lang.String archiverName, java.io.InputStream in) throws ArchiveException
Creates an archive input stream from an archiver name and an input stream.- Type Parameters:
I- TheArchiveInputStreamtype.- Parameters:
archiverName- the archive name, i.e. "ar", "arj", "zip", "tar", "jar", "cpio", "dump" or "7z"in- the input stream- Returns:
- the archive input stream
- Throws:
ArchiveException- if the archiver name is not knownStreamingNotSupportedException- if the format cannot be read from a streamjava.lang.IllegalArgumentException- if the archiver name or stream is null
-
createArchiveInputStream
public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInputStream(java.lang.String archiverName, java.io.InputStream in, java.nio.charset.Charset actualEncoding) throws ArchiveException
Description copied from interface:ArchiveStreamProviderCreates an archive input stream from an archiver name and an input stream.- Specified by:
createArchiveInputStreamin interfaceArchiveStreamProvider- Type Parameters:
I- TheArchiveInputStreamtype.- Parameters:
archiverName- the archiver name, i.e. "ar", "arj", "zip", "tar", "jar", "cpio", "dump" or "7z"in- the input streamactualEncoding- encoding name or null for the default- Returns:
- the archive input stream
- Throws:
ArchiveException- if the archiver name is not knownStreamingNotSupportedException- if the format cannot be read from a stream
-
createArchiveOutputStream
public <O extends ArchiveOutputStream<? extends ArchiveEntry>> O createArchiveOutputStream(java.lang.String archiverName, java.io.OutputStream out) throws ArchiveException
Creates an archive output stream from an archiver name and an output stream.- Type Parameters:
O- TheArchiveOutputStreamtype.- Parameters:
archiverName- the archive name, i.e. "ar", "zip", "tar", "jar" or "cpio"out- the output stream- Returns:
- the archive output stream
- Throws:
ArchiveException- if the archiver name is not knownStreamingNotSupportedException- if the format cannot be written to a streamjava.lang.IllegalArgumentException- if the archiver name or stream is null
-
createArchiveOutputStream
public <O extends ArchiveOutputStream<? extends ArchiveEntry>> O createArchiveOutputStream(java.lang.String archiverName, java.io.OutputStream out, java.nio.charset.Charset actualEncoding) throws ArchiveException
Description copied from interface:ArchiveStreamProviderCreates an archive output stream from an archiver name and an output stream.- Specified by:
createArchiveOutputStreamin interfaceArchiveStreamProvider- Type Parameters:
O- TheArchiveInputStreamtype.- Parameters:
archiverName- the archiver name, i.e. "ar", "zip", "tar", "jar" or "cpio"out- the output streamactualEncoding- encoding name or null for the default- Returns:
- the archive output stream
- Throws:
ArchiveException- if the archiver name is not knownStreamingNotSupportedException- if the format cannot be written to a stream
-
getArchiveInputStreamProviders
public java.util.SortedMap<java.lang.String,ArchiveStreamProvider> getArchiveInputStreamProviders()
Gets an unmodifiable sorted map from input stream provider names to provider objects.- Returns:
- an unmodifiable sorted map of from input stream provider names to provider objects.
-
getArchiveOutputStreamProviders
public java.util.SortedMap<java.lang.String,ArchiveStreamProvider> getArchiveOutputStreamProviders()
Gets an unmodifiable sorted map from output stream provider names to provider objects.- Returns:
- an unmodifiable sorted map of from input stream provider names to provider objects.
-
getEntryEncoding
public java.nio.charset.Charset getEntryEncoding()
Gets the encoding to use for arj, jar, ZIP, dump, cpio and tar files, or null for the archiver default.- Returns:
- entry encoding, or null for the archiver default
- Since:
- 1.27.1-0
-
getInputStreamArchiveNames
public java.util.Set<java.lang.String> getInputStreamArchiveNames()
Description copied from interface:ArchiveStreamProviderGets all the input stream archive names for this provider- Specified by:
getInputStreamArchiveNamesin interfaceArchiveStreamProvider- Returns:
- all the input archive names for this provider
-
getOutputStreamArchiveNames
public java.util.Set<java.lang.String> getOutputStreamArchiveNames()
Description copied from interface:ArchiveStreamProviderGets all the output stream archive names for this provider- Specified by:
getOutputStreamArchiveNamesin interfaceArchiveStreamProvider- Returns:
- all the output archive names for this provider
-
withInstalledProviders
@Experimental public ArchiveStreamFactory withInstalledProviders()
- Since:
- 1.27.1-0
-
withInstalledProviders
@Experimental public ArchiveStreamFactory withInstalledProviders(java.lang.ClassLoader classLoader)
- Since:
- 1.27.1-0
-
withProviders
@Experimental public ArchiveStreamFactory withProviders(java.lang.Iterable<? extends ArchiveStreamProvider> providers)
- Since:
- 1.27.1-0
-
-