Top Description Inners Fields Constructors Methods
java.util.zip

public Class ZipOutputStream

extends DeflaterOutputStream
implements ZipConstants
Class Inheritance
All Implemented Interfaces
java.util.zip.ZipConstants
Known Direct Subclasses
java.util.jar.JarOutputStream
Imports
java.io.OutputStream, .IOException, java.nio.charset.Charset, java.util.Objects, .Vector, .HashSet, sun.nio.cs.UTF_8, sun.security.action.GetBooleanAction

This class implements an output stream filter for writing files in the ZIP file format. Includes support for both compressed and uncompressed entries.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Author
David Connelly
Since
1.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private boolean
private byte[]
private CRC32
private ZipOutputStream.XEntry
public static final int
DEFLATED

Compression method for compressed (DEFLATED) entries.

private boolean
private static final boolean
inhibitZip64

Whether to use ZIP64 for ZIP files with more than 64k entries.

private long
private int
private HashSet<String>
public static final int
STORED

Compression method for uncompressed (STORED) entries.

private long
private Vector<ZipOutputStream.XEntry>
private final ZipCoder
Inherited from java.util.zip.DeflaterOutputStream:
bufdefusesDefaultDeflater

Constructor Summary

AccessConstructor and Description
public
ZipOutputStream(OutputStream
the actual output stream
out
)

Creates a new ZIP output stream.

public
ZipOutputStream(OutputStream
the actual output stream
out
,
Charset
the charset to be used to encode the entry names and comments
charset
)

Creates a new ZIP output stream.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.util.zip.DeflaterOutputStream.close.

Implements java.io.Closeable.close.

Closes the ZIP output stream as well as the stream being filtered.

public void
closeEntry()

Closes the current ZIP entry and positions the stream for writing the next entry.

private void
ensureOpen()

Checks to make sure that this stream has not been closed.

public void
finish()

Overrides java.util.zip.DeflaterOutputStream.finish.

Finishes writing the contents of the ZIP output stream without closing the underlying stream.

private int
getExtraLen(byte[] extra)

public void
putNextEntry(ZipEntry
the ZIP entry to be written
e
)

Begins writing a new ZIP file entry and positions the stream to the start of the entry data.

public void
setComment(String
the comment string
comment
)

Sets the ZIP file comment.

public void
setLevel(int
the compression level (0-9)
level
)

Sets the compression level for subsequent entries which are DEFLATED.

public void
setMethod(int
the default compression method
method
)

Sets the default compression method for subsequent entries.

private static int
private int
versionMadeBy(ZipEntry e, int version)

Adds information about compatibility of file attribute information to a version value.

public synchronized void
write(byte[]
the data to be written
b
,
int
the start offset in the data
off
,
int
the number of bytes that are written
len
)

Overrides java.util.zip.DeflaterOutputStream.write.

Writes an array of bytes to the current ZIP entry data.

private void
writeByte(int v)

private void
writeBytes(byte[] b, int off, int len)

private void
private void
writeEND(long off, long len)

private void
private void
writeExtra(byte[] extra)

private void
writeInt(long v)

private void
private void
writeLong(long v)

private void
writeShort(int v)

Inherited from java.util.zip.DeflaterOutputStream:
deflateflushwrite

Field Detail

closedback to summary
private boolean closed

Hides java.util.zip.DeflaterOutputStream.closed.

commentback to summary
private byte[] comment
crcback to summary
private CRC32 crc
currentback to summary
private ZipOutputStream.XEntry current
DEFLATEDback to summary
public static final int DEFLATED

Compression method for compressed (DEFLATED) entries.

finishedback to summary
private boolean finished
inhibitZip64back to summary
private static final boolean inhibitZip64

Whether to use ZIP64 for ZIP files with more than 64k entries. Until ZIP64 support in ZIP implementations is ubiquitous, this system property allows the creation of ZIP files which can be read by legacy ZIP implementations which tolerate "incorrect" total entry count fields, such as the ones in jdk6, and even some in jdk7.

locoffback to summary
private long locoff
methodback to summary
private int method
namesback to summary
private HashSet<String> names
STOREDback to summary
public static final int STORED

Compression method for uncompressed (STORED) entries.

writtenback to summary
private long written
xentriesback to summary
private Vector<ZipOutputStream.XEntry> xentries
zcback to summary
private final ZipCoder zc

Constructor Detail

ZipOutputStreamback to summary
public ZipOutputStream(OutputStream out)

Creates a new ZIP output stream.

The UTF-8 charset is used to encode the entry names and comments.

Parameters
out:OutputStream

the actual output stream

ZipOutputStreamback to summary
public ZipOutputStream(OutputStream out, Charset charset)

Creates a new ZIP output stream.

Parameters
out:OutputStream

the actual output stream

charset:Charset

the charset to be used to encode the entry names and comments

Since
1.7

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.util.zip.DeflaterOutputStream.close.

Implements java.io.Closeable.close.

Closes the ZIP output stream as well as the stream being filtered.

Exceptions
IOException:
if an I/O error has occurred
ZipException:
if a ZIP file error has occurred
closeEntryback to summary
public void closeEntry() throws IOException

Closes the current ZIP entry and positions the stream for writing the next entry.

Exceptions
IOException:
if an I/O error has occurred
ZipException:
if a ZIP format error has occurred
ensureOpenback to summary
private void ensureOpen() throws IOException

Checks to make sure that this stream has not been closed.

finishback to summary
public void finish() throws IOException

Overrides java.util.zip.DeflaterOutputStream.finish.

Finishes writing the contents of the ZIP output stream without closing the underlying stream. Use this method when applying multiple filters in succession to the same output stream.

Exceptions
IOException:
if an I/O exception has occurred
ZipException:
if a ZIP file error has occurred
getExtraLenback to summary
private int getExtraLen(byte[] extra)
putNextEntryback to summary
public void putNextEntry(ZipEntry e) throws IOException

Begins writing a new ZIP file entry and positions the stream to the start of the entry data. Closes the current entry if still active.

The default compression method will be used if no compression method was specified for the entry. When writing a compressed (DEFLATED) entry, and the compressed size has not been explicitly set with the ZipEntry#setCompressedSize(long) method, then the compressed size will be set to the actual compressed size after deflation.

The current time will be used if the entry has no set modification time.

API Note

When writing a directory entry, the STORED compression method should be used and the size and CRC-32 values should be set to 0:

ZipEntry e = new ZipEntry(entryName); if (e.isDirectory()) { e.setMethod(ZipEntry.STORED); e.setSize(0); e.setCrc(0); } stream.putNextEntry(e);
ZipEntry e = new ZipEntry(entryName);
if (e.isDirectory()) {
    e.setMethod(ZipEntry.STORED);
    e.setSize(0);
    e.setCrc(0);
}
stream.putNextEntry(e);
This allows optimal performance when processing directory entries.
Parameters
e:ZipEntry

the ZIP entry to be written

Exceptions
IOException:
if an I/O error has occurred
ZipException:
if a ZIP format error has occurred
setCommentback to summary
public void setComment(String comment)

Sets the ZIP file comment.

Parameters
comment:String

the comment string

Exceptions
IllegalArgumentException:
if the length of the specified ZIP file comment is greater than 0xFFFF bytes
setLevelback to summary
public void setLevel(int level)

Sets the compression level for subsequent entries which are DEFLATED. The default setting is DEFAULT_COMPRESSION.

Parameters
level:int

the compression level (0-9)

Exceptions
IllegalArgumentException:
if the compression level is invalid
setMethodback to summary
public void setMethod(int method)

Sets the default compression method for subsequent entries. This default will be used whenever the compression method is not specified for an individual ZIP file entry, and is initially set to DEFLATED.

Parameters
method:int

the default compression method

Exceptions
IllegalArgumentException:
if the specified compression method is invalid
versionback to summary
private static int version(ZipEntry e) throws ZipException
versionMadeByback to summary
private int versionMadeBy(ZipEntry e, int version)

Adds information about compatibility of file attribute information to a version value.

writeback to summary
public synchronized void write(byte[] b, int off, int len) throws IOException

Overrides java.util.zip.DeflaterOutputStream.write.

Writes an array of bytes to the current ZIP entry data. This method will block until all the bytes are written.

Parameters
b:byte[]

the data to be written

off:int

the start offset in the data

len:int

the number of bytes that are written

Exceptions
IOException:
if an I/O error has occurred
ZipException:
if a ZIP file error has occurred
writeByteback to summary
private void writeByte(int v) throws IOException
writeBytesback to summary
private void writeBytes(byte[] b, int off, int len) throws IOException
writeCENback to summary
private void writeCEN(ZipOutputStream.XEntry xentry) throws IOException
writeENDback to summary
private void writeEND(long off, long len) throws IOException
writeEXTback to summary
private void writeEXT(ZipEntry e) throws IOException
writeExtraback to summary
private void writeExtra(byte[] extra) throws IOException
writeIntback to summary
private void writeInt(long v) throws IOException
writeLOCback to summary
private void writeLOC(ZipOutputStream.XEntry xentry) throws IOException
writeLongback to summary
private void writeLong(long v) throws IOException
writeShortback to summary
private void writeShort(int v) throws IOException
java.util.zip back to summary

private Class ZipOutputStream.XEntry

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final ZipEntry
pack-priv final long

Constructor Summary

AccessConstructor and Description
public
XEntry(ZipEntry entry, long offset)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

entryback to summary
pack-priv final ZipEntry entry
offsetback to summary
pack-priv final long offset

Constructor Detail

XEntryback to summary
public XEntry(ZipEntry entry, long offset)