Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 895
نمايش فراداده

ZipOutputStreamjava.util.zip

Java 1.1closeable flushable

This class is a subclass of DeflaterOutputStream that writes data in ZIP file format to an output stream. Before writing any data to the ZipOutputStream, you must begin an entry within the ZIP file with putNextEntry( ). The ZipEntry object passed to this method should specify at least a name for the entry. Once you have begun an entry with putNextEntry( ), you can write the contents of that entry with the write( ) methods. When you reach the end of an entry, you can begin a new one by calling putNextEntry( ) again, you can close the current entry with closeEntry( ), or you can close the stream itself with close( ).

Before beginning an entry with putNextEntry( ), you can set the compression method and level with setMethod( ) and setLevel( ). The constants DEFLATED and STORED are the two legal values for setMethod( ). If you use STORED, the entry is stored in the ZIP file without any compression. If you use DEFLATED, you can also specify the compression speed/strength tradeoff by passing a number from 1 to 9 to setLevel( ), where 9 gives the strongest and slowest level of compression. You can also use the constants Deflater.BEST_SPEED, Deflater.BEST_COMPRESSION, and Deflater.DEFAULT_COMPRESSION with the setLevel( ) method.

If you are storing an entry without compression, the ZIP file format requires that you specify, in advance, the entry size and CRC-32 checksum in the ZipEntry object for the entry. An exception is thrown if these values are not specified or specified incorrectly.

Figure 16-147. java.util.zip.ZipOutputStream

public class

ZipOutputStream extends DeflaterOutputStream implements ZipConstants { // Public Constructors public

ZipOutputStream (java.io.OutputStream

out ); // Public Constants public static final int

DEFLATED ; =8 public static final int

STORED ; =0 // Public Instance Methods public void

closeEntry ( ) throws java.io.IOException; public void

putNextEntry (ZipEntry

e ) throws java.io.IOException; public void

setComment (String

comment ); public void

setLevel (int

level ); public void

setMethod (int

method ); // Public Methods Overriding DeflaterOutputStream public void

close ( ) throws java.io.IOException; public void

finish ( ) throws java.io.IOException; public void

write (byte[ ]

b , int

off , int

len ) throws java.io.IOException; synchronized }

Subclasses

java.util.jar.JarOutput Stream