TrueZIP Driver ZIP 7.0

de.schlichtherle.truezip.zip
Class RawZipOutputStream<E extends ZipEntry>

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

public abstract class RawZipOutputStream<E extends ZipEntry>
extends DecoratingOutputStream
implements Iterable<E>

Provides unsafe (raw) access to a ZIP file using unsynchronized methods and shared ZipEntry instances.

Warning: This class is not intended for public use - its API may change at will without prior notification!

Author:
Christian Schlichtherle

Nested Class Summary
private static class RawZipOutputStream.AppendingLEDataOutputStream
           
private static class RawZipOutputStream.ZipDeflater
          A Deflater which can be asked for its current deflation level and counts input and output data length as a long integer value.
 
Field Summary
private  long cdOffset
          Start of central directory.
private  Charset charset
          The charset to use for entry names and comments.
private  boolean closed
           
private  String comment
          The file comment.
private  CRC32 crc
          CRC instance to avoid parsing DEFLATED data twice.
private  long dataStart
          Start of entry data.
private  byte[] dbuf
          This buffer holds deflated data for output.
private  RawZipOutputStream.ZipDeflater def
          This instance is used for deflated output.
static Charset DEFAULT_CHARSET
          The default character set used for entry names and comments in ZIP files.
private  boolean deflate
          Whether or not we need to deflate the current entry.
private  Map<String,E> entries
          The list of ZIP entries started to be written so far.
private  E entry
          Current entry.
private  boolean finished
           
private  short method
          Default compression method for next entry.
private  byte[] sbuf
           
 
Fields inherited from class de.schlichtherle.truezip.io.DecoratingOutputStream
delegate
 
Constructor Summary
protected RawZipOutputStream(OutputStream out, Charset charset)
          Constructs a ZIP output stream which decorates the given output stream using the given charset.
protected RawZipOutputStream(OutputStream out, RawZipFile<E> appendee)
          Constructs a ZIP output stream which decorates the given output stream and apppends to the given raw ZIP file.
 
Method Summary
private static void checkLocalFileHeaderData(ZipEntry entry)
           
 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.
private  void deflate()
           
 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 getCharset()
          Returns the charset to use for entry names and the file comment.
 String getComment()
          Returns the file comment.
 E getEntry(String name)
          Returns 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<E> iterator()
          Returns an iteration of 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(E entry)
          Equivalent to putNextEntry(entry, true).
 void putNextEntry(E 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.
private static LEDataOutputStream toLEDataOutputStream(OutputStream out)
           
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
private  void writeCentralFileHeader(ZipEntry entry)
          Writes a Central File Header record.
private  void writeDataDescriptor()
           
private  void writeEndOfCentralDirectory()
          Writes the End Of Central Directory record.
private  void writeLocalFileHeader()
           
 
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
 

Field Detail

DEFAULT_CHARSET

public static final Charset DEFAULT_CHARSET
The default character set used for entry names and comments in ZIP files. This is "UTF-8" for compatibility with Sun's JDK implementation.


charset

private final Charset charset
The charset to use for entry names and comments.


crc

private final CRC32 crc
CRC instance to avoid parsing DEFLATED data twice.


def

private final RawZipOutputStream.ZipDeflater def
This instance is used for deflated output.


dbuf

private final byte[] dbuf
This buffer holds deflated data for output.


sbuf

private final byte[] sbuf

comment

private String comment
The file comment.


method

private short method
Default compression method for next entry.


entries

private final Map<String,E extends ZipEntry> entries
The list of ZIP entries started to be written so far. Maps entry names to zip entries.


dataStart

private long dataStart
Start of entry data.


cdOffset

private long cdOffset
Start of central directory.


finished

private boolean finished

closed

private boolean closed

entry

private E extends ZipEntry entry
Current entry.


deflate

private boolean deflate
Whether or not we need to deflate the current entry. This can be used together with the DEFLATED method to write already compressed entry data into the ZIP file.

Constructor Detail

RawZipOutputStream

protected RawZipOutputStream(@NonNull
                             OutputStream out,
                             @NonNull
                             Charset charset)
Constructs a ZIP output stream which decorates the given output stream using the given charset.

Throws:
NullPointerException - If any parameter is null.
UnsupportedCharsetException - If charset is not supported by this JVM.

RawZipOutputStream

protected RawZipOutputStream(@NonNull
                             OutputStream out,
                             @NonNull
                             RawZipFile<E> 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

toLEDataOutputStream

private static LEDataOutputStream toLEDataOutputStream(OutputStream out)

getCharset

public String getCharset()
Returns the charset to use for entry names and the file comment.


size

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


entries

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

Returns an enumeration of all entries written to this ZIP file 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.


iterator

public Iterator<E> iterator()
Returns an iteration of all entries written to this ZIP file so far. Note that the iteration supports element removal and the returned entries are shared with this instance. It is illegal to put more entries into this ZIP output stream concurrently or modify the state of the iterated entries.

Specified by:
iterator in interface Iterable<E extends ZipEntry>

getEntry

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

Parameters:
name - the name of the ZIP entry.

getComment

public String getComment()
Returns the file comment.


setComment

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


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)

getLevel

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


setLevel

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


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 RawZipOutputStream is currently writing a ZIP entry.


putNextEntry

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

Throws:
IOException

putNextEntry

public void putNextEntry(E 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 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 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.

checkLocalFileHeaderData

private static void checkLocalFileHeaderData(ZipEntry entry)
                                      throws ZipException
Throws:
ZipException

writeLocalFileHeader

private void writeLocalFileHeader()
                           throws IOException
Throws:
IOException - On any I/O related issue.

write

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

write

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

deflate

private void deflate()
              throws IOException
Throws:
IOException

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.

writeDataDescriptor

private void writeDataDescriptor()
                          throws IOException
Throws:
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.

writeCentralFileHeader

private void writeCentralFileHeader(ZipEntry entry)
                             throws IOException
Writes a Central File Header record.

Throws:
IOException - On any I/O related issue.

writeEndOfCentralDirectory

private void writeEndOfCentralDirectory()
                                 throws IOException
Writes the End Of Central Directory record.

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

TrueZIP Driver ZIP 7.0

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