org.kde.koala
Class KZip

java.lang.Object
  extended by org.kde.koala.KArchive
      extended by org.kde.koala.KZip
All Implemented Interfaces:
org.kde.qt.QtSupport

public class KZip
extends KArchive

This class implements a kioslave to access zip files from KDE. You can use it in IO_ReadOnly or in IO_WriteOnly mode, and it behaves just as expected. It can also be used in IO_ReadWrite mode, in this case one can append files to an existing zip archive. When you append new files, which are not yet in the zip, it works as expected, i.e. the files are appended at the end. When you append a file, which is already in the file, the reference to the old file is dropped and the new one is added to the zip - but the old data from the file itself is not deleted, it is still in the zipfile. so when you want to have a small and garbage-free zipfile, just read the contents of the appended zip file and write it to a new one in IO_WriteOnly mode. This is especially important when you don't want to leak information of how intermediate versions of files in the zip were looking. For more information on the zip fileformat go to http://www.pkware.com/products/enterprise/white_papers/appnote.html

Author:
Holger Schroeder

Field Summary
static int DefaultExtraField
           
static int DeflateCompression
           
static int ModificationTime
           
static int NoCompression
          Describes the compression type for a given file in the Zip archive.
static int NoExtraField
          Describes the contents of the "extra field" for a given file in the Zip archive.
 
Fields inherited from class org.kde.koala.KArchive
VIRTUAL_PREPARE_WRITING, VIRTUAL_WRITE_DATA, VIRTUAL_WRITE_DIR, VIRTUAL_WRITE_FILE, VIRTUAL_WRITE_SYMLINK
 
Constructor Summary
protected KZip(java.lang.Class dummy)
           
  KZip(org.kde.qt.QIODeviceInterface dev)
          Creates an instance that operates on the given device.
  KZip(java.lang.String filename)
          Creates an instance that operates on the given filename.
 
Method Summary
protected  boolean closeArchive()
           
 int compression()
          The current compression mode that will be used for new files.
 void dispose()
          Delete the wrapped C++ instance ahead of finalize()
 boolean doneWriting(int size)
          Write data to a file that has been created using prepareWriting().
 int extraField()
          The current type of "extra field" that will be used for new files.
 java.lang.String fileName()
          The name of the zip file, as passed to the constructor.
protected  void finalize()
          Deletes the wrapped C++ instance
 boolean isDisposed()
          Has the wrapped C++ instance been deleted?
protected  boolean openArchive(int mode)
          Opens the archive for reading.
protected  boolean prepareWriting_impl(java.lang.String name, java.lang.String user, java.lang.String group, int size, long perm, int atime, int mtime, int ctime)
           
 boolean prepareWriting(java.lang.String name, java.lang.String user, java.lang.String group, int size)
          Alternative method for writing: call prepareWriting(), then feed the data in small chunks using writeData(), and call doneWriting() when done.
 boolean prepareWriting(java.lang.String name, java.lang.String user, java.lang.String group, int size, long perm, int atime, int mtime, int ctime)
          Here's another way of writing a file into an archive: Call prepareWriting, then call writeData() as many times as wanted then call doneWriting( totalSize ).
 void setCompression(int c)
          Call this before writeFile or prepareWriting, to define whether the next files to be written should be compressed or not.
 void setExtraField(int ef)
          Call this before writeFile or prepareWriting, to define what the next file to be written should have in its extra field.
protected  boolean writeData_impl(java.lang.String data, int size)
           
 boolean writeData(java.lang.String data, int size)
          Write data to a file that has been created using prepareWriting().
 boolean writeDir(java.lang.String name, java.lang.String user, java.lang.String group)
          If an archive is opened for writing then you can add new directories using this function.
 boolean writeFile(java.lang.String name, java.lang.String user, java.lang.String group, int size, long perm, int atime, int mtime, int ctime, java.lang.String data)
          If an archive is opened for writing then you can add a new file using this function.
 boolean writeFile(java.lang.String name, java.lang.String user, java.lang.String group, int size, java.lang.String data)
          If an archive is opened for writing then you can add a new file using this function.
protected  boolean writeSymLink_impl(java.lang.String name, java.lang.String target, java.lang.String user, java.lang.String group, long perm, int atime, int mtime, int ctime)
           
 boolean writeSymLink(java.lang.String name, java.lang.String target, java.lang.String user, java.lang.String group, long perm, int atime, int mtime, int ctime)
          Writes a symbolic link to the archive if the archive must be opened for writing.
 
Methods inherited from class org.kde.koala.KArchive
addLocalDirectory, addLocalFile, close, closeSucceeded, device, directory, isOpened, mode, open, writeDir
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NoExtraField

public static final int NoExtraField
Describes the contents of the "extra field" for a given file in the Zip archive.

See Also:
Constant Field Values

ModificationTime

public static final int ModificationTime
See Also:
Constant Field Values

DefaultExtraField

public static final int DefaultExtraField
See Also:
Constant Field Values

NoCompression

public static final int NoCompression
Describes the compression type for a given file in the Zip archive.

See Also:
Constant Field Values

DeflateCompression

public static final int DeflateCompression
See Also:
Constant Field Values
Constructor Detail

KZip

protected KZip(java.lang.Class dummy)

KZip

public KZip(java.lang.String filename)
Creates an instance that operates on the given filename. using the compression filter associated to given mimetype.

Parameters:
filename - is a local path (e.g. "/home/holger/myfile.zip")

KZip

public KZip(org.kde.qt.QIODeviceInterface dev)
Creates an instance that operates on the given device. The device can be compressed (KFilterDev) or not (QFile, etc.).

Parameters:
dev - the device to access
Method Detail

fileName

public java.lang.String fileName()
The name of the zip file, as passed to the constructor. Null if you used the QIODevice constructor.

Returns:
the zip's file name, or null if a QIODevice is used

setExtraField

public void setExtraField(int ef)
Call this before writeFile or prepareWriting, to define what the next file to be written should have in its extra field.

Parameters:
ef - the type of "extra field"
See Also:
extraField()

extraField

public int extraField()
The current type of "extra field" that will be used for new files.

Returns:
the current type of "extra field"
See Also:
setExtraField(int)

setCompression

public void setCompression(int c)
Call this before writeFile or prepareWriting, to define whether the next files to be written should be compressed or not.

Parameters:
c - the new compression mode
See Also:
compression()

compression

public int compression()
The current compression mode that will be used for new files.

Returns:
the current compression mode
See Also:
setCompression(int)

writeFile

public boolean writeFile(java.lang.String name,
                         java.lang.String user,
                         java.lang.String group,
                         int size,
                         java.lang.String data)
If an archive is opened for writing then you can add a new file using this function. This method takes the whole data at once.

Overrides:
writeFile in class KArchive
Parameters:
name - can include subdirs e.g. path/to/the/file
user - the user owning the file
group - the group owning the file
size - the size of the file
data - a pointer to the data
Returns:
true if successful, false otherwise

prepareWriting

public boolean prepareWriting(java.lang.String name,
                              java.lang.String user,
                              java.lang.String group,
                              int size)
Alternative method for writing: call prepareWriting(), then feed the data in small chunks using writeData(), and call doneWriting() when done.

Overrides:
prepareWriting in class KArchive
Parameters:
name - can include subdirs e.g. path/to/the/file
user - the user owning the file
group - the group owning the file
size - unused argument
Returns:
true if successful, false otherwise

writeSymLink

public boolean writeSymLink(java.lang.String name,
                            java.lang.String target,
                            java.lang.String user,
                            java.lang.String group,
                            long perm,
                            int atime,
                            int mtime,
                            int ctime)
Description copied from class: KArchive
Writes a symbolic link to the archive if the archive must be opened for writing.

Overrides:
writeSymLink in class KArchive
Parameters:
name - name of symbolic link
target - target of symbolic link
user - the user that owns the directory
group - the group that owns the directory
perm - permissions of the directory
atime - time the file was last accessed
mtime - modification time of the file
ctime - creation time of the file

prepareWriting

public boolean prepareWriting(java.lang.String name,
                              java.lang.String user,
                              java.lang.String group,
                              int size,
                              long perm,
                              int atime,
                              int mtime,
                              int ctime)
Description copied from class: KArchive
Here's another way of writing a file into an archive: Call prepareWriting, then call writeData() as many times as wanted then call doneWriting( totalSize ). For tar.gz files, you need to know the size before hand, it is needed in the header! For zip files, size isn't used. This method also allows some file metadata to be set. However, depending on the archive type not all metadata might be regarded.

Overrides:
prepareWriting in class KArchive
Parameters:
name - the name of the file
user - the user that owns the file
group - the group that owns the file
size - the size of the file
perm - permissions of the file
atime - time the file was last accessed
mtime - modification time of the file
ctime - creation time of the file

writeFile

public boolean writeFile(java.lang.String name,
                         java.lang.String user,
                         java.lang.String group,
                         int size,
                         long perm,
                         int atime,
                         int mtime,
                         int ctime,
                         java.lang.String data)
Description copied from class: KArchive
If an archive is opened for writing then you can add a new file using this function. If the file name is for example "mydir/test1" then the directory "mydir" is automatically appended first if that did not happen yet. This method also allows some file metadata to be set. However, depending on the archive type not all metadata might be regarded.

Overrides:
writeFile in class KArchive
Parameters:
name - the name of the file
user - the user that owns the file
group - the group that owns the file
size - the size of the file
perm - permissions of the file
atime - time the file was last accessed
mtime - modification time of the file
ctime - creation time of the file
data - the data to write (size bytes)

writeData

public boolean writeData(java.lang.String data,
                         int size)
Write data to a file that has been created using prepareWriting().

Overrides:
writeData in class KArchive
Parameters:
data - a pointer to the data
size - the size of the chunk
Returns:
true if successful, false otherwise

doneWriting

public boolean doneWriting(int size)
Write data to a file that has been created using prepareWriting().

Overrides:
doneWriting in class KArchive
Parameters:
size - the size of the file
Returns:
true if successful, false otherwise
See Also:
KArchive.prepareWriting(java.lang.String, java.lang.String, java.lang.String, int)

openArchive

protected boolean openArchive(int mode)
Opens the archive for reading. Parses the directory listing of the archive and creates the KArchiveDirectory/KArchiveFile entries.

Parameters:
mode - the mode of the file

closeArchive

protected boolean closeArchive()

writeDir

public boolean writeDir(java.lang.String name,
                        java.lang.String user,
                        java.lang.String group)
Description copied from class: KArchive
If an archive is opened for writing then you can add new directories using this function. KArchive won't write one directory twice.

Overrides:
writeDir in class KArchive
Parameters:
name - the name of the directory
user - the user that owns the directory
group - the group that owns the directory

writeData_impl

protected boolean writeData_impl(java.lang.String data,
                                 int size)

prepareWriting_impl

protected boolean prepareWriting_impl(java.lang.String name,
                                      java.lang.String user,
                                      java.lang.String group,
                                      int size,
                                      long perm,
                                      int atime,
                                      int mtime,
                                      int ctime)

writeSymLink_impl

protected boolean writeSymLink_impl(java.lang.String name,
                                    java.lang.String target,
                                    java.lang.String user,
                                    java.lang.String group,
                                    long perm,
                                    int atime,
                                    int mtime,
                                    int ctime)

finalize

protected void finalize()
                 throws java.lang.InternalError
Deletes the wrapped C++ instance

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.InternalError

dispose

public void dispose()
Delete the wrapped C++ instance ahead of finalize()


isDisposed

public boolean isDisposed()
Has the wrapped C++ instance been deleted?