TrueZIP Driver ZIP 7.1

de.schlichtherle.truezip.zip
Class ZipOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by de.schlichtherle.truezip.io.DecoratingOutputStream
          extended by de.schlichtherle.truezip.zip.RawZipOutputStream<ZipEntry>
              extended by de.schlichtherle.truezip.zip.ZipOutputStream
All Implemented Interfaces:
Closeable, Flushable, Iterable<ZipEntry>

public class ZipOutputStream
extends RawZipOutputStream<ZipEntry>

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.truezip.io.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.

Author:
Christian Schlichtherle
See Also:
ZipFile

Field Summary
 
Fields inherited from class de.schlichtherle.truezip.zip.RawZipOutputStream
DEFAULT_CHARSET
 
Fields inherited from class de.schlichtherle.truezip.io.DecoratingOutputStream
delegate
 
Constructor Summary
ZipOutputStream(OutputStream out)
          Constructs a ZIP output stream which decorates the given output stream using the "UTF-8" charset.
ZipOutputStream(OutputStream out, Charset charset)
          Constructs a ZIP output stream which decorates the given output stream using the given charset.
ZipOutputStream(OutputStream out, ZipFile appendee)
          Constructs a ZIP output stream which decorates the given output stream and apppends to the given raw ZIP file.
 
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<? extends ZipEntry> entries()
          Deprecated. Use iterator() instead.
 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 entry for the given name or null if no entry with this 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 RawZipOutputStream is currently writing a ZIP entry.
 Iterator<ZipEntry> iterator()
          Returns a safe iteration of clones for all entries written to this ZIP file so far.
 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.truezip.zip.RawZipOutputStream
getCharset, putNextEntry
 
Methods inherited from class de.schlichtherle.truezip.io.DecoratingOutputStream
flush, toString, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ZipOutputStream

public ZipOutputStream(OutputStream out)
Constructs a ZIP output stream which decorates the given output stream using the "UTF-8" charset.

Throws:
NullPointerException - If out is null.

ZipOutputStream

public ZipOutputStream(OutputStream out,
                       Charset charset)
Constructs a ZIP output stream which decorates the given output stream using the given charset.

Throws:
NullPointerException - If any parameter is null.

ZipOutputStream

public ZipOutputStream(OutputStream out,
                       ZipFile appendee)
                throws ZipException
Constructs a ZIP output stream which decorates the given output stream and apppends to the given raw ZIP file.

In order to append entries to an existing ZIP file, out must be set up so that it appends to the same ZIP file from which appendee is reading. appendee may already be closed.

Throws:
NullPointerException - If any parameter is null.
ZipException - if appendee has a postamble, i.e. some data after its central directory and before its end.
Method Detail

size

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

Overrides:
size in class RawZipOutputStream<ZipEntry>

entries

public Enumeration<? extends ZipEntry> entries()
Deprecated. Use iterator() instead.

Returns a safe enumeration of clones for all 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 RawZipOutputStream<ZipEntry>

iterator

public Iterator<ZipEntry> iterator()
Returns a safe iteration of clones for all entries written to this ZIP file so far. This method takes a snapshot of the collection of all entries and clones them while iterating, so concurrent modifications or state changes do not affect this instance, the returned enumeration or the enumerated ZIP entries. The iteration does not support element removal.

Specified by:
iterator in interface Iterable<ZipEntry>
Overrides:
iterator in class RawZipOutputStream<ZipEntry>

getEntry

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

Overrides:
getEntry in class RawZipOutputStream<ZipEntry>
Parameters:
name - the name of the ZIP entry.

setComment

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

Overrides:
setComment in class RawZipOutputStream<ZipEntry>

getComment

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

Overrides:
getComment in class RawZipOutputStream<ZipEntry>

setLevel

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

Overrides:
setLevel in class RawZipOutputStream<ZipEntry>

getLevel

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

Overrides:
getLevel in class RawZipOutputStream<ZipEntry>

getMethod

public int getMethod()
Description copied from class: RawZipOutputStream
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 RawZipOutputStream<ZipEntry>
See Also:
RawZipOutputStream.setMethod(int), ZipEntry.getMethod()

setMethod

public void setMethod(int method)
Description copied from class: RawZipOutputStream
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 RawZipOutputStream<ZipEntry>
See Also:
RawZipOutputStream.getMethod(), ZipEntry.setMethod(int)

length

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

Overrides:
length in class RawZipOutputStream<ZipEntry>

isBusy

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

Overrides:
isBusy in class RawZipOutputStream<ZipEntry>

putNextEntry

public void putNextEntry(ZipEntry entry,
                         boolean deflate)
                  throws IOException
Description copied from class: RawZipOutputStream
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 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 RawZipOutputStream<ZipEntry>
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 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 RawZipOutputStream<ZipEntry>
Throws:
IOException - On any I/O related issue.

write

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

closeEntry

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

Overrides:
closeEntry in class RawZipOutputStream<ZipEntry>
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: RawZipOutputStream
Closes the current entry and writes the Central Directory to the underlying output stream.

Notes:

Overrides:
finish in class RawZipOutputStream<ZipEntry>
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: RawZipOutputStream
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 RawZipOutputStream<ZipEntry>
Throws:
IOException - On any I/O related issue.

TrueZIP Driver ZIP 7.1

Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.