43 Q_DISABLE_COPY(QuaZipPrivate)
65 bool hasCurrentFile_f;
70 bool dataDescriptorWritingEnabled;
76 if (defaultFileNameCodec ==
NULL) {
79 return defaultFileNameCodec;
83 inline QuaZipPrivate(
QuaZip *q)
85 fileNameCodec(getDefaultFileNameCodec()),
89 hasCurrentFile_f(false),
91 dataDescriptorWritingEnabled(true),
96 lastMappedDirectoryEntry.num_of_file = 0;
97 lastMappedDirectoryEntry.pos_in_zip_directory = 0;
100 inline QuaZipPrivate(
QuaZip *q,
const QString &zipName)
102 fileNameCodec(getDefaultFileNameCodec()),
107 hasCurrentFile_f(false),
109 dataDescriptorWritingEnabled(true),
114 lastMappedDirectoryEntry.num_of_file = 0;
115 lastMappedDirectoryEntry.pos_in_zip_directory = 0;
118 inline QuaZipPrivate(
QuaZip *q, QIODevice *ioDevice)
120 fileNameCodec(getDefaultFileNameCodec()),
124 hasCurrentFile_f(false),
126 dataDescriptorWritingEnabled(true),
131 lastMappedDirectoryEntry.num_of_file = 0;
132 lastMappedDirectoryEntry.pos_in_zip_directory = 0;
135 template <
typename TFileInfo>
136 bool getFileInfoList(QList<TFileInfo> *
result)
const;
139 inline void clearDirectoryMap();
140 inline void addCurrentFileToDirectoryMap(
const QString &fileName);
141 bool goToFirstUnmappedFile();
142 QHash<QString, unz64_file_pos> directoryCaseSensitive;
143 QHash<QString, unz64_file_pos> directoryCaseInsensitive;
150 void QuaZipPrivate::clearDirectoryMap() {
151 directoryCaseInsensitive.clear();
152 directoryCaseSensitive.clear();
153 lastMappedDirectoryEntry.num_of_file = 0;
154 lastMappedDirectoryEntry.pos_in_zip_directory = 0;
157 void QuaZipPrivate::addCurrentFileToDirectoryMap(
const QString &fileName) {
158 if (!hasCurrentFile_f || fileName.isEmpty()) {
164 directoryCaseSensitive.insert(fileName, fileDirectoryPos);
167 QString lower = fileName.toLower();
168 if (!directoryCaseInsensitive.contains(lower))
169 directoryCaseInsensitive.insert(lower, fileDirectoryPos);
172 lastMappedDirectoryEntry.pos_in_zip_directory)
173 lastMappedDirectoryEntry = fileDirectoryPos;
176 bool QuaZipPrivate::goToFirstUnmappedFile() {
180 "QuaZipPrivate::goToNextUnmappedFile(): ZIP is not open in "
185 if (lastMappedDirectoryEntry.pos_in_zip_directory == 0) {
192 hasCurrentFile_f = zipError ==
UNZ_OK;
194 return hasCurrentFile_f;
211 qWarning(
"QuaZip::open(): ZIP already opened");
214 QIODevice *ioDevice = p->ioDevice;
215 if (ioDevice ==
NULL) {
216 if (p->zipName.isEmpty()) {
218 "QuaZip::open(): set either ZIP file name or IO device "
222 ioDevice =
new QFile(p->zipName);
230 p->unzFile_f = unzOpenInternal(ioDevice,
NULL, 1, flags);
233 p->unzFile_f =
unzOpen2(ioDevice, ioApi);
234 if (p->unzFile_f !=
NULL) {
242 if (p->unzFile_f !=
NULL) {
243 if (ioDevice->isSequential()) {
245 if (!p->zipName.isEmpty())
delete ioDevice;
248 "only mdCreate can be used with "
249 "sequential devices");
253 p->ioDevice = ioDevice;
257 if (!p->zipName.isEmpty())
delete ioDevice;
265 if (p->dataDescriptorWritingEnabled)
281 if (p->zipFile_f !=
NULL) {
285 if (p->zipFile_f !=
NULL) {
286 if (ioDevice->isSequential()) {
291 "only mdCreate can be used with "
292 "sequential devices");
293 if (!p->zipName.isEmpty())
delete ioDevice;
299 p->ioDevice = ioDevice;
303 if (!p->zipName.isEmpty())
delete ioDevice;
307 qWarning(
"QuaZip::open(): unknown mode: %d", (
int)mode);
308 if (!p->zipName.isEmpty())
delete ioDevice;
318 qWarning(
"QuaZip::close(): ZIP is not open");
321 p->zipError =
unzClose(p->unzFile_f);
330 : p->commentCodec->fromUnicode(p->comment)
334 qWarning(
"QuaZip::close(): unknown mode: %d", (
int)p->mode);
338 if (!p->zipName.isEmpty()) {
342 p->clearDirectoryMap();
348 qWarning(
"QuaZip::setZipName(): ZIP is already open!");
351 p->zipName = zipName;
357 qWarning(
"QuaZip::setIoDevice(): ZIP is already open!");
360 p->ioDevice = ioDevice;
361 p->zipName = QString();
366 fakeThis->p->zipError =
UNZ_OK;
368 qWarning(
"QuaZip::getEntriesCount(): ZIP is not open in mdUnzip mode");
372 if ((fakeThis->p->zipError =
380 fakeThis->p->zipError =
UNZ_OK;
382 qWarning(
"QuaZip::getComment(): ZIP is not open in mdUnzip mode");
387 if ((fakeThis->p->zipError =
392 p->unzFile_f, comment.data(), comment.size())) < 0)
394 fakeThis->p->zipError =
UNZ_OK;
395 return p->commentCodec->toUnicode(comment);
401 qWarning(
"QuaZip::setCurrentFile(): ZIP is not open in mdUnzip mode");
404 if (fileName.isEmpty()) {
405 p->hasCurrentFile_f =
false;
409 if (p->unzFile_f ==
NULL) {
419 QString lower, current;
420 if (!sens) lower = fileName.toLower();
421 p->hasCurrentFile_f =
false;
427 if (p->directoryCaseSensitive.contains(fileName))
428 fileDirPos = p->directoryCaseSensitive.value(fileName);
430 if (p->directoryCaseInsensitive.contains(lower))
431 fileDirPos = p->directoryCaseInsensitive.value(lower);
436 p->hasCurrentFile_f = p->zipError ==
UNZ_OK;
439 if (p->hasCurrentFile_f)
return p->hasCurrentFile_f;
442 for (
bool more = p->goToFirstUnmappedFile(); more; more =
goToNextFile()) {
444 if (current.isEmpty())
return false;
446 if (current == fileName)
break;
448 if (current.toLower() == lower)
break;
451 return p->hasCurrentFile_f;
457 qWarning(
"QuaZip::goToFirstFile(): ZIP is not open in mdUnzip mode");
461 p->hasCurrentFile_f = p->zipError ==
UNZ_OK;
462 return p->hasCurrentFile_f;
468 qWarning(
"QuaZip::goToFirstFile(): ZIP is not open in mdUnzip mode");
472 p->hasCurrentFile_f = p->zipError ==
UNZ_OK;
474 return p->hasCurrentFile_f;
492 fakeThis->p->zipError =
UNZ_OK;
495 "QuaZip::getCurrentFileInfo(): ZIP is not open in mdUnzip "
503 if (info ==
NULL)
return false;
512 p->unzFile_f,
NULL, fileName.data(), fileName.size(),
513 extra.data(), extra.size(), comment.data(), comment.size())) !=
526 info->
name = p->fileNameCodec->toUnicode(fileName);
527 info->
comment = p->commentCodec->toUnicode(comment);
535 p->addCurrentFileToDirectoryMap(info->
name);
541 fakeThis->p->zipError =
UNZ_OK;
544 "QuaZip::getCurrentFileName(): ZIP is not open in mdUnzip "
551 p->unzFile_f,
NULL, fileName.data(), fileName.size(),
NULL, 0,
554 QString
result = p->fileNameCodec->toUnicode(fileName.constData());
557 p->addCurrentFileToDirectoryMap(
result);
562 p->fileNameCodec = fileNameCodec;
572 p->commentCodec = commentCodec;
584 if (!p->zipName.isEmpty())
604 p->dataDescriptorWritingEnabled = enabled;
608 return p->dataDescriptorWritingEnabled;
611 template <
typename TFileInfo>
631 *ok = !
name.isEmpty();
635 template <
typename TFileInfo>
636 bool QuaZipPrivate::getFileInfoList(QList<TFileInfo> *
result)
const {
637 QuaZipPrivate *fakeThis =
const_cast<QuaZipPrivate *
>(
this);
638 fakeThis->zipError =
UNZ_OK;
641 "QuaZip::getFileNameList/getFileInfoList(): "
642 "ZIP is not open in mdUnzip mode");
652 result->append(QuaZip_getFileInfo<TFileInfo>(q, &ok));
653 if (!ok)
return false;
656 if (zipError !=
UNZ_OK)
return false;
657 if (currentFile.isEmpty()) {
667 if (p->getFileInfoList(&list))
670 return QStringList();
674 QList<QuaZipFileInfo> list;
675 if (p->getFileInfoList(&list))
678 return QList<QuaZipFileInfo>();
682 QList<QuaZipFileInfo64> list;
683 if (p->getFileInfoList(&list))
686 return QList<QuaZipFileInfo64>();
694 return Qt::CaseSensitive;
702 QuaZipPrivate::defaultFileNameCodec = codec;
static QTextCodec * codecForName(const char *name)
static QTextCodec * codecForLocale()
QString getZipName() const
Returns the name of the ZIP file.
int getEntriesCount() const
Returns number of the entries in the ZIP central directory.
void setComment(const QString &comment)
Sets the global comment in the ZIP file.
void setCommentCodec(QTextCodec *commentCodec)
Sets the codec used to encode/decode comments inside archive.
static Qt::CaseSensitivity convertCaseSensitivity(CaseSensitivity cs)
Returns the actual case sensitivity for the specified QuaZIP one.
bool isAutoClose() const
Returns the auto-close flag.
bool isDataDescriptorWritingEnabled() const
Returns the data descriptor default writing mode.
QStringList getFileNameList() const
Returns a list of files inside the archive.
static void setDefaultFileNameCodec(QTextCodec *codec)
Sets the default file name codec to use.
void setFileNameCodec(QTextCodec *fileNameCodec)
Sets the codec used to encode/decode file names inside archive.
QTextCodec * getCommentCodec() const
Returns the codec used to encode/decode comments inside archive.
unzFile getUnzFile()
Returns unzFile handle.
zipFile getZipFile()
Returns zipFile handle.
void setAutoClose(bool autoClose) const
Sets or unsets the auto-close flag.
Mode
Open mode of the ZIP file.
@ mdAdd
ZIP file was opened for adding files in the archive.
@ mdCreate
ZIP file was created with open() call.
@ mdUnzip
ZIP file is open for reading files inside it.
@ mdNotOpen
ZIP file is not open. This is the initial mode.
QString getComment() const
Returns global comment in the ZIP file.
Mode getMode() const
Returns the mode in which ZIP file was opened.
CaseSensitivity
Case sensitivity for the file names.
@ csSensitive
Case sensitive.
void setIoDevice(QIODevice *ioDevice)
Sets the device representing the ZIP file.
void setDataDescriptorWritingEnabled(bool enabled)
Changes the data descriptor writing mode.
bool setCurrentFile(const QString &fileName, CaseSensitivity cs=csDefault)
Sets current file by its name.
bool goToFirstFile()
Sets the current file to the first file in the archive.
void close()
Closes ZIP file.
QList< QuaZipFileInfo > getFileInfoList() const
Returns information list about all files inside the archive.
QIODevice * getIoDevice() const
Returns the device representing this ZIP file.
QuaZip()
Constructs QuaZip object.
QString getCurrentFileName() const
Returns the current file name.
bool isZip64Enabled() const
Returns whether the zip64 mode is enabled.
void setZipName(const QString &zipName)
Sets the name of the ZIP file.
QTextCodec * getFileNameCodec() const
Returns the codec used to encode/decode comments inside archive.
int getZipError() const
Returns the error code of the last operation.
void setZip64Enabled(bool zip64)
Enables the zip64 mode.
bool open(Mode mode, zlib_filefunc_def *ioApi=NULL)
Opens ZIP file.
QList< QuaZipFileInfo64 > getFileInfoList64() const
Returns information list about all files inside the archive.
bool getCurrentFileInfo(QuaZipFileInfo *info) const
Retrieves information about the current file.
bool isOpen() const
Returns true if ZIP file is open, false otherwise.
bool goToNextFile()
Sets the current file to the next file in the archive.
bool hasCurrentFile() const
Returns true if the current file has been set.
~QuaZip()
Destroys QuaZip object.
constexpr QRegularExpression::PatternOption CaseInsensitive
TFileInfo QuaZip_getFileInfo(QuaZip *zip, bool *ok)
Information about a file inside archive (with zip64 support).
quint16 versionNeeded
Version needed to extract.
quint32 externalAttr
External file attributes.
quint16 method
Compression method.
QDateTime dateTime
Last modification date and time.
quint64 uncompressedSize
Uncompressed file size.
quint16 flags
General purpose flags.
quint16 versionCreated
Version created by.
bool toQuaZipFileInfo(QuaZipFileInfo &info) const
Converts to QuaZipFileInfo.
quint16 diskNumberStart
Disk number start.
QByteArray extra
Extra field.
quint64 compressedSize
Compressed file size.
quint16 internalAttr
Internal file attributes.
Information about a file inside archive.
ZPOS64_T pos_in_zip_directory
ZPOS64_T uncompressed_size
unzFile ZEXPORT unzOpen2(voidpf file, zlib_filefunc_def *pzlib_filefunc32_def)
int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos)
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
int ZEXPORT unzGoToFirstFile(unzFile file)
int ZEXPORT unzClearFlags(unzFile file, unsigned flags)
int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos)
int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
int ZEXPORT unzSetFlags(unzFile file, unsigned flags)
int ZEXPORT unzGoToNextFile(unzFile file)
int ZEXPORT unzGetGlobalComment(unzFile file, char *szComment, uLong uSizeBuf)
int ZEXPORT unzClose(unzFile file)
#define UNZ_END_OF_LIST_OF_FILE
int ZEXPORT zipClose(zipFile file, const char *global_comment)
zipFile ZEXPORT zipOpen2(voidpf file, int append, zipcharpc *globalcomment, zlib_filefunc_def *pzlib_filefunc32_def)
int ZEXPORT zipSetFlags(zipFile file, unsigned flags)
zipFile ZEXPORT zipOpen3(voidpf file, int append, zipcharpc *globalcomment, zlib_filefunc64_32_def *pzlib_filefunc64_32_def, unsigned flags)
#define APPEND_STATUS_ADDINZIP
#define ZIP_WRITE_DATA_DESCRIPTOR
#define APPEND_STATUS_CREATEAFTER
#define APPEND_STATUS_CREATE