2 -- A kind of "standard" GPL license statement --
3 QuaZIP - a Qt/C++ wrapper for the ZIP/UNZIP package
4 Copyright (C) 2005-2007 Sergey A. Tachenov
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 -- A kind of "standard" GPL license statement ends here --
22 See COPYING file for GPL.
24 You are also permitted to use QuaZIP under the terms of LGPL (see
25 COPYING.LGPL). You are free to choose either license, but please note
26 that QuaZIP makes use of Qt, which is not licensed under LGPL. So if
27 you are using Open Source edition of Qt, you therefore MUST use GPL for
28 your code based on QuaZIP, since it would be also based on Qt in this
29 case. If you are Qt commercial license owner, then you are free to use
30 QuaZIP as long as you respect either GPL or LGPL for QuaZIP code.
38 fileNameCodec(QTextCodec::codecForLocale()),
39 commentCodec(QTextCodec::codecForLocale()),
40 mode(mdNotOpen
), hasCurrentFile_f(false), zipError(UNZ_OK
)
44 QuaZip::QuaZip(const QString
& zipName
):
45 fileNameCodec(QTextCodec::codecForLocale()),
46 commentCodec(QTextCodec::codecForLocale()),
48 mode(mdNotOpen
), hasCurrentFile_f(false), zipError(UNZ_OK
)
57 bool QuaZip::open(Mode mode
, zlib_filefunc_def
* ioApi
)
61 qWarning("QuaZip::open(): ZIP already opened");
66 unzFile_f
=unzOpen2(QFile::encodeName(zipName
).constData(), ioApi
);
71 zipError
=UNZ_OPENERROR
;
77 zipFile_f
=zipOpen2(QFile::encodeName(zipName
).constData(),
78 mode
==mdCreate
?APPEND_STATUS_CREATE
:
79 mode
==mdAppend
?APPEND_STATUS_CREATEAFTER
:
80 APPEND_STATUS_ADDINZIP
,
87 zipError
=UNZ_OPENERROR
;
91 qWarning("QuaZip::open(): unknown mode: %d", (int)mode
);
102 qWarning("QuaZip::close(): ZIP is not open");
105 zipError
=unzClose(unzFile_f
);
110 zipError
=zipClose(zipFile_f
, commentCodec
->fromUnicode(comment
).constData());
113 qWarning("QuaZip::close(): unknown mode: %d", (int)mode
);
116 if(zipError
==UNZ_OK
) mode
=mdNotOpen
;
119 void QuaZip::setZipName(const QString
& zipName
)
122 qWarning("QuaZip::setZipName(): ZIP is already open!");
125 this->zipName
=zipName
;
128 int QuaZip::getEntriesCount()const
130 QuaZip
*fakeThis
=(QuaZip
*)this; // non-const
131 fakeThis
->zipError
=UNZ_OK
;
133 qWarning("QuaZip::getEntriesCount(): ZIP is not open in mdUnzip mode");
136 unz_global_info globalInfo
;
137 if((fakeThis
->zipError
=unzGetGlobalInfo(unzFile_f
, &globalInfo
))!=UNZ_OK
)
139 return (int)globalInfo
.number_entry
;
142 QString
QuaZip::getComment()const
144 QuaZip
*fakeThis
=(QuaZip
*)this; // non-const
145 fakeThis
->zipError
=UNZ_OK
;
147 qWarning("QuaZip::getComment(): ZIP is not open in mdUnzip mode");
150 unz_global_info globalInfo
;
152 if((fakeThis
->zipError
=unzGetGlobalInfo(unzFile_f
, &globalInfo
))!=UNZ_OK
)
154 comment
.resize(globalInfo
.size_comment
);
155 if((fakeThis
->zipError
=unzGetGlobalComment(unzFile_f
, comment
.data(), comment
.size()))!=UNZ_OK
)
157 return commentCodec
->toUnicode(comment
);
160 bool QuaZip::setCurrentFile(const QString
& fileName
, CaseSensitivity cs
)
164 qWarning("QuaZip::setCurrentFile(): ZIP is not open in mdUnzip mode");
167 if(fileName
.isNull()) {
168 hasCurrentFile_f
=false;
171 // Unicode-aware reimplementation of the unzLocateFile function
172 if(unzFile_f
==NULL
) {
173 zipError
=UNZ_PARAMERROR
;
176 if(fileName
.length()>MAX_FILE_NAME_LENGTH
) {
177 zipError
=UNZ_PARAMERROR
;
187 } else sens
=cs
==csSensitive
;
188 QString lower
, current
;
189 if(!sens
) lower
=fileName
.toLower();
190 hasCurrentFile_f
=false;
191 for(bool more
=goToFirstFile(); more
; more
=goToNextFile()) {
192 current
=getCurrentFileName();
193 if(current
.isNull()) return false;
195 if(current
==fileName
) break;
197 if(current
.toLower()==lower
) break;
200 return hasCurrentFile_f
;
203 bool QuaZip::goToFirstFile()
207 qWarning("QuaZip::goToFirstFile(): ZIP is not open in mdUnzip mode");
210 zipError
=unzGoToFirstFile(unzFile_f
);
211 hasCurrentFile_f
=zipError
==UNZ_OK
;
212 return hasCurrentFile_f
;
215 bool QuaZip::goToNextFile()
219 qWarning("QuaZip::goToFirstFile(): ZIP is not open in mdUnzip mode");
222 zipError
=unzGoToNextFile(unzFile_f
);
223 hasCurrentFile_f
=zipError
==UNZ_OK
;
224 if(zipError
==UNZ_END_OF_LIST_OF_FILE
) zipError
=UNZ_OK
;
225 return hasCurrentFile_f
;
228 bool QuaZip::getCurrentFileInfo(QuaZipFileInfo
*info
)const
230 QuaZip
*fakeThis
=(QuaZip
*)this; // non-const
231 fakeThis
->zipError
=UNZ_OK
;
233 qWarning("QuaZip::getCurrentFileInfo(): ZIP is not open in mdUnzip mode");
236 unz_file_info info_z
;
240 if(info
==NULL
) return false;
241 if(!isOpen()||!hasCurrentFile()) return false;
242 if((fakeThis
->zipError
=unzGetCurrentFileInfo(unzFile_f
, &info_z
, NULL
, 0, NULL
, 0, NULL
, 0))!=UNZ_OK
)
244 fileName
.resize(info_z
.size_filename
);
245 extra
.resize(info_z
.size_file_extra
);
246 comment
.resize(info_z
.size_file_comment
);
247 if((fakeThis
->zipError
=unzGetCurrentFileInfo(unzFile_f
, NULL
,
248 fileName
.data(), fileName
.size(),
249 extra
.data(), extra
.size(),
250 comment
.data(), comment
.size()))!=UNZ_OK
)
252 info
->versionCreated
=info_z
.version
;
253 info
->versionNeeded
=info_z
.version_needed
;
254 info
->flags
=info_z
.flag
;
255 info
->method
=info_z
.compression_method
;
256 info
->crc
=info_z
.crc
;
257 info
->compressedSize
=info_z
.compressed_size
;
258 info
->uncompressedSize
=info_z
.uncompressed_size
;
259 info
->diskNumberStart
=info_z
.disk_num_start
;
260 info
->internalAttr
=info_z
.internal_fa
;
261 info
->externalAttr
=info_z
.external_fa
;
262 info
->name
=fileNameCodec
->toUnicode(fileName
);
263 info
->comment
=commentCodec
->toUnicode(comment
);
265 info
->dateTime
=QDateTime(
266 QDate(info_z
.tmu_date
.tm_year
, info_z
.tmu_date
.tm_mon
+1, info_z
.tmu_date
.tm_mday
),
267 QTime(info_z
.tmu_date
.tm_hour
, info_z
.tmu_date
.tm_min
, info_z
.tmu_date
.tm_sec
));
271 QString
QuaZip::getCurrentFileName()const
273 QuaZip
*fakeThis
=(QuaZip
*)this; // non-const
274 fakeThis
->zipError
=UNZ_OK
;
276 qWarning("QuaZip::getCurrentFileName(): ZIP is not open in mdUnzip mode");
279 if(!isOpen()||!hasCurrentFile()) return QString();
280 QByteArray
fileName(MAX_FILE_NAME_LENGTH
, 0);
281 if((fakeThis
->zipError
=unzGetCurrentFileInfo(unzFile_f
, NULL
, fileName
.data(), fileName
.size(),
282 NULL
, 0, NULL
, 0))!=UNZ_OK
)
284 return fileNameCodec
->toUnicode(fileName
.constData());