QuaZip Class Reference

ZIP archive. More...

`#include <quazip/quazip.h>`

Collaboration diagram for QuaZip:
[legend]

List of all members.

## Public Types

enum | Constants { MAX_FILE_NAME_LENGTH = 256 }
| Useful constants. More...
enum | Mode { mdNotOpen, mdUnzip, mdCreate, mdAppend, mdAdd }
| Open mode of the ZIP file. More...
enum | CaseSensitivity { csDefault = 0, csSensitive = 1, csInsensitive = 2 }
| Case sensitivity for the file names. More...

## Public Member Functions

| QuaZip ()
| Constructs QuaZip object.
| QuaZip (const QString &zipName)
| Constructs QuaZip object associated with ZIP file zipName.
| QuaZip (QIODevice *ioDevice)
| Constructs QuaZip object associated with ZIP file represented by ioDevice.
| ~QuaZip ()
| Destroys QuaZip object.
bool | open (Mode mode, zlib_filefunc_def *ioApi=NULL)
| Opens ZIP file.
void | close ()
| Closes ZIP file.
void | setFileNameCodec (QTextCodec *fileNameCodec)
| Sets the codec used to encode/decode file names inside archive.
void | setFileNameCodec (const char *fileNameCodecName)
| Sets the codec used to encode/decode file names inside archive.
QTextCodec * | getFileNameCodec () const
| Returns the codec used to encode/decode comments inside archive.
void | setCommentCodec (QTextCodec *commentCodec)
| Sets the codec used to encode/decode comments inside archive.
void | setCommentCodec (const char *commentCodecName)
| Sets the codec used to encode/decode comments inside archive.
QTextCodec * | getCommentCodec () const
| Returns the codec used to encode/decode comments inside archive.
QString | getZipName () const
| Returns the name of the ZIP file.
void | setZipName (const QString &zipName)
| Sets the name of the ZIP file.
QIODevice * | getIoDevice () const
| Returns the device representing this ZIP file.
void | setIoDevice (QIODevice *ioDevice)
| Sets the device representing the ZIP file.
Mode | getMode () const
| Returns the mode in which ZIP file was opened.
bool | isOpen () const
| Returns `true` if ZIP file is open, `false` otherwise.
int | getZipError () const
| Returns the error code of the last operation.
int | getEntriesCount () const
| Returns number of the entries in the ZIP central directory.
QString | getComment () const
| Returns global comment in the ZIP file.
void | setComment (const QString &comment)
| Sets the global comment in the ZIP file.
bool | goToFirstFile ()
| Sets the current file to the first file in the archive.
bool | goToNextFile ()
| Sets the current file to the next file in the archive.
bool | setCurrentFile (const QString &fileName, CaseSensitivity cs=csDefault)
| Sets current file by its name.
bool | hasCurrentFile () const
| Returns `true` if the current file has been set.
bool | getCurrentFileInfo (QuaZipFileInfo *info) const
| Retrieves information about the current file.
QString | getCurrentFileName () const
| Returns the current file name.
unzFile | getUnzFile ()
| Returns `unzFile` handle.
zipFile | getZipFile ()
| Returns `zipFile` handle.
void | setDataDescriptorWritingEnabled (bool enabled)
| Changes the data descriptor writing mode.
bool | isDataDescriptorWritingEnabled () const
| Returns the data descriptor default writing mode.
QStringList | getFileNameList () const
| Returns a list of files inside the archive.
QList< QuaZipFileInfo > | getFileInfoList () const
| Returns information list about all files inside the archive.

## Static Public Member Functions

static Qt::CaseSensitivity | convertCaseSensitivity (CaseSensitivity cs)
| Returns the actual case sensitivity for the specified QuaZIP one.

## Friends

class | QuaZipPrivate

## Detailed Description

ZIP archive.

This class implements basic interface to the ZIP archive. It can be used to read table contents of the ZIP archive and retreiving information about the files inside it.

You can also use this class to open files inside archive by passing pointer to the instance of this class to the constructor of the QuaZipFile class. But see QuaZipFile::QuaZipFile(QuaZip*, QObject*) for the possible pitfalls.

This class is indended to provide interface to the ZIP subpackage of the ZIP/UNZIP package as well as to the UNZIP subpackage. But currently it supports only UNZIP.

The use of this class is simple - just create instance using constructor, then set ZIP archive file name using setFile() function (if you did not passed the name to the constructor), then open() and then use different functions to work with it! Well, if you are paranoid, you may also wish to call close before destructing the instance, to check for errors on close.

You may also use getUnzFile() and getZipFile() functions to get the ZIP archive handle and use it with ZIP/UNZIP package API directly.

This class supports localized file names inside ZIP archive, but you have to set up proper codec with setCodec() function. By default, locale codec will be used, which is probably ok for UNIX systems, but will almost certainly fail with ZIP archives created in Windows. This is because Windows ZIP programs have strange habit of using DOS encoding for file names in ZIP archives. For example, ZIP archive with cyrillic names created in Windows will have file names in `IBM866` encoding instead of `WINDOWS-1251`. I think that calling one function is not much trouble, but for true platform independency it would be nice to have some mechanism for file name encoding auto detection using locale information. Does anyone know a good way to do it?

## Member Enumeration Documentation

enum QuaZip::Constants

Useful constants.

Enumerator:

  MAX_FILE_NAME_LENGTH | Maximum file name length. Taken from `UNZ_MAXFILENAMEINZIP` constant in unzip.c.

enum QuaZip::Mode

Open mode of the ZIP file.

Enumerator:

  mdNotOpen | ZIP file is not open. This is the initial mode.
  mdUnzip | ZIP file is open for reading files inside it.
  mdCreate | ZIP file was created with open() call.
  mdAppend | ZIP file was opened in append mode. This refers to `APPEND_STATUS_CREATEAFTER` mode in ZIP/UNZIP package and means that zip is appended to some existing file what is useful when that file contains self-extractor code. This is obviously not what you whant to use to add files to the existing ZIP archive.
  mdAdd | ZIP file was opened for adding files in the archive.

enum QuaZip::CaseSensitivity

Case sensitivity for the file names.

This is what you specify when accessing files in the archive. Works perfectly fine with any characters thanks to Qt's great unicode support. This is different from ZIP/UNZIP API, where only US-ASCII characters was supported.

Enumerator:

  csDefault | Default for platform. Case sensitive for UNIX, not for Windows.
  csSensitive | Case sensitive.
  csInsensitive | Case insensitive.

## Constructor & Destructor Documentation

QuaZip::QuaZip | ( |  | ) |

Constructs QuaZip object.

Call setName() before opening constructed object.

QuaZip::QuaZip | ( | QIODevice * | ioDevice | ) |

Constructs QuaZip object associated with ZIP file represented by ioDevice.

The IO device must be seekable, otherwise an error will occur when opening.

QuaZip::~QuaZip | ( |  | ) |

Destroys QuaZip object.

Calls close() if necessary.

References close(), and isOpen().

## Member Function Documentation

Qt::CaseSensitivity QuaZip::convertCaseSensitivity | ( | QuaZip::CaseSensitivity | cs | ) | `[static]`

Returns the actual case sensitivity for the specified QuaZIP one.

Parameters:

  cs | The value to convert.

Returns:
  If CaseSensitivity::csDefault, then returns the default file name case sensitivity for the platform. Otherwise, just returns the appropriate value from the Qt::CaseSensitivity enum.

References csDefault, and csSensitive.

Referenced by QuaZipDir::exists(), and setCurrentFile().

bool QuaZip::open | ( | Mode | mode,
|  | zlib_filefunc_def * | ioApi = `NULL`
| ) |  |

Opens ZIP file.

Argument mode specifies open mode of the ZIP archive. See Mode for details. Note that there is zipOpen2() function in the ZIP/UNZIP API which accepts globalcomment argument, but it does not use it anywhere, so this open() function does not have this argument. See setComment() if you need to set global comment.

If the ZIP file is accessed via explicitly set QIODevice, then this device is opened in the necessary mode. If the device was already opened by some other means, then the behaviour is defined by the device implementation, but generally it is not a very good idea. For example, QFile will at least issue a warning.

Returns:
  `true` if successful, `false` otherwise.

Note:
  ZIP/UNZIP API open calls do not return error code - they just return `NULL` indicating an error. But to make things easier, quazip.h header defines additional error code `UNZ_ERROROPEN` and getZipError() will return it if the open call of the ZIP/UNZIP API returns `NULL`.

Argument ioApi specifies IO function set for ZIP/UNZIP package to use. See unzip.h, zip.h and ioapi.h for details. Note that IO API for QuaZip is different from the original package. The file path argument was changed to be of type `voidpf`, and QuaZip passes a QIODevice pointer there. This QIODevice is either set explicitly via setIoDevice() or the QuaZip(QIODevice*) constructor, or it is created internally when opening the archive by its file name. The default API (qioapi.cpp) just delegates everything to the QIODevice API. Not only this allows to use a QIODevice instead of file name, but also has a nice side effect of raising the file size limit from 2G to 4G.

In short: just forget about the ioApi argument and you'll be fine.

References isOpen(), mdAdd, mdAppend, mdCreate, mdUnzip, QuaZipPrivate::unzFile_f, and QuaZipPrivate::zipFile_f.

Referenced by JlCompress::compressDir(), JlCompress::compressFile(), JlCompress::compressFiles(), JlCompress::extractDir(), JlCompress::extractFile(), JlCompress::extractFiles(), JlCompress::getFileList(), and QuaZipFile::open().

void QuaZip::close | ( |  | ) |

Closes ZIP file.

Call getZipError() to determine if the close was successful. The underlying QIODevice is also closed, regardless of whether it was set explicitly or not.

References mdAdd, mdAppend, mdCreate, mdNotOpen, mdUnzip, QuaZipPrivate::unzFile_f, and QuaZipPrivate::zipFile_f.

Referenced by QuaZipFile::close(), JlCompress::compressDir(), JlCompress::compressFile(), JlCompress::compressFiles(), JlCompress::extractDir(), JlCompress::extractFile(), JlCompress::extractFiles(), JlCompress::getFileList(), QuaZipFile::open(), and ~QuaZip().

void QuaZip::setFileNameCodec | ( | QTextCodec * | fileNameCodec | ) |

Sets the codec used to encode/decode file names inside archive.

This is necessary to access files in the ZIP archive created under Windows with non-latin characters in file names. For example, file names with cyrillic letters will be in `IBM866` encoding.

void QuaZip::setFileNameCodec | ( | const char * | fileNameCodecName | ) |

Sets the codec used to encode/decode file names inside archive.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Equivalent to calling setFileNameCodec(QTextCodec::codecForName(codecName));

void QuaZip::setCommentCodec | ( | QTextCodec * | commentCodec | ) |

Sets the codec used to encode/decode comments inside archive.

This codec defaults to locale codec, which is probably ok.

void QuaZip::setCommentCodec | ( | const char * | commentCodecName | ) |

Sets the codec used to encode/decode comments inside archive.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Equivalent to calling setCommentCodec(QTextCodec::codecForName(codecName));

QString QuaZip::getZipName | ( |  | ) | const

Returns the name of the ZIP file.

Returns null string if no ZIP file name has been set, for example when the QuaZip instance is set up to use a QIODevice instead.

See also:
  setZipName(), setIoDevice(), getIoDevice()

Referenced by QuaZipFile::getZipName().

void QuaZip::setZipName | ( | const QString & | zipName | ) |

Sets the name of the ZIP file.

Does nothing if the ZIP file is open.

Does not reset error code returned by getZipError().

See also:
  setIoDevice(), getIoDevice(), getZipName()

References isOpen().

QIODevice * QuaZip::getIoDevice | ( |  | ) | const

Returns the device representing this ZIP file.

Returns null string if no device has been set explicitly, for example when opening a ZIP file by name.

See also:
  setIoDevice(), getZipName(), setZipName()

void QuaZip::setIoDevice | ( | QIODevice * | ioDevice | ) |

Sets the device representing the ZIP file.

Does nothing if the ZIP file is open.

Does not reset error code returned by getZipError().

See also:
  getIoDevice(), getZipName(), setZipName()

References isOpen().

int QuaZip::getZipError | ( |  | ) | const

Returns the error code of the last operation.

Returns `UNZ_OK` if the last operation was successful.

Error code resets to `UNZ_OK` every time you call any function that accesses something inside ZIP archive, even if it is `const` (like getEntriesCount()). open() and close() calls reset error code too. See documentation for the specific functions for details on error detection.

Referenced by QuaZipFile::close(), JlCompress::compressDir(), JlCompress::compressFile(), JlCompress::compressFiles(), JlCompress::extractDir(), JlCompress::extractFile(), JlCompress::extractFiles(), QuaZipFile::getActualFileName(), QuaZipFile::getFileInfo(), JlCompress::getFileList(), and QuaZipFile::open().

int QuaZip::getEntriesCount | ( |  | ) | const

Returns number of the entries in the ZIP central directory.

Returns negative error code in the case of error. The same error code will be returned by subsequent getZipError() call.

References mdUnzip, and QuaZipPrivate::unzFile_f.

void QuaZip::setComment | ( | const QString & | comment | ) |

Sets the global comment in the ZIP file.

The comment will be written to the archive on close operation. QuaZip makes a distinction between a null QByteArray() comment and an empty "" comment in the QuaZip::mdAdd mode. A null comment is the default and it means "don't change the comment". An empty comment removes the original comment.

See also:
  open()

bool QuaZip::goToFirstFile | ( |  | ) |

Sets the current file to the first file in the archive.

Returns `true` on success, `false` otherwise. Call getZipError() to get the error code.

References mdUnzip, and QuaZipPrivate::unzFile_f.

Referenced by JlCompress::extractDir(), JlCompress::getFileList(), and setCurrentFile().

bool QuaZip::goToNextFile | ( |  | ) |

Sets the current file to the next file in the archive.

Returns `true` on success, `false` otherwise. Call getZipError() to determine if there was an error.

Should be used only in QuaZip::mdUnzip mode.

Note:
  If the end of file was reached, getZipError() will return `UNZ_OK` instead of `UNZ_END_OF_LIST_OF_FILE`. This is to make things like this easier:

       for(bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) {
         // do something
       }
       if(zip.getZipError()==UNZ_OK) {
         // ok, there was no error
       }

References mdUnzip, and QuaZipPrivate::unzFile_f.

Referenced by JlCompress::extractDir(), JlCompress::getFileList(), and setCurrentFile().

bool QuaZip::setCurrentFile | ( | const QString & | fileName,
|  | CaseSensitivity | cs = `csDefault`
| ) |  |

Sets current file by its name.

Returns `true` if successful, `false` otherwise. Argument cs specifies case sensitivity of the file name. Call getZipError() in the case of a failure to get error code.

This is not a wrapper to unzLocateFile() function. That is because I had to implement locale-specific case-insensitive comparison.

Here are the differences from the original implementation:

- If the file was not found, error code is `UNZ_OK`, not `UNZ_END_OF_LIST_OF_FILE` (see also goToNextFile()).
- If this function fails, it unsets the current file rather than resetting it back to what it was before the call.

If fileName is null string then this function unsets the current file and return `true`. Note that you should close the file first if it is open! See QuaZipFile::QuaZipFile(QuaZip*,QObject*) for the details.

Should be used only in QuaZip::mdUnzip mode.

See also:
  setFileNameCodec(), CaseSensitivity

References convertCaseSensitivity(), getCurrentFileName(), goToFirstFile(), goToNextFile(), MAX_FILE_NAME_LENGTH, mdUnzip, and QuaZipPrivate::unzFile_f.

Referenced by QuaZipFile::open().

bool QuaZip::getCurrentFileInfo | ( | QuaZipFileInfo * | info | ) | const

Retrieves information about the current file.

Fills the structure pointed by info. Returns `true` on success, `false` otherwise. In the latter case structure pointed by info remains untouched. If there was an error, getZipError() returns error code.

Should be used only in QuaZip::mdUnzip mode.

Does nothing and returns `false` in any of the following cases.

- ZIP is not open;
- ZIP does not have current file;
- info is `NULL`;

In all these cases getZipError() returns `UNZ_OK` since there is no ZIP/UNZIP API call.

References QuaZipFileInfo::comment, QuaZipFileInfo::compressedSize, QuaZipFileInfo::crc, QuaZipFileInfo::dateTime, QuaZipFileInfo::diskNumberStart, QuaZipFileInfo::externalAttr, QuaZipFileInfo::extra, QuaZipFileInfo::flags, hasCurrentFile(), QuaZipFileInfo::internalAttr, isOpen(), mdUnzip, QuaZipFileInfo::method, QuaZipFileInfo::name, QuaZipFileInfo::uncompressedSize, QuaZipPrivate::unzFile_f, QuaZipFileInfo::versionCreated, and QuaZipFileInfo::versionNeeded.

Referenced by QuaZipFile::getFileInfo(), and JlCompress::getFileList().

QString QuaZip::getCurrentFileName | ( |  | ) | const

Returns the current file name.

Equivalent to calling getCurrentFileInfo() and then getting `name` field of the QuaZipFileInfo structure, but faster and more convenient.

Should be used only in QuaZip::mdUnzip mode.

References hasCurrentFile(), isOpen(), MAX_FILE_NAME_LENGTH, mdUnzip, and QuaZipPrivate::unzFile_f.

Referenced by JlCompress::extractDir(), QuaZipFile::getActualFileName(), and setCurrentFile().

unzFile QuaZip::getUnzFile | ( |  | ) |

Returns `unzFile` handle.

You can use this handle to directly call UNZIP part of the ZIP/UNZIP package functions (see unzip.h).

Warning:
  When using the handle returned by this function, please keep in mind that QuaZip class is unable to detect any changes you make in the ZIP file state (e. g. changing current file, or closing the handle). So please do not do anything with this handle that is possible to do with the functions of this class. Or at least return the handle in the original state before calling some another function of this class (including implicit destructor calls and calls from the QuaZipFile objects that refer to this QuaZip instance!). So if you have changed the current file in the ZIP archive - then change it back or you may experience some strange behavior or even crashes.

References QuaZipPrivate::unzFile_f.

Referenced by QuaZipFile::atEnd(), QuaZipFile::close(), QuaZipFile::csize(), QuaZipFile::open(), QuaZipFile::pos(), QuaZipFile::readData(), and QuaZipFile::usize().

zipFile QuaZip::getZipFile | ( |  | ) |

Returns `zipFile` handle.

You can use this handle to directly call ZIP part of the ZIP/UNZIP package functions (see zip.h). Warnings about the getUnzFile() function also apply to this function.

References QuaZipPrivate::zipFile_f.

Referenced by QuaZipFile::close(), QuaZipFile::open(), and QuaZipFile::writeData().

void QuaZip::setDataDescriptorWritingEnabled | ( | bool | enabled | ) |

Changes the data descriptor writing mode.

According to the ZIP format specification, a file inside archive may have a data descriptor immediately following the file data. This is reflected by a special flag in the local file header and in the central directory. By default, QuaZIP sets this flag and writes the data descriptor unless both method and level were set to 0, in which case it operates in 1.0-compatible mode and never writes data descriptors.

By setting this flag to false, it is possible to disable data descriptor writing, thus increasing compatibility with archive readers that don't understand this feature of the ZIP file format.

Setting this flag affects all the QuaZipFile instances that are opened after this flag is set.

The data descriptor writing mode is enabled by default.

Parameters:

  enabled | If `true`, enable local descriptor writing, disable it otherwise.

See also:
  QuaZipFile::setDataDescriptorWritingEnabled()

bool QuaZip::isDataDescriptorWritingEnabled | ( |  | ) | const

Returns the data descriptor default writing mode.

See also:
  setDataDescriptorWritingEnabled()

Referenced by QuaZipFile::open().

QStringList QuaZip::getFileNameList | ( |  | ) | const

Returns a list of files inside the archive.

Returns:
  A list of file names or an empty list if there was an error or if the archive is empty (call getZipError() to figure out which).

See also:
  getFileInfoList()

QList< QuaZipFileInfo > QuaZip::getFileInfoList | ( |  | ) | const

Returns information list about all files inside the archive.

Returns:
  A list of QuaZipFileInfo objects or an empty list if there was an error or if the archive is empty (call getZipError() to figure out which).

See also:
  getFileNameList()

The documentation for this class was generated from the following files:

- quazip/quazip.h
- quazip/quazip.cpp