ewe.zip
Class ZipOutputStream

java.lang.Object
  extended byewe.util.Errorable
      extended byewe.io.BasicStreamObject
          extended byewe.io.StreamObject
              extended byewe.zip.ZipOutputStream
All Implemented Interfaces:
BasicStream, Stream, Streamable, ZipConstants

public class ZipOutputStream
extends StreamObject
implements ZipConstants

This is a FilterOutputStream that writes the files into a zip archive one after another. It has a special method to start a new zip entry. The zip entries contains information about the file name size, compressed size, CRC, etc. It includes support for STORED and DEFLATED entries. This class is not thread safe.


Field Summary
static int DEFLATED
          Compression method.
 int level
           
static int STORED
          Compression method.
 
Fields inherited from class ewe.io.StreamObject
napIterations, napTime, READWRITE_CLOSED, READWRITE_ERROR, READWRITE_WOULDBLOCK
 
Fields inherited from class ewe.io.BasicStreamObject
closed
 
Fields inherited from class ewe.util.Errorable
error
 
Fields inherited from interface ewe.zip.ZipConstants
C_COMPRESSED_SIZE, C_COMPRESSION_METHOD, C_CRC32, C_DISK_NUMBER_START, C_EXTERNAL_FILE_ATTRIBUTES, C_EXTRA_FIELD_LENGTH, C_FILE_COMMENT_LENGTH, C_FILENAME_LENGTH, C_GENERAL_PURPOSE_BIT_FLAG, C_HDR_SIG, C_INTERNAL_FILE_ATTRIBUTES, C_LAST_MOD_FILE_DATE, C_LAST_MOD_FILE_TIME, C_RELATIVE_OFFSET_LOCAL_HEADER, C_SIGNATURE, C_SIZE, C_UNCOMPRESSED_SIZE, C_VERSION_MADE_BY, C_VERSION_NEEDED_TO_EXTRACT, D_COMPRESSED_SIZE, D_CRC32, D_HDR_SIG, D_SIGNATURE, D_SIZE, D_UNCOMPRESSED_SIZE, EC_HDR_SIG, EC_NUM_DISK_WITH_START_CENTRAL_DIR, EC_NUM_ENTRIES_CENTRL_DIR_THS_DISK, EC_NUMBER_THIS_DISK, EC_OFFSET_START_CENTRAL_DIRECTORY, EC_SIGNATURE, EC_SIZE, EC_SIZE_CENTRAL_DIRECTORY, EC_TOTAL_ENTRIES_CENTRAL_DIR, EC_ZIPFILE_COMMENT_LENGTH, L_COMPRESSED_SIZE, L_COMPRESSION_METHOD, L_CRC32, L_EXTRA_FIELD_LENGTH, L_FILENAME_LENGTH, L_GENERAL_PURPOSE_BIT_FLAG, L_HDR_SIG, L_LAST_MOD_FILE_DATE, L_LAST_MOD_FILE_TIME, L_SIGNATURE, L_SIZE, L_UNCOMPRESSED_SIZE, L_VERSION_NEEDED_TO_EXTRACT, Z_BEST_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION
 
Constructor Summary
ZipOutputStream(BasicStream out)
          Creates a new Zip output stream, writing a zip archive.
 
Method Summary
 boolean closeEntry()
          Closes the current entry.
 boolean finish()
          Finishes the stream.
 boolean flushStream()
          This is non-blocking.
 int nonBlockingWrite(byte[] b, int off, int len)
          Writes the given buffer to the current entry.
 boolean putNextEntry(ZipEntry entry)
          Starts a new Zip entry.
 boolean setComment(String comment)
          Set the zip file comment.
 void setLevel(int level)
          Sets default compression level.
 boolean setMethod(int method)
          Sets default compression method.
 
Methods inherited from class ewe.io.StreamObject
close, closeStream, doRead, doWrite, flush, isOpen, nap, nap, nonBlockingRead, read, readAByte, readBytes, readBytes, write, writeAByte, writeBytes, writeBytes
 
Methods inherited from class ewe.io.BasicStreamObject
getException, getName, read, read, throwIOException, toInputStream, toOutputStream, toStream, write, write
 
Methods inherited from class ewe.util.Errorable
returnError, returnError, returnError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

STORED

public static final int STORED
Compression method. This method doesn't compress at all.

See Also:
Constant Field Values

DEFLATED

public static final int DEFLATED
Compression method. This method uses the Deflater.

See Also:
Constant Field Values

level

public int level
Constructor Detail

ZipOutputStream

public ZipOutputStream(BasicStream out)
Creates a new Zip output stream, writing a zip archive.

Parameters:
out - the output stream to which the zip archive is written.
Method Detail

setComment

public boolean setComment(String comment)
Set the zip file comment.

Parameters:
comment - the comment.
Throws:
IllegalArgumentException - if UTF8 encoding of comment is longer than 0xffff bytes.

setMethod

public boolean setMethod(int method)
Sets default compression method. If the Zip entry specifies another method its method takes precedence.

Parameters:
method - the method.
Throws:
IllegalArgumentException - if method is not supported.
See Also:
STORED, DEFLATED

setLevel

public void setLevel(int level)
Sets default compression level. The new level will be activated immediately.

Throws:
IllegalArgumentException - if level is not supported.
See Also:
Deflater

putNextEntry

public boolean putNextEntry(ZipEntry entry)
Starts a new Zip entry. It automatically closes the previous entry if present. If the compression method is stored, the entry must have a valid size and crc, otherwise all elements (except name) are optional, but must be correct if present. If the time is not set in the entry, the current time is used.

Parameters:
entry - the entry.
Throws:
IOException - if an I/O error occured.
IllegalStateException - if stream was finished

closeEntry

public boolean closeEntry()
Closes the current entry.

Throws:
IOException - if an I/O error occured.
IllegalStateException - if no entry is active.

nonBlockingWrite

public int nonBlockingWrite(byte[] b,
                            int off,
                            int len)
Writes the given buffer to the current entry.

Specified by:
nonBlockingWrite in interface BasicStream
Overrides:
nonBlockingWrite in class StreamObject
Parameters:
b - Source byte array holding data to be written.
off - Starting index in buff for data to be written.
len - Number of bytes to write - should never be zero.
Returns:
greater than 0 = Number of bytes actually written.
0 = No bytes could be written yet - but the stream is still open.
-1 = Stream has been closed - no further writes are possible.
-2 = IO error - something went wrong.
Note that these values are different to the Stream.writeBytes method.
Throws:
IOException - if an I/O error occured.
IllegalStateException - if no entry is active.

finish

public boolean finish()
Finishes the stream. This will write the central directory at the end of the zip file and flush the stream.

Throws:
IOException - if an I/O error occured.

flushStream

public boolean flushStream()
                    throws IOException
Description copied from interface: BasicStream
This is non-blocking. It returns true if the flush completed, false if it did not, or throws an exception on error.

Specified by:
flushStream in interface BasicStream
Throws:
IOException