类 ZipOutputStream

所有已实现的接口:
Closeable, Flushable, AutoCloseable

public class ZipOutputStream extends FilterOutputStream
Reimplementation of java.util.zip.ZipOutputStream that does handle the extended functionality of this package, especially internal/external file attributes and extra fields with different layouts for local file data and central directory entries.

This class will try to use RandomAccessFile when you know that the output is going to go to a file.

If RandomAccessFile cannot be used, this implementation will use a Data Descriptor to store size and CRC information for DEFLATED entries, this means, you don't need to calculate them yourself. Unfortunately this is not possible for the STORED method, here setting the CRC and uncompressed size information is required before putNextEntry can be called.

  • 嵌套类概要

    嵌套类
    修饰符和类型
    说明
    static final class 
    enum that represents the possible policies for creating Unicode extra fields.
  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    protected byte[]
    This buffer servers as a Deflater.
    protected static final byte[]
    central file header signature
    protected static final byte[]
    data descriptor signature
    protected Deflater
    This Deflater object is used for output.
    static final int
    Default compression level for deflated entries.
    static final int
    Compression method for deflated entries.
    static final int
    General purpose flag, which indicates that filenames are written in utf-8.
    protected static final byte[]
    end of central dir signature
    protected static final byte[]
    local file header signature
    static final int
    Compression method for stored entries.
    static final int
    General purpose flag, which indicates that filenames are written in utf-8.

    从类继承的字段 java.io.FilterOutputStream

    out
  • 构造器概要

    构造器
    构造器
    说明
    Creates a new ZIP OutputStream writing to a File.
    Creates a new ZIP OutputStream filtering the underlying stream.
  • 方法概要

    修饰符和类型
    方法
    说明
    protected static long
    adjustToLong(int i)
    Assumes a negative integer really is a positive integer that has wrapped around and re-creates the original value.
    void
    Closes this output stream and releases any system resources associated with the stream.
    void
    Writes all necessary data for this entry.
    protected final void
    Writes next block of compressed data to the output stream.
    void
    Finishs writing the contents and closes this as well as the underlying stream.
    void
    Flushes this output stream and forces any buffered output bytes to be written out to the stream.
    protected byte[]
    Retrieve the bytes for the given String in the encoding set for this Stream.
    The encoding to use for filenames and the file comment.
    boolean
    This method indicates whether this archive is writing to a seekable stream (i.e., to a random access file).
    void
    Begin writing next entry.
    void
    setComment(String comment)
    Set the file comment.
    void
    Whether to create Unicode Extra Fields.
    void
    setEncoding(String encoding)
    The encoding to use for filenames and the file comment.
    void
    setFallbackToUTF8(boolean b)
    Whether to fall back to UTF and the language encoding flag if the file name cannot be encoded using the specified encoding.
    void
    setLevel(int level)
    Sets the compression level for subsequent entries.
    void
    setMethod(int method)
    Sets the default compression method for subsequent entries.
    void
    Whether to set the language encoding flag if the file name encoding is UTF-8.
    protected static byte[]
    toDosTime(long t)
    Convert a Date object to a DOS date/time field.
    protected static ZipLong
    Convert a Date object to a DOS date/time field.
    void
    write(byte[] b, int offset, int length)
    Writes bytes to ZIP entry.
    void
    write(int b)
    Writes a single byte to ZIP entry.
    protected void
    Writes the "End of central dir record".
    protected void
    Writes the central file header entry.
    protected void
    Writes the data descriptor entry.
    protected void
    Writes the local file header entry
    protected final void
    writeOut(byte[] data)
    Write bytes to output or random access file.
    protected final void
    writeOut(byte[] data, int offset, int length)
    Write bytes to output or random access file.

    从类继承的方法 java.io.FilterOutputStream

    write

    从类继承的方法 java.io.OutputStream

    nullOutputStream

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 字段详细资料

    • DEFLATED

      public static final int DEFLATED
      Compression method for deflated entries.
      从以下版本开始:
      1.1
      另请参阅:
    • DEFAULT_COMPRESSION

      public static final int DEFAULT_COMPRESSION
      Default compression level for deflated entries.
      从以下版本开始:
      Ant 1.7
      另请参阅:
    • STORED

      public static final int STORED
      Compression method for stored entries.
      从以下版本开始:
      1.1
      另请参阅:
    • UFT8_NAMES_FLAG

      public static final int UFT8_NAMES_FLAG
      General purpose flag, which indicates that filenames are written in utf-8.
      另请参阅:
    • EFS_FLAG

      public static final int EFS_FLAG
      General purpose flag, which indicates that filenames are written in utf-8. use UFT8_NAMES_FLAG instead
      另请参阅:
    • def

      protected Deflater def
      This Deflater object is used for output.

      This attribute is only protected to provide a level of API backwards compatibility. This class used to extend DeflaterOutputStream up to Revision 1.13.

      从以下版本开始:
      1.14
    • buf

      protected byte[] buf
      This buffer servers as a Deflater.

      This attribute is only protected to provide a level of API backwards compatibility. This class used to extend DeflaterOutputStream up to Revision 1.13.

      从以下版本开始:
      1.14
    • LFH_SIG

      protected static final byte[] LFH_SIG
      local file header signature
      从以下版本开始:
      1.1
    • DD_SIG

      protected static final byte[] DD_SIG
      data descriptor signature
      从以下版本开始:
      1.1
    • CFH_SIG

      protected static final byte[] CFH_SIG
      central file header signature
      从以下版本开始:
      1.1
    • EOCD_SIG

      protected static final byte[] EOCD_SIG
      end of central dir signature
      从以下版本开始:
      1.1
  • 构造器详细资料

    • ZipOutputStream

      public ZipOutputStream(OutputStream out)
      Creates a new ZIP OutputStream filtering the underlying stream.
      参数:
      out - the outputstream to zip
      从以下版本开始:
      1.1
    • ZipOutputStream

      public ZipOutputStream(File file) throws IOException
      Creates a new ZIP OutputStream writing to a File. Will use random access if possible.
      参数:
      file - the file to zip to
      抛出:
      IOException - on error
      从以下版本开始:
      1.14
  • 方法详细资料

    • isSeekable

      public boolean isSeekable()
      This method indicates whether this archive is writing to a seekable stream (i.e., to a random access file).

      For seekable streams, you don't need to calculate the CRC or uncompressed size for STORED entries before invoking putNextEntry(com.sh.common.zip.ZipEntry).

      返回:
      true if seekable
      从以下版本开始:
      1.17
    • setEncoding

      public void setEncoding(String encoding)
      The encoding to use for filenames and the file comment.

      For a list of possible values see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html. Defaults to the platform's default character encoding.

      参数:
      encoding - the encoding value
      从以下版本开始:
      1.3
    • getEncoding

      public String getEncoding()
      The encoding to use for filenames and the file comment.
      返回:
      null if using the platform's default character encoding.
      从以下版本开始:
      1.3
    • setUseLanguageEncodingFlag

      public void setUseLanguageEncodingFlag(boolean b)
      Whether to set the language encoding flag if the file name encoding is UTF-8.

      Defaults to true.

      参数:
      b - boolean
    • setCreateUnicodeExtraFields

      public void setCreateUnicodeExtraFields(ZipOutputStream.UnicodeExtraFieldPolicy b)
      Whether to create Unicode Extra Fields.

      Defaults to NEVER.

      参数:
      b - UnicodeExtraFieldPolicy
    • setFallbackToUTF8

      public void setFallbackToUTF8(boolean b)
      Whether to fall back to UTF and the language encoding flag if the file name cannot be encoded using the specified encoding.

      Defaults to false.

      参数:
      b - boolean
    • finish

      public void finish() throws IOException
      Finishs writing the contents and closes this as well as the underlying stream.
      抛出:
      IOException - on error
      从以下版本开始:
      1.1
    • closeEntry

      public void closeEntry() throws IOException
      Writes all necessary data for this entry.
      抛出:
      IOException - on error
      从以下版本开始:
      1.1
    • putNextEntry

      public void putNextEntry(ZipEntry ze) throws IOException
      Begin writing next entry.
      参数:
      ze - the entry to write
      抛出:
      IOException - on error
      从以下版本开始:
      1.1
    • setComment

      public void setComment(String comment)
      Set the file comment.
      参数:
      comment - the comment
      从以下版本开始:
      1.1
    • setLevel

      public void setLevel(int level)
      Sets the compression level for subsequent entries.

      Default is Deflater.DEFAULT_COMPRESSION.

      参数:
      level - the compression level.
      抛出:
      IllegalArgumentException - if an invalid compression level is specified.
      从以下版本开始:
      1.1
    • setMethod

      public void setMethod(int method)
      Sets the default compression method for subsequent entries.

      Default is DEFLATED.

      参数:
      method - an int from java.util.zip.ZipEntry
      从以下版本开始:
      1.1
    • write

      public void write(byte[] b, int offset, int length) throws IOException
      Writes bytes to ZIP entry.
      覆盖:
      write 在类中 FilterOutputStream
      参数:
      b - the byte array to write
      offset - the start position to write from
      length - the number of bytes to write
      抛出:
      IOException - on error
    • write

      public void write(int b) throws IOException
      Writes a single byte to ZIP entry.

      Delegates to the three arg method.

      覆盖:
      write 在类中 FilterOutputStream
      参数:
      b - the byte to write
      抛出:
      IOException - on error
      从以下版本开始:
      1.14
    • close

      public void close() throws IOException
      Closes this output stream and releases any system resources associated with the stream.
      指定者:
      close 在接口中 AutoCloseable
      指定者:
      close 在接口中 Closeable
      覆盖:
      close 在类中 FilterOutputStream
      抛出:
      IOException - if an I/O error occurs.
      从以下版本开始:
      1.14
    • flush

      public void flush() throws IOException
      Flushes this output stream and forces any buffered output bytes to be written out to the stream.
      指定者:
      flush 在接口中 Flushable
      覆盖:
      flush 在类中 FilterOutputStream
      抛出:
      IOException - if an I/O error occurs.
      从以下版本开始:
      1.14
    • deflate

      protected final void deflate() throws IOException
      Writes next block of compressed data to the output stream.
      抛出:
      IOException - on error
      从以下版本开始:
      1.14
    • writeLocalFileHeader

      protected void writeLocalFileHeader(ZipEntry ze) throws IOException
      Writes the local file header entry
      参数:
      ze - the entry to write
      抛出:
      IOException - on error
      从以下版本开始:
      1.1
    • writeDataDescriptor

      protected void writeDataDescriptor(ZipEntry ze) throws IOException
      Writes the data descriptor entry.
      参数:
      ze - the entry to write
      抛出:
      IOException - on error
      从以下版本开始:
      1.1
    • writeCentralFileHeader

      protected void writeCentralFileHeader(ZipEntry ze) throws IOException
      Writes the central file header entry.
      参数:
      ze - the entry to write
      抛出:
      IOException - on error
      从以下版本开始:
      1.1
    • writeCentralDirectoryEnd

      protected void writeCentralDirectoryEnd() throws IOException
      Writes the "End of central dir record".
      抛出:
      IOException - on error
      从以下版本开始:
      1.1
    • toDosTime

      protected static ZipLong toDosTime(Date time)
      Convert a Date object to a DOS date/time field.
      参数:
      time - the Date to convert
      返回:
      the date as a ZipLong
      从以下版本开始:
      1.1
    • toDosTime

      protected static byte[] toDosTime(long t)
      Convert a Date object to a DOS date/time field.

      Stolen from InfoZip's fileio.c

      参数:
      t - number of milliseconds since the epoch
      返回:
      the date as a byte array
      从以下版本开始:
      1.26
    • getBytes

      protected byte[] getBytes(String name) throws ZipException
      Retrieve the bytes for the given String in the encoding set for this Stream.
      参数:
      name - the string to get bytes from
      返回:
      the bytes as a byte array
      抛出:
      ZipException - on error
      从以下版本开始:
      1.3
    • writeOut

      protected final void writeOut(byte[] data) throws IOException
      Write bytes to output or random access file.
      参数:
      data - the byte array to write
      抛出:
      IOException - on error
      从以下版本开始:
      1.14
    • writeOut

      protected final void writeOut(byte[] data, int offset, int length) throws IOException
      Write bytes to output or random access file.
      参数:
      data - the byte array to write
      offset - the start position to write from
      length - the number of bytes to write
      抛出:
      IOException - on error
      从以下版本开始:
      1.14
    • adjustToLong

      protected static long adjustToLong(int i)
      Assumes a negative integer really is a positive integer that has wrapped around and re-creates the original value.
      参数:
      i - the value to treat as unsigned int.
      返回:
      the unsigned int as a long.
      从以下版本开始:
      1.34