org.dmonix.util.zip
Class ZipWriter

java.lang.Object
  extended byorg.dmonix.util.zip.ZipWriter

public class ZipWriter
extends java.lang.Object

Class for writing/deflating entries to a ZIP file.

Copyright: Copyright (c) 2001

Company: dmonix.org

Version:
1.0
Author:
Peter Nerg

Method Summary
 void addFile(java.io.File file)
          Add a new file entry to the existing zip file.
 void addFile(java.io.File file, java.io.File directory)
          Add a new file entry to the existing zip file.
 void addFile(java.io.File file, java.io.File directory, java.lang.String comment)
          Add a new file entry to the existing zip file.
 void addFile(java.io.File file, java.lang.String comment)
          Add a new file entry to the existing zip file.
 void addFile(java.lang.String fileName)
          Add a new file entry to the existing zip file
The method will add the file to zip file with no information on the original files source path.
 void addFile(java.lang.String fileName, java.lang.String comment)
          Add a new file entry to the existing zip file
The method will implicitly perform an addZipEntry and a closeZipEntry.
 void addFiles(java.io.File[] files)
          Add an array of files to the existing zip file.
 void addZipEntry(java.lang.String name)
          Creates a new entry in the ZIP file.
 void addZipEntry(java.lang.String name, java.lang.String comment)
          Creates a new entry in the ZIP file.
 void close()
          Close the outputstream to the zip file.
 void closeZipEntry()
          Closes the current zip entry.
static ZipWriter createZipFile(java.io.File file)
          Creates a new ZIP file.
static ZipWriter createZipFile(java.io.File file, int compressionLevel)
          Creates a new ZIP file.
static ZipWriter createZipFile(java.lang.String fileName)
          Creates a new ZIP file.
static ZipWriter createZipFile(java.lang.String fileName, int compressionLevel)
          Creates a new ZIP file.
 void setCompressionLevel(int compressionLevel)
          Sets the level of compression.
 void setZipfileComment(java.lang.String comment)
          Set a comment for the current ZIP file.
 void writeDataToZipEntry(byte[] data)
          Writes data to the current entry.
 void writeDataToZipEntry(byte[] data, int offset, int length)
          Writes data to the current entry.
 void writeDataToZipEntry(java.io.InputStream istream, boolean closeInputStream)
          Copies data from the provided stream to the current entry
A call to addZipEntry must preceed this operation.
 void writeDataToZipEntry(int data)
          Writes data to the current entry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addFile

public void addFile(java.io.File file,
                    java.io.File directory)
             throws java.io.IOException
Add a new file entry to the existing zip file.
The method will add the file to zip file with information on the original files source path relative to the supplied directory path.
E.g. the directory /etc/foo/ and the file /etc/foo/data/file.txt results in an entry called data/file.txt/
The method will implicitly perform an addZipEntry and a closeZipEntry.

Parameters:
file - A file object
directory - A parent directory
Throws:
java.io.IOException
See Also:
org.dmonix.io.IOUtil.getRelativePath(File, File)

addFile

public void addFile(java.io.File file)
             throws java.io.IOException
Add a new file entry to the existing zip file.
The method will add the file to zip file with no information on the original files source path.
I.e. the file /etc/foo/data/file.txt will be written to the ZIP file simply as file.txt.
Note that there is a risk of filename collision this way.
The method will implicitly perform an addZipEntry and a closeZipEntry.

Parameters:
file - The file
Throws:
java.io.IOException

addFile

public void addFile(java.lang.String fileName)
             throws java.io.IOException
Add a new file entry to the existing zip file
The method will add the file to zip file with no information on the original files source path.
I.e. the file /etc/foo/data/file.txt will be written to the ZIP file simply as file.txt.
Note that there is a risk of filename collision this way.
The method will implicitly perform an addZipEntry and a closeZipEntry.

Parameters:
fileName - name/path of the file to add
Throws:
java.io.IOException

addFile

public void addFile(java.io.File file,
                    java.io.File directory,
                    java.lang.String comment)
             throws java.io.IOException
Add a new file entry to the existing zip file.
The method will add the file to zip file with information on the original files source path relative to the supplied directory path.
E.g. the directory /etc/foo/ and the file /etc/foo/data/file.txt results in an entry called data/file.txt/
The method will implicitly perform an addZipEntry and a closeZipEntry.

Parameters:
file - A file object
directory - A parent directory
comment - The entry comment
Throws:
java.io.IOException
See Also:
org.dmonix.io.IOUtil.getRelativePath(File, File)

addFile

public void addFile(java.io.File file,
                    java.lang.String comment)
             throws java.io.IOException
Add a new file entry to the existing zip file.
The method will add the file to zip file with no information on the original files source path.
I.e. the file /etc/foo/data/file.txt will be written to the ZIP file simply as file.txt.
Note that there is a risk of filename collision this way.
The method will implicitly perform an addZipEntry and a closeZipEntry.

Parameters:
file - The file
comment - The entry comment
Throws:
java.io.IOException

addFile

public void addFile(java.lang.String fileName,
                    java.lang.String comment)
             throws java.io.IOException
Add a new file entry to the existing zip file
The method will implicitly perform an addZipEntry and a closeZipEntry.

Parameters:
fileName - name/path of the file to add
comment - The entry comment
Throws:
java.io.IOException

addFiles

public void addFiles(java.io.File[] files)
              throws java.io.IOException
Add an array of files to the existing zip file.
This is equal to multiple calls to addFile(File)

Parameters:
files - The files to add
Throws:
java.io.IOException
See Also:
addFile(File)

addZipEntry

public void addZipEntry(java.lang.String name)
                 throws java.io.IOException
Creates a new entry in the ZIP file.

Parameters:
name - The name/path of the entry
Throws:
java.io.IOException

addZipEntry

public void addZipEntry(java.lang.String name,
                        java.lang.String comment)
                 throws java.io.IOException
Creates a new entry in the ZIP file.

Parameters:
name - The name/path of the entry
comment - The entry comment
Throws:
java.io.IOException

writeDataToZipEntry

public void writeDataToZipEntry(int data)
                         throws java.io.IOException
Writes data to the current entry.
A call to addZipEntry must preceed this operation.

Parameters:
data - The data to write to the entry
Throws:
java.io.IOException
See Also:
addZipEntry(String), addZipEntry(String, String)

writeDataToZipEntry

public void writeDataToZipEntry(byte[] data)
                         throws java.io.IOException
Writes data to the current entry.
A call to addZipEntry must preceed this operation.

Parameters:
data - The data to write to the entry
Throws:
java.io.IOException
See Also:
addZipEntry(String), addZipEntry(String, String)

writeDataToZipEntry

public void writeDataToZipEntry(byte[] data,
                                int offset,
                                int length)
                         throws java.io.IOException
Writes data to the current entry.
A call to addZipEntry must preceed this operation.

Parameters:
data - The data to write to the entry
offset - The offset from where to start read data in the data array
length - The amount of data to read from the data array
Throws:
java.io.IOException
See Also:
addZipEntry(String), addZipEntry(String, String)

writeDataToZipEntry

public void writeDataToZipEntry(java.io.InputStream istream,
                                boolean closeInputStream)
                         throws java.io.IOException
Copies data from the provided stream to the current entry
A call to addZipEntry must preceed this operation.

Parameters:
istream - The inputstrream from where to copy data
closeInputStream - If the inputstream is to be closed
Throws:
java.io.IOException
See Also:
addZipEntry(String), addZipEntry(String, String)

closeZipEntry

public void closeZipEntry()
                   throws java.io.IOException
Closes the current zip entry.
A call to addZipEntry must preceed this operation.

Throws:
java.io.IOException
See Also:
addZipEntry(String), addZipEntry(String, String)

close

public void close()
           throws java.io.IOException
Close the outputstream to the zip file.
The methods does nothing if the outputstream is already closed.

Throws:
java.io.IOException

setCompressionLevel

public void setCompressionLevel(int compressionLevel)
Sets the level of compression.

Parameters:
compressionLevel - The compression level [0-9]

setZipfileComment

public void setZipfileComment(java.lang.String comment)
Set a comment for the current ZIP file.

Parameters:
comment - The ZIP file comment

createZipFile

public static ZipWriter createZipFile(java.io.File file)
                               throws java.io.IOException
Creates a new ZIP file.

Parameters:
file - The ZIP file
Returns:
A ZIPWriter instance
Throws:
java.io.IOException

createZipFile

public static ZipWriter createZipFile(java.io.File file,
                                      int compressionLevel)
                               throws java.io.IOException
Creates a new ZIP file.

Parameters:
file - The ZIP file
compressionLevel - The compression level [0-9]
Returns:
A ZIPWriter instance
Throws:
java.io.IOException

createZipFile

public static ZipWriter createZipFile(java.lang.String fileName)
                               throws java.io.IOException
Creates a new ZIP file.

Parameters:
fileName - Name of the zip file
Returns:
A ZIPWriter instance
Throws:
java.io.IOException

createZipFile

public static ZipWriter createZipFile(java.lang.String fileName,
                                      int compressionLevel)
                               throws java.io.IOException
Creates a new ZIP file.

Parameters:
fileName - Name of the zip file
compressionLevel - The compression level [0-9]
Returns:
A ZIPWriter instance
Throws:
java.io.IOException


Copyright © 1998-2005 dmonix.org Peter Nerg, All Rights Reserved.