# ArchiveStreamFactory (Apache Commons Compress 1.13 API)

org.apache.commons.compress.archivers

## Class ArchiveStreamFactory

- java.lang.Object
- - org.apache.commons.compress.archivers.ArchiveStreamFactory

- All Implemented Interfaces:
    ArchiveStreamProvider

      public class ArchiveStreamFactory
      extends Object
      implements ArchiveStreamProvider

  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 = new FileOutputStream(output);
       ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);

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

       os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
       IOUtils.copy(new FileInputStream(file2), os);
       os.closeArchiveEntry();
       os.close();

  Decompressing a ZIP-File:

       final InputStream is = new FileInputStream(input);
       ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is);
       ZipArchiveEntry entry = (ZipArchiveEntry)in.getNextEntry();
       OutputStream out = new FileOutputStream(new File(dir, entry.getName()));
       IOUtils.copy(in, out);
       out.close();
       in.close();

  This class is immutable
    provided that the deprecated method setEntryEncoding is not used.
  This class is thread-safe
    even if the deprecated method setEntryEncoding is used

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `AR` Constant (value "ar") used to identify the AR archive format.
    `static String` | `ARJ` Constant (value "arj") used to identify the ARJ archive format.
    `static String` | `CPIO` Constant (value "cpio") used to identify the CPIO archive format.
    `static String` | `DUMP` Constant (value "dump") used to identify the Unix DUMP archive format.
    `static String` | `JAR` Constant (value "jar") used to identify the JAR archive format.
    `static String` | `SEVEN_Z` Constant (value "7z") used to identify the 7z archive format.
    `static String` | `TAR` Constant used to identify the TAR archive format.
    `static String` | `ZIP` Constant (value "zip") used to identify the ZIP archive format.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ArchiveStreamFactory()` Create an instance using the platform default encoding.
    `ArchiveStreamFactory(String encoding)` Create an instance using the specified encoding.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `ArchiveInputStream` | `createArchiveInputStream(InputStream in)` Create an archive input stream from an input stream, autodetecting the archive type from the first few bytes of the stream.
    `ArchiveInputStream` | `createArchiveInputStream(String archiverName, InputStream in)` Creates an archive input stream from an archiver name and an input stream.
    `ArchiveInputStream` | `createArchiveInputStream(String archiverName, InputStream in, String actualEncoding)` Creates an archive input stream from an archiver name and an input stream.
    `ArchiveOutputStream` | `createArchiveOutputStream(String archiverName, OutputStream out)` Creates an archive output stream from an archiver name and an output stream.
    `ArchiveOutputStream` | `createArchiveOutputStream(String archiverName, OutputStream out, String actualEncoding)` Creates an archive output stream from an archiver name and an output stream.
    `static SortedMap<String,ArchiveStreamProvider>` | `findAvailableArchiveInputStreamProviders()` Constructs a new sorted map from input stream provider names to provider objects.
    `static SortedMap<String,ArchiveStreamProvider>` | `findAvailableArchiveOutputStreamProviders()` Constructs a new sorted map from output stream provider names to provider objects.
    `SortedMap<String,ArchiveStreamProvider>` | `getArchiveInputStreamProviders()`
    `SortedMap<String,ArchiveStreamProvider>` | `getArchiveOutputStreamProviders()`
    `String` | `getEntryEncoding()` Returns the encoding to use for arj, jar, zip, dump, cpio and tar files, or null for the archiver default.
    `Set<String>` | `getInputStreamArchiveNames()` Gets all the input stream archive names for this provider
    `Set<String>` | `getOutputStreamArchiveNames()` Gets all the output stream archive names for this provider
    `void` | `setEntryEncoding(String entryEncoding)` Deprecated. 1.10 use `ArchiveStreamFactory(String)` to specify the encoding

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### AR

          public static final String AR

      Constant (value "ar") used to identify the AR archive format.
      Since:
        1.1
      See Also:
        Constant Field Values

    - #### ARJ

          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
      See Also:
        Constant Field Values

    - #### CPIO

          public static final String CPIO

      Constant (value "cpio") used to identify the CPIO archive format.
      Since:
        1.1
      See Also:
        Constant Field Values

    - #### DUMP

          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
      See Also:
        Constant Field Values

    - #### JAR

          public static final String JAR

      Constant (value "jar") used to identify the JAR archive format.
      Since:
        1.1
      See Also:
        Constant Field Values

    - #### TAR

          public static final String TAR

      Constant used to identify the TAR archive format.
      Since:
        1.1
      See Also:
        Constant Field Values

    - #### ZIP

          public static final 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 String SEVEN_Z

      Constant (value "7z") used to identify the 7z archive format.
      Since:
        1.8
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### ArchiveStreamFactory

          public ArchiveStreamFactory()

      Create an instance using the platform default encoding.

    - #### ArchiveStreamFactory

          public ArchiveStreamFactory(String encoding)

      Create an instance using the specified encoding.
      Parameters:
        `encoding` - the encoding to be used.
      Since:
        1.10

  - ### Method Detail

    - #### findAvailableArchiveInputStreamProviders

          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:
        An immutable, map from names to provider objects
      Since:
        1.13

    - #### findAvailableArchiveOutputStreamProviders

          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:
        An immutable, map from names to provider objects
      Since:
        1.13

    - #### getEntryEncoding

          public String getEntryEncoding()

      Returns 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.5

    - #### setEntryEncoding

          @Deprecated
          public void setEntryEncoding(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` - the entry encoding, null uses the archiver default.
      Throws:
        `IllegalStateException` - if the constructor `ArchiveStreamFactory(String)` was used to specify the factory encoding.
      Since:
        1.5

    - #### createArchiveInputStream

          public ArchiveInputStream createArchiveInputStream(String archiverName,
                                                    InputStream in)
                                                      throws ArchiveException

      Creates an archive input stream from an archiver name and an input stream.
      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 known
        `StreamingNotSupportedException` - if the format cannot be read from a stream
        `IllegalArgumentException` - if the archiver name or stream is null

    - #### createArchiveInputStream

          public ArchiveInputStream createArchiveInputStream(String archiverName,
                                                    InputStream in,
                                                    String actualEncoding)
                                                      throws ArchiveException

      Description copied from interface: `ArchiveStreamProvider`
      Creates an archive input stream from an archiver name and an input stream.
      Specified by:
        `createArchiveInputStream` in interface `ArchiveStreamProvider`
      Parameters:
        `archiverName` - the archive name, i.e. "ar", "arj", "zip", "tar", "jar", "cpio", "dump" or "7z"
        `in` - the input stream
        `actualEncoding` - encoding name or null for the default
      Returns:
        the archive input stream
      Throws:
        `ArchiveException` - if the archiver name is not known
        `StreamingNotSupportedException` - if the format cannot be read from a stream

    - #### createArchiveOutputStream

          public ArchiveOutputStream createArchiveOutputStream(String archiverName,
                                                      OutputStream out)
                                                        throws ArchiveException

      Creates an archive output stream from an archiver name and an output stream.
      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 known
        `StreamingNotSupportedException` - if the format cannot be written to a stream
        `IllegalArgumentException` - if the archiver name or stream is null

    - #### createArchiveOutputStream

          public ArchiveOutputStream createArchiveOutputStream(String archiverName,
                                                      OutputStream out,
                                                      String actualEncoding)
                                                        throws ArchiveException

      Description copied from interface: `ArchiveStreamProvider`
      Creates an archive output stream from an archiver name and an output stream.
      Specified by:
        `createArchiveOutputStream` in interface `ArchiveStreamProvider`
      Parameters:
        `archiverName` - the archive name, i.e. "ar", "zip", "tar", "jar" or "cpio"
        `out` - the output stream
        `actualEncoding` - encoding name or null for the default
      Returns:
        the archive output stream
      Throws:
        `ArchiveException` - if the archiver name is not known
        `StreamingNotSupportedException` - if the format cannot be written to a stream

    - #### createArchiveInputStream

          public ArchiveInputStream createArchiveInputStream(InputStream in)
                                                      throws ArchiveException

      Create 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:
        `in` - the input stream
      Returns:
        the archive input stream
      Throws:
        `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

    - #### getArchiveInputStreamProviders

          public SortedMap<String,ArchiveStreamProvider> getArchiveInputStreamProviders()

    - #### getArchiveOutputStreamProviders

          public SortedMap<String,ArchiveStreamProvider> getArchiveOutputStreamProviders()

    - #### getInputStreamArchiveNames

          public Set<String> getInputStreamArchiveNames()

      Description copied from interface: `ArchiveStreamProvider`
      Gets all the input stream archive names for this provider
      Specified by:
        `getInputStreamArchiveNames` in interface `ArchiveStreamProvider`
      Returns:
        all the input archive names for this provider

    - #### getOutputStreamArchiveNames

          public Set<String> getOutputStreamArchiveNames()

      Description copied from interface: `ArchiveStreamProvider`
      Gets all the output stream archive names for this provider
      Specified by:
        `getOutputStreamArchiveNames` in interface `ArchiveStreamProvider`
      Returns:
        all the output archive names for this provider

Copyright © 2016 The Apache Software Foundation. All rights reserved.