# SevenZOutputFile (Apache Commons Compress 1.13 API)

org.apache.commons.compress.archivers.sevenz

## Class SevenZOutputFile

- java.lang.Object
- - org.apache.commons.compress.archivers.sevenz.SevenZOutputFile

- All Implemented Interfaces:
    Closeable, AutoCloseable

      public class SevenZOutputFile
      extends Object
      implements Closeable

  Writes a 7z file.
  Since:
    1.6

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SevenZOutputFile(File filename)` Opens file to write a 7z archive to.
    `SevenZOutputFile(SeekableByteChannel channel)` Prepares channel to write a 7z archive to.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Closes the archive, calling `finish()` if necessary.
    `void` | `closeArchiveEntry()` Closes the archive entry.
    `SevenZArchiveEntry` | `createArchiveEntry(File inputFile, String entryName)` Create an archive entry using the inputFile and entryName provided.
    `void` | `finish()` Finishes the addition of entries to this archive, without closing it.
    `void` | `putArchiveEntry(ArchiveEntry archiveEntry)` Records an archive entry to add.
    `void` | `setContentCompression(SevenZMethod method)` Sets the default compression method to use for entry contents - the default is LZMA2.
    `void` | `setContentMethods(Iterable<? extends SevenZMethodConfiguration> methods)` Sets the default (compression) methods to use for entry contents - the default is LZMA2.
    `void` | `write(byte[] b)` Writes a byte array to the current archive entry.
    `void` | `write(byte[] b, int off, int len)` Writes part of a byte array to the current archive entry.
    `void` | `write(int b)` Writes a byte to the current archive entry.

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

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

- - ### Constructor Detail

    - #### SevenZOutputFile

          public SevenZOutputFile(File filename)
                           throws IOException

      Opens file to write a 7z archive to.
      Parameters:
        `filename` - the file to write to
      Throws:
        `IOException` - if opening the file fails

    - #### SevenZOutputFile

          public SevenZOutputFile(SeekableByteChannel channel)
                           throws IOException

      Prepares channel to write a 7z archive to.

      `SeekableInMemoryByteChannel` allows you to write to an in-memory archive.

      Parameters:
        `channel` - the channel to write to
      Throws:
        `IOException` - if the channel cannot be positioned properly
      Since:
        1.13

  - ### Method Detail

    - #### setContentCompression

          public void setContentCompression(SevenZMethod method)

      Sets the default compression method to use for entry contents - the default is LZMA2.

      Currently only `SevenZMethod.COPY`, `SevenZMethod.LZMA2`, `SevenZMethod.BZIP2` and `SevenZMethod.DEFLATE` are supported.

      This is a short form for passing a single-element iterable to `setContentMethods(java.lang.Iterable<? extends org.apache.commons.compress.archivers.sevenz.SevenZMethodConfiguration>)`.

      Parameters:
        `method` - the default compression method

    - #### setContentMethods

          public void setContentMethods(Iterable<? extends SevenZMethodConfiguration> methods)

      Sets the default (compression) methods to use for entry contents - the default is LZMA2.

      Currently only `SevenZMethod.COPY`, `SevenZMethod.LZMA2`, `SevenZMethod.BZIP2` and `SevenZMethod.DEFLATE` are supported.

      The methods will be consulted in iteration order to create the final output.

      Parameters:
        `methods` - the default (compression) methods
      Since:
        1.8

    - #### close

          public void close()
                     throws IOException

      Closes the archive, calling `finish()` if necessary.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `IOException` - on error

    - #### createArchiveEntry

          public SevenZArchiveEntry createArchiveEntry(File inputFile,
                                              String entryName)
                                                throws IOException

      Create an archive entry using the inputFile and entryName provided.
      Parameters:
        `inputFile` - file to create an entry from
        `entryName` - the name to use
      Returns:
        the ArchiveEntry set up with details from the file
      Throws:
        `IOException` - on error

    - #### putArchiveEntry

          public void putArchiveEntry(ArchiveEntry archiveEntry)
                               throws IOException

      Records an archive entry to add. The caller must then write the content to the archive and call `closeArchiveEntry()` to complete the process.
      Parameters:
        `archiveEntry` - describes the entry
      Throws:
        `IOException` - on error

    - #### closeArchiveEntry

          public void closeArchiveEntry()
                                 throws IOException

      Closes the archive entry.
      Throws:
        `IOException` - on error

    - #### write

          public void write(int b)
                     throws IOException

      Writes a byte to the current archive entry.
      Parameters:
        `b` - The byte to be written.
      Throws:
        `IOException` - on error

    - #### write

          public void write(byte[] b)
                     throws IOException

      Writes a byte array to the current archive entry.
      Parameters:
        `b` - The byte array to be written.
      Throws:
        `IOException` - on error

    - #### write

          public void write(byte[] b,
                   int off,
                   int len)
                     throws IOException

      Writes part of a byte array to the current archive entry.
      Parameters:
        `b` - The byte array to be written.
        `off` - offset into the array to start writing from
        `len` - number of bytes to write
      Throws:
        `IOException` - on error

    - #### finish

          public void finish()
                      throws IOException

      Finishes the addition of entries to this archive, without closing it.
      Throws:
        `IOException` - if archive is already closed.

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