QuaZIP quazip-0-7-2
|
ZIP archive. More...
#include <quazip/quazip.h>
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. | |
bool | getCurrentFileInfo (QuaZipFileInfo64 *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. | |
QList< QuaZipFileInfo64 > | getFileInfoList64 () const |
Returns information list about all files inside the archive. | |
void | setZip64Enabled (bool zip64) |
Enables the zip64 mode. | |
bool | isZip64Enabled () const |
Returns whether the zip64 mode is enabled. | |
bool | isAutoClose () const |
Returns the auto-close flag. | |
void | setAutoClose (bool autoClose) const |
Sets or unsets the auto-close flag. | |
Static Public Member Functions | |
static Qt::CaseSensitivity | convertCaseSensitivity (CaseSensitivity cs) |
Returns the actual case sensitivity for the specified QuaZIP one. | |
static void | setDefaultFileNameCodec (QTextCodec *codec) |
Sets the default file name codec to use. | |
static void | setDefaultFileNameCodec (const char *codecName) |
Friends | |
class | QuaZipPrivate |
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?
enum QuaZip::Constants |
enum QuaZip::Mode |
Open mode of the ZIP file.
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 |
mdAdd |
ZIP file was opened for adding files in the archive. |
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.
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 | ( | ) |
Qt::CaseSensitivity QuaZip::convertCaseSensitivity | ( | QuaZip::CaseSensitivity | cs | ) | [static] |
Returns the actual case sensitivity for the specified QuaZIP one.
cs | The value to convert. |
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 QuaZIP checks if the open mode is compatible to the mode needed for the requested operation. If necessary, seeking is performed to position the device properly.
true
if successful, false
otherwise.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 non-zip64 archives).
In short: just forget about the ioApi argument and you'll be fine.
References QString::isEmpty(), isOpen(), QIODevice::isSequential(), mdAdd, mdAppend, mdCreate, mdUnzip, QuaZipPrivate::unzFile_f, and QuaZipPrivate::zipFile_f.
Referenced by JlCompress::compressDir(), JlCompress::compressFile(), JlCompress::compressFiles(), and QuaZipFile::open().
void QuaZip::close | ( | ) |
Closes ZIP file.
Call getZipError() to determine if the close was successful.
If the file was opened by name, then the underlying QIODevice is closed and deleted.
If the underlying QIODevice was set explicitly using setIoDevice() or the appropriate constructor, then it is closed if the auto-close flag is set (which it is by default). Call setAutoClose() to clear the auto-close flag if this behavior is undesirable.
Since Qt 5.1, the QSaveFile was introduced. It breaks the QIODevice API by making close() private and crashing the application if it is called from the base class where it is public. It is an excellent example of poor design that illustrates why you should never ever break an is-a relationship between the base class and a subclass. QuaZIP works around this bug by checking if the QIODevice is an instance of QSaveFile, using qobject_cast<>, and if it is, calls QSaveFile::commit() instead of close(). It is a really ugly hack, but at least it makes your programs work instead of crashing. Note that if the auto-close flag is cleared, then this is a non-issue, and commit() isn't called.
References QByteArray::constData(), QTextCodec::fromUnicode(), QString::isEmpty(), QString::isNull(), mdAdd, mdAppend, mdCreate, mdNotOpen, mdUnzip, QuaZipPrivate::unzFile_f, and QuaZipPrivate::zipFile_f.
Referenced by QuaZipFile::close(), JlCompress::compressDir(), JlCompress::compressFile(), JlCompress::compressFiles(), 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));
References QTextCodec::codecForName().
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));
References QTextCodec::codecForName().
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.
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().
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.
References QString::isEmpty().
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().
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(), QuaZipFile::getActualFileName(), QuaZipFile::getFileInfo(), 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.
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.
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.
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 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:
UNZ_OK
, not UNZ_END_OF_LIST_OF_FILE
(see also goToNextFile()).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.
References QHash::contains(), convertCaseSensitivity(), getCurrentFileName(), goToNextFile(), QString::isEmpty(), QString::length(), MAX_FILE_NAME_LENGTH, mdUnzip, QString::toLower(), QuaZipPrivate::unzFile_f, and QHash::value().
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.
In both cases getZipError() returns UNZ_OK
since there is no ZIP/UNZIP API call.
This overload doesn't support zip64, but will work OK on zip64 archives except that if one of the sizes (compressed or uncompressed) is greater than 0xFFFFFFFFu, it will be set to exactly 0xFFFFFFFFu.
References QuaZipFileInfo64::toQuaZipFileInfo().
Referenced by QuaZipFile::getFileInfo().
bool QuaZip::getCurrentFileInfo | ( | QuaZipFileInfo64 * | info | ) | const |
Retrieves information about the current file.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This function supports zip64. If the archive doesn't use zip64, it is completely equivalent to getCurrentFileInfo(QuaZipFileInfo* info) except for the argument type.
References QuaZipFileInfo64::comment, QuaZipFileInfo64::compressedSize, QuaZipFileInfo64::crc, QByteArray::data(), QuaZipFileInfo64::dateTime, QuaZipFileInfo64::diskNumberStart, QuaZipFileInfo64::externalAttr, QuaZipFileInfo64::extra, QuaZipFileInfo64::flags, hasCurrentFile(), QuaZipFileInfo64::internalAttr, isOpen(), mdUnzip, QuaZipFileInfo64::method, QuaZipFileInfo64::name, QByteArray::resize(), QByteArray::size(), QTextCodec::toUnicode(), QuaZipFileInfo64::uncompressedSize, QuaZipPrivate::unzFile_f, QuaZipFileInfo64::versionCreated, and QuaZipFileInfo64::versionNeeded.
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 QByteArray::constData(), QByteArray::data(), hasCurrentFile(), QString::isEmpty(), isOpen(), MAX_FILE_NAME_LENGTH, mdUnzip, QByteArray::size(), QTextCodec::toUnicode(), and QuaZipPrivate::unzFile_f.
Referenced by 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).
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.
Note that if the ZIP archive is written into a QIODevice for which QIODevice::isSequential() returns true
, then the data descriptor is mandatory and will be written even if this flag is set to false.
enabled | If true , enable local descriptor writing, disable it otherwise. |
bool QuaZip::isDataDescriptorWritingEnabled | ( | ) | const |
Returns the data descriptor default writing mode.
Referenced by QuaZipFile::open().
QStringList QuaZip::getFileNameList | ( | ) | const |
Returns a list of files inside the archive.
QList< QuaZipFileInfo > QuaZip::getFileInfoList | ( | ) | const |
Returns information list about all files inside the archive.
This function doesn't support zip64, but will still work with zip64 archives, converting results using QuaZipFileInfo64::toQuaZipFileInfo(). If all file sizes are below 4 GB, it will work just fine.
QList< QuaZipFileInfo64 > QuaZip::getFileInfoList64 | ( | ) | const |
Returns information list about all files inside the archive.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This function supports zip64.
void QuaZip::setZip64Enabled | ( | bool | zip64 | ) |
Enables the zip64 mode.
zip64 | If true , the zip64 mode is enabled, disabled otherwise. |
Once this is enabled, all new files (until the mode is disabled again) will be created in the zip64 mode, thus enabling the ability to write files larger than 4 GB. By default, the zip64 mode is off due to compatibility reasons.
Note that this does not affect the ability to read zip64 archives in any way.
bool QuaZip::isZip64Enabled | ( | ) | const |
Returns whether the zip64 mode is enabled.
true
if and only if the zip64 mode is enabled.Referenced by QuaZipFile::open().
bool QuaZip::isAutoClose | ( | ) | const |
Returns the auto-close flag.
void QuaZip::setAutoClose | ( | bool | autoClose | ) | const |
Sets or unsets the auto-close flag.
By default, QuaZIP opens the underlying QIODevice when open() is called, and closes it when close() is called. In some cases, when the device is set explicitly using setIoDevice(), it may be desirable to leave the device open. If the auto-close flag is unset using this method, then the device isn't closed automatically if it was set explicitly.
If it is needed to clear this flag, it is recommended to do so before opening the archive because otherwise QuaZIP may close the device during the open() call if an error is encountered after the device is opened.
If the device was not set explicitly, but rather the setZipName() or the appropriate constructor was used to set the ZIP file name instead, then the auto-close flag has no effect, and the internal device is closed nevertheless because there is no other way to close it.
void QuaZip::setDefaultFileNameCodec | ( | QTextCodec * | codec | ) | [static] |
Sets the default file name codec to use.
The default codec is used by the constructors, so calling this function won't affect the QuaZip instances already created at that moment.
The codec specified here can be overriden by calling setFileNameCodec(). If neither function is called, QTextCodec::codecForLocale() will be used to decode or encode file names. Use this function with caution if the application uses other libraries that depend on QuaZIP. Those libraries can either call this function by themselves, thus overriding your setting or can rely on the default encoding, thus failing mysteriously if you change it. For these reasons, it isn't recommended to use this function if you are developing a library, not an application. Instead, ask your library users to call it in case they need specific encoding.
In most cases, using setFileNameCodec() instead is the right choice. However, if you depend on third-party code that uses QuaZIP, then the reasons stated above can actually become a reason to use this function in case the third-party code in question fails because it doesn't understand the encoding you need and doesn't provide a way to specify it. This applies to the JlCompress class as well, as it was contributed and doesn't support explicit encoding parameters.
In short: use setFileNameCodec() when you can, resort to setDefaultFileNameCodec() when you don't have access to the QuaZip instance.
codec | The codec to use by default. If NULL, resets to default. |
Referenced by setDefaultFileNameCodec().
void QuaZip::setDefaultFileNameCodec | ( | const char * | codecName | ) | [static] |
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 setDefltFileNameCodec(QTextCodec::codecForName(codecName)).
References QTextCodec::codecForName(), and setDefaultFileNameCodec().