de.schlichtherle.util.zip
Class BasicZipOutputStream

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

public class BasicZipOutputStream
extends FilterOutputStream

This class is not intended for public use! The methods in this class are unsynchronized and entries/getEntry(java.lang.String) enumerate/return ZipEntry instances which are shared with this class rather than clones of them. This may be used by subclasses in order to benefit from the slightly better performance.

Since:
TrueZIP 6.4
Version:
TrueZIP 6.7
Author:
Christian Schlichtherle
See Also:
ZipOutputStream

Field Summary
static String DEFAULT_CHARSET
          The default character set used for entry names and comments in ZIP compatible files.
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
BasicZipOutputStream(OutputStream out)
          Creates a new ZIP output stream decorating the given output stream, using the "UTF-8" charset.
BasicZipOutputStream(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 an enumeration 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.
 String getEncoding()
          Returns the charset to use for filenames and the file comment.
 ZipEntry getEntry(String name)
          Returns 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)
          Equivalent to putNextEntry(entry, true).
 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 java.io.FilterOutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARSET

public static final String DEFAULT_CHARSET
The default character set used for entry names and comments in ZIP compatible files. This is "UTF-8" for compatibility with Sun's JDK implementation. Note that you should use "IBM437" for ordinary ZIP files instead.

See Also:
Constant Field Values
Constructor Detail

BasicZipOutputStream

public BasicZipOutputStream(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.

BasicZipOutputStream

public BasicZipOutputStream(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

getEncoding

public String getEncoding()
Returns the charset to use for filenames and the file comment.


size

public int size()
Returns the number of ZIP entries written so far.


entries

public Enumeration entries()
Returns an enumeration of the ZIP entries written so far. Note that the enumerated entries are shared with this class. It is illegal to put more entries into this ZIP output stream concurrently or modify the state of the enumerated entries.


getEntry

public ZipEntry getEntry(String name)
Returns the ZipEntry for the given name or null if no entry with that name exists. Note that the returned entry is shared with this class. It is illegal to change its state!

Parameters:
name - Name of the ZIP entry.

getComment

public String getComment()
Returns the file comment.


setComment

public void setComment(String comment)
Sets the file comment.


getLevel

public int getLevel()
Returns the compression level currently used.


setLevel

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


getMethod

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

See Also:
setMethod(int), ZipEntry.getMethod()

setMethod

public void setMethod(int method)
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.

See Also:
getMethod(), ZipEntry.setMethod(int)

length

public long length()
Returns the total number of (compressed) bytes this stream has written to the underlying stream.


isBusy

public boolean isBusy()
Returns true if and only if this BasicZipOutputStream is currently writing a ZIP entry.


putNextEntry

public final void putNextEntry(ZipEntry entry)
                        throws IOException
Equivalent to putNextEntry(entry, true).

Throws:
IOException

putNextEntry

public void putNextEntry(ZipEntry entry,
                         boolean deflate)
                  throws IOException
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.

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 FilterOutputStream
Throws:
IOException - On any I/O related issue.

write

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

closeEntry

public void closeEntry()
                throws IOException
Writes all necessary data for this entry to the underlying stream.

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
Closes the current entry and writes the Central Directory to the underlying output stream.

Notes:

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
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 FilterOutputStream
Throws:
IOException - On any I/O related issue.