org.entityfs.util.zip
Class AbstractZipCreator<T extends AbstractZipCreator<?>>

java.lang.Object
  extended by org.entityfs.util.AbstractIteratorUtil
      extended by org.entityfs.util.zip.AbstractZipCreator<T>
Direct Known Subclasses:
JarCreator, ZipCreator

public abstract class AbstractZipCreator<T extends AbstractZipCreator<?>>
extends AbstractIteratorUtil

Abstract base class for creating Zip files.

Since:
1.0
Author:
Karl Gustafsson

Constructor Summary
protected AbstractZipCreator(Iterator<? extends ZipEntryInfo> itr, DirectoryView targetDirectory, String targetFileName, ErrorHandlingStrategy ehs)
          Create an AbstractZipCreator that uses the supplied ErrorHandlingStrategy.
 
Method Summary
 EFile create()
          Create the Zip file.
 EFile create(EntityLock wl)
          Create the Zip file.
protected  ZipOutputStream createOutputStream(WritableFile f)
          Create the output stream to write contents to.
protected  EFile createTargetFile(DirectoryView dv, String name)
          Create the target Zip file (empty).
protected  DirectoryView getTargetDirectory()
           
protected  String getTargetFileName()
           
protected  T getThis()
          Get this cast to its actual type.
 T setComment(String comment)
          Set the Zip file comment.
 T setLevel(int level)
          Set the entry compression level (see ZipOutputStream.setLevel(int)).
 T setMethod(int method)
          Set the entry compression method (see ZipOutputStream.setMethod(int)).
 
Methods inherited from class org.entityfs.util.AbstractIteratorUtil
letErrorStrategyHandle, letErrorStrategyHandle, letErrorStrategyHandle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractZipCreator

protected AbstractZipCreator(Iterator<? extends ZipEntryInfo> itr,
                             DirectoryView targetDirectory,
                             String targetFileName,
                             ErrorHandlingStrategy ehs)
Create an AbstractZipCreator that uses the supplied ErrorHandlingStrategy.

Parameters:
itr - The iterator to get ZipEntryInfo objects from.
targetDirectory - The directory where the Zip file will be created.
targetFileName - The name of the Zip file.
ehs - The error handling strategy. Set to null to use the default ErrorHandlingStrategy.
Method Detail

getThis

protected final T getThis()
Get this cast to its actual type. This is useful for returning the right type of this when implementing setter methods that return the called object.

Returns:
this cast to its actual type.

getTargetDirectory

protected DirectoryView getTargetDirectory()

getTargetFileName

protected String getTargetFileName()

setComment

public T setComment(String comment)
Set the Zip file comment.

Parameters:
comment - The Zip file comment.
Returns:
this.

setMethod

public T setMethod(int method)
Set the entry compression method (see ZipOutputStream.setMethod(int)). Default is ZipOutputStream.DEFLATED.

Parameters:
method - The compression method.
Returns:
this.

setLevel

public T setLevel(int level)
Set the entry compression level (see ZipOutputStream.setLevel(int)). Default is to use the Zlib library's default compression level. It is currently set to 6, but that may change in the future.

Parameters:
level - The compression level.
Returns:
this.

createTargetFile

protected EFile createTargetFile(DirectoryView dv,
                                 String name)
Create the target Zip file (empty). Subclasses may override this to create the file in the way that they want.

Parameters:
dv - The parent directory. This is locked for writing when this method is called. The method should not release that lock.
name - The name of the file
Returns:
The Zip file.

createOutputStream

protected ZipOutputStream createOutputStream(WritableFile f)
                                      throws IOException
Create the output stream to write contents to. Subclasses may override this to provide their own output stream implementations.

The target file is locked for writing when this method is called.

Returns:
An open output stream.
Throws:
IOException

create

public EFile create(EntityLock wl)
             throws UncheckedInterruptedException,
                    EntityFoundException
Create the Zip file.

Parameters:
wl - A locked write lock for the target directory. The lock will be released as soon as the Zip file is created and locked. This method is responsible for unlocking this lock, regardless of its result, even if it throws an exception.
Returns:
The new Zip file, or null if the supplied iterator did not return any entities. (In which case the file is not created.)
Throws:
UncheckedInterruptedException - If the thread is interrupted.
EntityFoundException - If an entity with the same name as the Zip file already exists.

create

public EFile create()
             throws UncheckedInterruptedException,
                    EntityFoundException
Create the Zip file.

Returns:
The new Zip file, or null if the supplied iterator did not return any entities. (In which case the file is not created.)
Throws:
UncheckedInterruptedException - If the thread is interrupted.
EntityFoundException - If an entity with the same name as the Zip file already exists.