Package org.apache.commons.compress.archivers.zip

# Class StreamCompressor

java.lang.Object
org.apache.commons.compress.archivers.zip.StreamCompressor

All Implemented Interfaces:
  `Closeable`, `AutoCloseable`

public abstract class StreamCompressor extends Object implements Closeable
Encapsulates a `Deflater` and crc calculator, handling multiple types of output streams. Currently `ZipEntry.DEFLATED` and `ZipEntry.STORED` are the only supported compression methods.

Since:
  1.10

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  `static StreamCompressor`
  `create(int compressionLevel, ScatterGatherBackingStore bs)`
  Create a stream compressor with the given compression level.
  `static StreamCompressor`
  `create(ScatterGatherBackingStore bs)`
  Create a stream compressor with the default compression level.
  `void`
  `deflate(InputStream source, int method)`
  Deflate the given source using the supplied compression method
  `long`
  `getBytesRead()`
  Return the number of bytes read from the source stream
  `long`
  `getBytesWrittenForLastEntry()`
  The number of bytes written to the output for the last entry
  `long`
  `getCrc32()`
  The crc32 of the last deflated file
  `long`
  `getTotalBytesWritten()`
  The total number of bytes written to the output for all files
  `void`
  `writeCounted(byte[] data)`
  `void`
  `writeCounted(byte[] data, int offset, int length)`
  `protected abstract void`
  `writeOut(byte[] data, int offset, int length)`

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

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

- ## Method Details

  - ### create

    public static StreamCompressor create(int compressionLevel, ScatterGatherBackingStore bs)
    Create a stream compressor with the given compression level.
    Parameters:
      `compressionLevel` - The `Deflater` compression level
      `bs` - The ScatterGatherBackingStore to receive output
    Returns:
      A stream compressor

  - ### create

    public static StreamCompressor create(ScatterGatherBackingStore bs)
    Create a stream compressor with the default compression level.
    Parameters:
      `bs` - The ScatterGatherBackingStore to receive output
    Returns:
      A stream compressor

  - ### close

    public void close() throws IOException
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`
    Throws:
      `IOException`

  - ### deflate

    public void deflate(InputStream source, int method) throws IOException
    Deflate the given source using the supplied compression method
    Parameters:
      `source` - The source to compress
      `method` - The #ZipArchiveEntry compression method
    Throws:
      `IOException` - When failures happen

  - ### getBytesRead

    public long getBytesRead()
    Return the number of bytes read from the source stream
    Returns:
      The number of bytes read, never negative

  - ### getBytesWrittenForLastEntry

    public long getBytesWrittenForLastEntry()
    The number of bytes written to the output for the last entry
    Returns:
      The number of bytes, never negative

  - ### getCrc32

    public long getCrc32()
    The crc32 of the last deflated file
    Returns:
      the crc32

  - ### getTotalBytesWritten

    public long getTotalBytesWritten()
    The total number of bytes written to the output for all files
    Returns:
      The number of bytes, never negative

  - ### writeCounted

    public void writeCounted(byte[] data) throws IOException
    Throws:
      `IOException`

  - ### writeCounted

    public void writeCounted(byte[] data, int offset, int length) throws IOException
    Throws:
      `IOException`

  - ### writeOut

    protected abstract void writeOut(byte[] data, int offset, int length) throws IOException
    Throws:
      `IOException`