KArchive 5.109.0
Public Types | Public Member Functions | Protected Member Functions | List of all members
KZip

A class for reading / writing zip archives. More...

Public Types

enum  Compression { NoCompression = 0 , DeflateCompression = 1 }
 Describes the compression type for a given file in the Zip archive. More...
 
enum  ExtraField { NoExtraField = 0 , ModificationTime = 1 , DefaultExtraField = 1 }
 Describes the contents of the "extra field" for a given file in the Zip archive. More...
 

Public Member Functions

 KZip (const QString &filename)
 Creates an instance that operates on the given filename.
 
 KZip (QIODevice *dev)
 Creates an instance that operates on the given device.
 
 ~KZip () override
 If the zip file is still opened, then it will be closed automatically by the destructor.
 
Compression compression () const
 The current compression mode that will be used for new files.
 
ExtraField extraField () const
 The current type of "extra field" that will be used for new files.
 
void setCompression (Compression c)
 Call this before writeFile or prepareWriting, to define whether the next files to be written should be compressed or not.
 
void setExtraField (ExtraField ef)
 Call this before writeFile or prepareWriting, to define what the next file to be written should have in its extra field.
 
bool writeData (const char *data, qint64 size) override
 Write data to a file that has been created using prepareWriting().
 
- Public Member Functions inherited from KArchive
bool addLocalDirectory (const QString &path, const QString &destName)
 Writes a local directory into the archive, including all its contents, recursively.
 
bool addLocalFile (const QString &fileName, const QString &destName)
 Writes a local file into the archive.
 
virtual bool close ()
 Closes the archive.
 
QIODevice * device () const
 The underlying device.
 
const KArchiveDirectorydirectory () const
 If an archive is opened for reading, then the contents of the archive can be accessed via this function.
 
QString errorString () const
 Returns a description of the last error.
 
QString fileName () const
 The name of the archive file, as passed to the constructor that takes a fileName, or an empty string if you used the QIODevice constructor.
 
bool finishWriting (qint64 size)
 Call finishWriting after writing the data.
 
bool isOpen () const
 Checks whether the archive is open.
 
QIODevice::OpenMode mode () const
 Returns the mode in which the archive was opened.
 
virtual bool open (QIODevice::OpenMode mode)
 Opens the archive for reading or writing.
 
bool prepareWriting (const QString &name, const QString &user, const QString &group, qint64 size, mode_t perm=0100644, const QDateTime &atime=QDateTime(), const QDateTime &mtime=QDateTime(), const QDateTime &ctime=QDateTime())
 Here's another way of writing a file into an archive: Call prepareWriting(), then call writeData() as many times as wanted then call finishWriting( totalSize ).
 
virtual bool writeData (const char *data, qint64 size)
 Write data into the current file - to be called after calling prepareWriting.
 
bool writeDir (const QString &name, const QString &user=QString(), const QString &group=QString(), mode_t perm=040755, const QDateTime &atime=QDateTime(), const QDateTime &mtime=QDateTime(), const QDateTime &ctime=QDateTime())
 If an archive is opened for writing then you can add new directories using this function.
 
bool writeFile (const QString &name, const QByteArray &data, mode_t perm=0100644, const QString &user=QString(), const QString &group=QString(), const QDateTime &atime=QDateTime(), const QDateTime &mtime=QDateTime(), const QDateTime &ctime=QDateTime())
 Writes a new file into the archive.
 
bool writeFile (const QString &name, const QString &user, const QString &group, const char *data, qint64 size, mode_t perm=0100644, const QDateTime &atime=QDateTime(), const QDateTime &mtime=QDateTime(), const QDateTime &ctime=QDateTime())
 
bool writeSymLink (const QString &name, const QString &target, const QString &user=QString(), const QString &group=QString(), mode_t perm=0120755, const QDateTime &atime=QDateTime(), const QDateTime &mtime=QDateTime(), const QDateTime &ctime=QDateTime())
 Writes a symbolic link to the archive if supported.
 

Protected Member Functions

bool closeArchive () override
 Closes the archive.
 
bool doFinishWriting (qint64 size) override
 Write data to a file that has been created using prepareWriting().
 
bool doPrepareWriting (const QString &name, const QString &user, const QString &group, qint64 size, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &creationTime) override
 Reimplemented from KArchive.
 
bool doWriteDir (const QString &name, const QString &user, const QString &group, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime) override
 Reimplemented from KArchive.
 
bool doWriteSymLink (const QString &name, const QString &target, const QString &user, const QString &group, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime) override
 Reimplemented from KArchive.
 
bool openArchive (QIODevice::OpenMode mode) override
 Opens the archive for reading.
 
void virtual_hook (int id, void *data) override
 
- Protected Member Functions inherited from KArchive
 KArchive (const QString &fileName)
 Base constructor (protected since this is a pure virtual class).
 
 KArchive (QIODevice *dev)
 Base constructor (protected since this is a pure virtual class).
 
virtual bool closeArchive ()=0
 Closes the archive.
 
virtual bool createDevice (QIODevice::OpenMode mode)
 Can be reimplemented in order to change the creation of the device (when using the fileName constructor).
 
virtual bool doFinishWriting (qint64 size)=0
 Called after writing the data.
 
virtual bool doPrepareWriting (const QString &name, const QString &user, const QString &group, qint64 size, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime)=0
 This virtual method must be implemented by subclasses.
 
virtual bool doWriteDir (const QString &name, const QString &user, const QString &group, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime)=0
 Write a directory to the archive.
 
virtual bool doWriteSymLink (const QString &name, const QString &target, const QString &user, const QString &group, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime)=0
 Writes a symbolic link to the archive.
 
KArchiveDirectoryfindOrCreate (const QString &path)
 Ensures that path exists, create otherwise.
 
virtual bool openArchive (QIODevice::OpenMode mode)=0
 Opens an archive for reading or writing.
 
virtual KArchiveDirectoryrootDir ()
 Retrieves or create the root directory.
 
void setDevice (QIODevice *dev)
 Can be called by derived classes in order to set the underlying device.
 
void setErrorString (const QString &errorStr)
 Sets error description.
 
void setRootDir (KArchiveDirectory *rootDir)
 Derived classes call setRootDir from openArchive, to set the root directory after parsing an existing archive.
 
virtual void virtual_hook (int id, void *data)
 

Detailed Description

A class for reading / writing zip archives.

You can use it in QIODevice::ReadOnly or in QIODevice::WriteOnly mode, and it behaves just as expected. It can also be used in QIODevice::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 QIODevice::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 holge.nosp@m.r-kd.nosp@m.e@hol.nosp@m.gis..nosp@m.net

Member Enumeration Documentation

◆ Compression

Describes the compression type for a given file in the Zip archive.

Enumerator
NoCompression 

Uncompressed.

DeflateCompression 

Deflate compression method.

◆ ExtraField

Describes the contents of the "extra field" for a given file in the Zip archive.

Enumerator
NoExtraField 

No extra field.

ModificationTime 

Modification time ("extended timestamp" header)

Constructor & Destructor Documentation

◆ KZip() [1/2]

KZip::KZip ( const QString &  filename)

Creates an instance that operates on the given filename.

using the compression filter associated to given mimetype.

Parameters
filenameis a local path (e.g. "/home/holger/myfile.zip")

◆ KZip() [2/2]

KZip::KZip ( QIODevice *  dev)

Creates an instance that operates on the given device.

The device can be compressed (KCompressionDevice) or not (QFile, etc.).

Warning
Do not assume that giving a QFile here will decompress the file, in case it's compressed!
Parameters
devthe device to access

◆ ~KZip()

KZip::~KZip ( )
override

If the zip file is still opened, then it will be closed automatically by the destructor.

Member Function Documentation

◆ closeArchive()

bool KZip::closeArchive ( )
overrideprotectedvirtual

Closes the archive.

Implements KArchive.

◆ compression()

Compression KZip::compression ( ) const

The current compression mode that will be used for new files.

Returns
the current compression mode
See also
setCompression()

◆ doFinishWriting()

bool KZip::doFinishWriting ( qint64  size)
overrideprotectedvirtual

Write data to a file that has been created using prepareWriting().

Parameters
sizethe size of the file
Returns
true if successful, false otherwise

Implements KArchive.

◆ doPrepareWriting()

bool KZip::doPrepareWriting ( const QString &  name,
const QString &  user,
const QString &  group,
qint64  size,
mode_t  perm,
const QDateTime &  atime,
const QDateTime &  mtime,
const QDateTime &  creationTime 
)
overrideprotectedvirtual

Reimplemented from KArchive.

Implements KArchive.

◆ doWriteDir()

bool KZip::doWriteDir ( const QString &  name,
const QString &  user,
const QString &  group,
mode_t  perm,
const QDateTime &  atime,
const QDateTime &  mtime,
const QDateTime &  ctime 
)
overrideprotectedvirtual

Reimplemented from KArchive.

Implements KArchive.

◆ doWriteSymLink()

bool KZip::doWriteSymLink ( const QString &  name,
const QString &  target,
const QString &  user,
const QString &  group,
mode_t  perm,
const QDateTime &  atime,
const QDateTime &  mtime,
const QDateTime &  ctime 
)
overrideprotectedvirtual

Reimplemented from KArchive.

Implements KArchive.

◆ extraField()

ExtraField KZip::extraField ( ) const

The current type of "extra field" that will be used for new files.

Returns
the current type of "extra field"
See also
setExtraField()

◆ openArchive()

bool KZip::openArchive ( QIODevice::OpenMode  mode)
overrideprotectedvirtual

Opens the archive for reading.

Parses the directory listing of the archive and creates the KArchiveDirectory/KArchiveFile entries.

Parameters
modethe mode of the file

Implements KArchive.

◆ setCompression()

void KZip::setCompression ( Compression  c)

Call this before writeFile or prepareWriting, to define whether the next files to be written should be compressed or not.

Parameters
cthe new compression mode
See also
compression()

◆ setExtraField()

void KZip::setExtraField ( ExtraField  ef)

Call this before writeFile or prepareWriting, to define what the next file to be written should have in its extra field.

Parameters
efthe type of "extra field"
See also
extraField()

◆ virtual_hook()

void KZip::virtual_hook ( int  id,
void *  data 
)
overrideprotectedvirtual

Reimplemented from KArchive.

◆ writeData()

bool KZip::writeData ( const char *  data,
qint64  size 
)
overridevirtual

Write data to a file that has been created using prepareWriting().

Parameters
dataa pointer to the data
sizethe size of the chunk
Returns
true if successful, false otherwise

Reimplemented from KArchive.