com.evanmclean.evlib.util.zip
Class Zipper

java.lang.Object
  extended by com.evanmclean.evlib.util.zip.Zipper

public class Zipper
extends Object

Convenience wrapper around a ZipOutputStream to easily add files and folders to a zip file.

Author:
Evan McLean McLean Computer Services (see the overview for copyright and licensing.)

Nested Class Summary
static class Zipper.FolderAction
          Action to take for folders when recursively adding the contents of a folder.
 
Field Summary
static int DEFLATED
          Compression method for compressed (DEFLATED) entries.
static int STORED
          Compression method for uncompressed (STORED) entries.
 
Constructor Summary
Zipper(File file)
          Create a zip to the specified file with the default Zipper.FolderAction of Zipper.FolderAction.ADD.
Zipper(File file, Zipper.FolderAction folder_action)
          Zip to the specified file.
Zipper(OutputStream out)
          Create a zip to the specified output stream with the default Zipper.FolderAction of Zipper.FolderAction.ADD.
Zipper(OutputStream out, Zipper.FolderAction folder_action)
          Create a zip to the specified output stream.
Zipper(ZipOutputStream out)
          Create a zip to the specified zip output stream with the default Zipper.FolderAction of Zipper.FolderAction.ADD.
Zipper(ZipOutputStream out, Zipper.FolderAction folder_action)
          Create a zip to the specified zip output stream.
 
Method Summary
 void close()
          Closes the zip output stream as well as the underlying stream.
 void copy(File file)
          Copy a file into the zip, just using the base name of the file.
 void copy(File folder, boolean zip_folder_name)
          Recursively copy all files in a folder into the zip.
 void copy(File file, String name)
          Copy the file into the zip, using the specified file name.
 void copy(InputStream in, String name)
          Read the input stream into the zip, using the specified file name.
 void finish()
          Finishes writing the contents of the zip output stream without closing the underlying stream.
 Zipper.FolderAction getFolderAction()
          Get the current Zipper.FolderAction that is used for calls to copy(File, boolean).
 void setComment(String comment)
          Set the comment to be included in the zip file.
 void setFolderAction(Zipper.FolderAction folder_action)
          Set the Zipper.FolderAction to be used for calls to copy(File, boolean).
 void setLevel(int level)
          Set the compression level to a value between 0 (none), 1 (fast) and 9 (best).
 void setMethod(int method)
          Sets the default compression method for subsequent entries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFLATED

public static final int DEFLATED
Compression method for compressed (DEFLATED) entries.

See Also:
Constant Field Values

STORED

public static final int STORED
Compression method for uncompressed (STORED) entries.

See Also:
Constant Field Values
Constructor Detail

Zipper

public Zipper(File file)
       throws FileNotFoundException
Create a zip to the specified file with the default Zipper.FolderAction of Zipper.FolderAction.ADD.

Parameters:
file - File to create as a zip file.
Throws:
FileNotFoundException

Zipper

public Zipper(File file,
              Zipper.FolderAction folder_action)
       throws FileNotFoundException
Zip to the specified file.

Parameters:
file - File to create as a zip file.
folder_action - The Zipper.FolderAction to use.
Throws:
FileNotFoundException

Zipper

public Zipper(OutputStream out)
Create a zip to the specified output stream with the default Zipper.FolderAction of Zipper.FolderAction.ADD.

Parameters:
out - Output stream to write as a zip file.

Zipper

public Zipper(OutputStream out,
              Zipper.FolderAction folder_action)
Create a zip to the specified output stream.

Parameters:
out - Output stream to write as a zip file.
folder_action - The Zipper.FolderAction to use.

Zipper

public Zipper(ZipOutputStream out)
Create a zip to the specified zip output stream with the default Zipper.FolderAction of Zipper.FolderAction.ADD.

Parameters:
out - Zip output stream to write as a zip file.

Zipper

public Zipper(ZipOutputStream out,
              Zipper.FolderAction folder_action)
Create a zip to the specified zip output stream.

Parameters:
out - Zip output stream to write as a zip file.
folder_action - The Zipper.FolderAction to use.
Method Detail

close

public void close()
           throws IOException
Closes the zip output stream as well as the underlying stream.

Throws:
IOException

copy

public void copy(File file)
          throws IOException
Copy a file into the zip, just using the base name of the file.

Parameters:
file -
Throws:
IOException

copy

public void copy(File folder,
                 boolean zip_folder_name)
          throws IOException
Recursively copy all files in a folder into the zip.

Parameters:
folder - Folder to copy.
zip_folder_name - If true, then the name of the folder is used as the base folder for all the files within the zip, otherwise files and folders are stored relative to the root of the zip file.
Throws:
IOException

copy

public void copy(File file,
                 String name)
          throws IOException
Copy the file into the zip, using the specified file name.

Parameters:
file - The file to zip.
name - The name for the file within the zip, which may include a path component.
Throws:
IOException

copy

public void copy(InputStream in,
                 String name)
          throws IOException
Read the input stream into the zip, using the specified file name.

Parameters:
in - The input to zip.
name - The name for the file within the zip, which may include a path component.
Throws:
IOException

finish

public void finish()
            throws IOException
Finishes writing the contents of the zip output stream without closing the underlying stream. Use this method when applying multiple filters in succession to the same output stream.

Throws:
IOException

getFolderAction

public Zipper.FolderAction getFolderAction()
Get the current Zipper.FolderAction that is used for calls to copy(File, boolean).

Returns:
See Zipper.FolderAction

setComment

public void setComment(String comment)
Set the comment to be included in the zip file.

Parameters:
comment - The comment.

setFolderAction

public void setFolderAction(Zipper.FolderAction folder_action)
Set the Zipper.FolderAction to be used for calls to copy(File, boolean).

Parameters:
folder_action - See Zipper.FolderAction

setLevel

public void setLevel(int level)
Set the compression level to a value between 0 (none), 1 (fast) and 9 (best).

Parameters:
level - Compression level to use.

setMethod

public void setMethod(int method)
Sets the default compression method for subsequent entries. This default will be used whenever the compression method is not specified for an individual ZIP file entry, and is initially set to DEFLATED.

Parameters:
method - One of STORED or DEFLATED.