de.schlichtherle.util.zip
Class ZipOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by de.schlichtherle.util.zip.BasicZipOutputStream
              extended by de.schlichtherle.util.zip.ZipOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class ZipOutputStream
extends BasicZipOutputStream

Drop-in replacement for java.util.zip.ZipOutputStream.

This class starts writing ordinary ZIP32 File Format. It automatically adds ZIP64 extensions if required, i.e. if the file size exceeds 4GB or more than 65535 entries are written. This implies that the class may produce ZIP archive files which cannot be read by older ZIP implementations.

If the system property de.schlichtherle.util.zip.zip64ext is set to true (case is ignored), then ZIP64 extensions are always added when writing a ZIP archive file, regardless of its size. This system property is primarily intended for unit testing purposes. During normal operations, it should not be set as many third party tools would not treat the redundant ZIP64 extensions correctly. Note that it's impossible to inhibit ZIP64 extensions.

This class is thread-safe.

Version:
TrueZIP 6.7
Author:
Christian Schlichtherle
See Also:
ZipFile

Field Summary
 
Fields inherited from class de.schlichtherle.util.zip.BasicZipOutputStream
DEFAULT_CHARSET
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
ZipOutputStream(OutputStream out)
          Creates a new ZIP output stream decorating the given output stream, using the UTF-8 charset.
ZipOutputStream(OutputStream out, String charset)
          Creates a new ZIP output stream decorating the given output stream.
 
Method Summary
 void close()
          Closes this output stream and releases any system resources associated with the stream.
 void closeEntry()
          Writes all necessary data for this entry to the underlying stream.
 Enumeration entries()
          Returns a safe enumeration of clones of the ZIP entries written so far.
 void finish()
          Closes the current entry and writes the Central Directory to the underlying output stream.
 String getComment()
          Returns the file comment.
 ZipEntry getEntry(String name)
          Returns a clone of the ZipEntry for the given name or null if no entry with that name exists.
 int getLevel()
          Returns the compression level currently used.
 int getMethod()
          Returns the default compression method for subsequent entries.
 boolean isBusy()
          Returns true if and only if this BasicZipOutputStream is currently writing a ZIP entry.
 long length()
          Returns the total number of (compressed) bytes this stream has written to the underlying stream.
 void putNextEntry(ZipEntry entry, boolean deflate)
          Starts writing the next ZIP entry to the underlying stream.
 void setComment(String comment)
          Sets the file comment.
 void setLevel(int level)
          Sets the compression level for subsequent entries.
 void setMethod(int method)
          Sets the default compression method for subsequent entries.
 int size()
          Returns the number of ZIP entries written so far.
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class de.schlichtherle.util.zip.BasicZipOutputStream
getEncoding, putNextEntry
 
Methods inherited from class java.io.FilterOutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ZipOutputStream

public ZipOutputStream(OutputStream out)
                throws NullPointerException
Creates a new ZIP output stream decorating the given output stream, using the UTF-8 charset.

Throws:
NullPointerException - If out is null.

ZipOutputStream

public ZipOutputStream(OutputStream out,
                       String charset)
                throws NullPointerException,
                       UnsupportedEncodingException
Creates a new ZIP output stream decorating the given output stream.

Throws:
NullPointerException - If out or charset is null.
UnsupportedEncodingException - If charset is not supported by this JVM.
Method Detail

size

public int size()
Description copied from class: BasicZipOutputStream
Returns the number of ZIP entries written so far.

Overrides:
size in class BasicZipOutputStream

entries

public Enumeration entries()
Returns a safe enumeration of clones of the ZIP entries written so far. This method takes a snapshot of the collection of all entries and enumerates their clones, so concurrent modifications or state changes do not affect this instance, the returned enumeration or the enumerated ZIP entries.

Overrides:
entries in class BasicZipOutputStream

getEntry

public ZipEntry getEntry(String name)
Returns a clone of the ZipEntry for the given name or null if no entry with that name exists.

Overrides:
getEntry in class BasicZipOutputStream
Parameters:
name - Name of the ZIP entry.

setComment

public void setComment(String comment)
Description copied from class: BasicZipOutputStream
Sets the file comment.

Overrides:
setComment in class BasicZipOutputStream

getComment

public String getComment()
Description copied from class: BasicZipOutputStream
Returns the file comment.

Overrides:
getComment in class BasicZipOutputStream

setLevel

public void setLevel(int level)
Description copied from class: BasicZipOutputStream
Sets the compression level for subsequent entries.

Overrides:
setLevel in class BasicZipOutputStream

getLevel

public int getLevel()
Description copied from class: BasicZipOutputStream
Returns the compression level currently used.

Overrides:
getLevel in class BasicZipOutputStream

getMethod

public int getMethod()
Description copied from class: BasicZipOutputStream
Returns the default compression method for subsequent entries. This property is only used if a ZipEntry does not specify a compression method.

Overrides:
getMethod in class BasicZipOutputStream
See Also:
BasicZipOutputStream.setMethod(int), ZipEntry.getMethod()

setMethod

public void setMethod(int method)
Description copied from class: BasicZipOutputStream
Sets the default compression method for subsequent entries. This property is only used if a ZipEntry does not specify a compression method.

Legal values are ZipEntry.STORED (uncompressed) and ZipEntry.DEFLATED (compressed). The initial value is ZipEntry.DEFLATED.

Overrides:
setMethod in class BasicZipOutputStream
See Also:
BasicZipOutputStream.getMethod(), ZipEntry.setMethod(int)

length

public long length()
Description copied from class: BasicZipOutputStream
Returns the total number of (compressed) bytes this stream has written to the underlying stream.

Overrides:
length in class BasicZipOutputStream

isBusy

public final boolean isBusy()
Description copied from class: BasicZipOutputStream
Returns true if and only if this BasicZipOutputStream is currently writing a ZIP entry.

Overrides:
isBusy in class BasicZipOutputStream

putNextEntry

public void putNextEntry(ZipEntry entry,
                         boolean deflate)
                  throws IOException
Description copied from class: BasicZipOutputStream
Starts writing the next ZIP entry to the underlying stream. Note that if two or more entries with the same name are written consecutively to this stream, the last entry written will shadow all other entries, i.e. all of them are written to the ZIP compatible file (and hence require space), but only the last will be accessible from the central directory. This is unlike the genuine java.util.zip.ZipOutputStream which would throw a ZipException in this method when the second entry with the same name is to be written.

Overrides:
putNextEntry in class BasicZipOutputStream
Parameters:
entry - The ZIP entry to write.
deflate - Whether or not the entry data should be deflated. This should be set to false if and only if you are writing data which has been read from a ZIP archive file and has not been inflated again. The entries' properties CRC, compressed size and uncompressed size must be set appropriately.
Throws:
ZipException - If and only if writing the entry is impossible because the resulting file would not comply to the ZIP file format specification.
IOException - On any I/O related issue.

write

public void write(int b)
           throws IOException
Overrides:
write in class BasicZipOutputStream
Throws:
IOException - On any I/O related issue.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class BasicZipOutputStream
Throws:
IOException - On any I/O related issue.

closeEntry

public void closeEntry()
                throws IOException
Description copied from class: BasicZipOutputStream
Writes all necessary data for this entry to the underlying stream.

Overrides:
closeEntry in class BasicZipOutputStream
Throws:
ZipException - If and only if writing the entry is impossible because the resulting file would not comply to the ZIP file format specification.
IOException - On any I/O related issue.

finish

public void finish()
            throws IOException
Description copied from class: BasicZipOutputStream
Closes the current entry and writes the Central Directory to the underlying output stream.

Notes:

Overrides:
finish in class BasicZipOutputStream
Throws:
ZipException - If and only if writing the entry is impossible because the resulting file would not comply to the ZIP file format specification.
IOException - On any I/O related issue.

close

public void close()
           throws IOException
Description copied from class: BasicZipOutputStream
Closes this output stream and releases any system resources associated with the stream. This closes the open output stream writing to this ZIP file, if any.

Specified by:
close in interface Closeable
Overrides:
close in class BasicZipOutputStream
Throws:
IOException - On any I/O related issue.