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.
33 #include "quazipfile.h"
37 QuaZipFile::QuaZipFile():
38 zip(NULL
), internal(true), zipError(UNZ_OK
)
42 QuaZipFile::QuaZipFile(QObject
*parent
):
43 QIODevice(parent
), zip(NULL
), internal(true), zipError(UNZ_OK
)
47 QuaZipFile::QuaZipFile(const QString
& zipName
, QObject
*parent
):
48 QIODevice(parent
), internal(true), zipError(UNZ_OK
)
50 zip
=new QuaZip(zipName
);
54 QuaZipFile::QuaZipFile(const QString
& zipName
, const QString
& fileName
,
55 QuaZip::CaseSensitivity cs
, QObject
*parent
):
56 QIODevice(parent
), internal(true), zipError(UNZ_OK
)
58 zip
=new QuaZip(zipName
);
60 this->fileName
=fileName
;
61 this->caseSensitivity
=cs
;
64 QuaZipFile::QuaZipFile(QuaZip
*zip
, QObject
*parent
):
66 zip(zip
), internal(false),
71 QuaZipFile::~QuaZipFile()
74 if(internal
) delete zip
;
77 QString
QuaZipFile::getZipName()const
79 return zip
==NULL
?QString():zip
->getZipName();
82 QString
QuaZipFile::getActualFileName()const
85 if(zip
==NULL
||(openMode()&WriteOnly
)) return QString();
86 QString name
=zip
->getCurrentFileName();
88 setZipError(zip
->getZipError());
92 void QuaZipFile::setZipName(const QString
& zipName
)
95 qWarning("QuaZipFile::setZipName(): file is already open - can not set ZIP name");
98 if(zip
!=NULL
&&internal
) delete zip
;
99 zip
=new QuaZip(zipName
);
104 void QuaZipFile::setZip(QuaZip
*zip
)
107 qWarning("QuaZipFile::setZip(): file is already open - can not set ZIP");
110 if(this->zip
!=NULL
&&internal
) delete this->zip
;
112 this->fileName
=QString();
116 void QuaZipFile::setFileName(const QString
& fileName
, QuaZip::CaseSensitivity cs
)
119 qWarning("QuaZipFile::setFileName(): call setZipName() first");
123 qWarning("QuaZipFile::setFileName(): should not be used when not using internal QuaZip");
127 qWarning("QuaZipFile::setFileName(): can not set file name for already opened file");
130 this->fileName
=fileName
;
131 this->caseSensitivity
=cs
;
134 void QuaZipFile::setZipError(int zipError
)const
136 QuaZipFile
*fakeThis
=(QuaZipFile
*)this; // non-const
137 fakeThis
->zipError
=zipError
;
139 fakeThis
->setErrorString(QString());
141 fakeThis
->setErrorString(tr("ZIP/UNZIP API error %1").arg(zipError
));
144 bool QuaZipFile::open(OpenMode mode
)
146 return open(mode
, NULL
);
149 bool QuaZipFile::open(OpenMode mode
, int *method
, int *level
, bool raw
, const char *password
)
153 qWarning("QuaZipFile::open(): already opened");
156 if(mode
&Unbuffered
) {
157 qWarning("QuaZipFile::open(): Unbuffered mode is not supported");
160 if((mode
&ReadOnly
)&&!(mode
&WriteOnly
)) {
162 if(!zip
->open(QuaZip::mdUnzip
)) {
163 setZipError(zip
->getZipError());
166 if(!zip
->setCurrentFile(fileName
, caseSensitivity
)) {
167 setZipError(zip
->getZipError());
173 qWarning("QuaZipFile::open(): zip is NULL");
176 if(zip
->getMode()!=QuaZip::mdUnzip
) {
177 qWarning("QuaZipFile::open(): file open mode %d incompatible with ZIP open mode %d",
178 (int)mode
, (int)zip
->getMode());
181 if(!zip
->hasCurrentFile()) {
182 qWarning("QuaZipFile::open(): zip does not have current file");
186 setZipError(unzOpenCurrentFile3(zip
->getUnzFile(), method
, level
, (int)raw
, password
));
187 if(zipError
==UNZ_OK
) {
194 qWarning("QuaZipFile::open(): open mode %d not supported by this function", (int)mode
);
198 bool QuaZipFile::open(OpenMode mode
, const QuaZipNewInfo
& info
,
199 const char *password
, quint32 crc
,
200 int method
, int level
, bool raw
,
201 int windowBits
, int memLevel
, int strategy
)
206 qWarning("QuaZipFile::open(): already opened");
209 if((mode
&WriteOnly
)&&!(mode
&ReadOnly
)) {
211 qWarning("QuaZipFile::open(): write mode is incompatible with internal QuaZip approach");
215 qWarning("QuaZipFile::open(): zip is NULL");
218 if(zip
->getMode()!=QuaZip::mdCreate
&&zip
->getMode()!=QuaZip::mdAppend
&&zip
->getMode()!=QuaZip::mdAdd
) {
219 qWarning("QuaZipFile::open(): file open mode %d incompatible with ZIP open mode %d",
220 (int)mode
, (int)zip
->getMode());
223 info_z
.tmz_date
.tm_year
=info
.dateTime
.date().year();
224 info_z
.tmz_date
.tm_mon
=info
.dateTime
.date().month() - 1;
225 info_z
.tmz_date
.tm_mday
=info
.dateTime
.date().day();
226 info_z
.tmz_date
.tm_hour
=info
.dateTime
.time().hour();
227 info_z
.tmz_date
.tm_min
=info
.dateTime
.time().minute();
228 info_z
.tmz_date
.tm_sec
=info
.dateTime
.time().second();
230 info_z
.internal_fa
=(uLong
)info
.internalAttr
;
231 info_z
.external_fa
=(uLong
)info
.externalAttr
;
232 setZipError(zipOpenNewFileInZip3(zip
->getZipFile(),
233 zip
->getFileNameCodec()->fromUnicode(info
.name
).constData(), &info_z
,
234 info
.extraLocal
.constData(), info
.extraLocal
.length(),
235 info
.extraGlobal
.constData(), info
.extraGlobal
.length(),
236 zip
->getCommentCodec()->fromUnicode(info
.comment
).constData(),
237 method
, level
, (int)raw
,
238 windowBits
, memLevel
, strategy
,
239 password
, (uLong
)crc
));
240 if(zipError
==UNZ_OK
) {
246 this->uncompressedSize
=info
.uncompressedSize
;
252 qWarning("QuaZipFile::open(): open mode %d not supported by this function", (int)mode
);
256 bool QuaZipFile::isSequential()const
261 qint64
QuaZipFile::pos()const
264 qWarning("QuaZipFile::pos(): call setZipName() or setZip() first");
268 qWarning("QuaZipFile::pos(): file is not open");
271 if(openMode()&ReadOnly
)
272 return unztell(zip
->getUnzFile());
277 bool QuaZipFile::atEnd()const
280 qWarning("QuaZipFile::atEnd(): call setZipName() or setZip() first");
284 qWarning("QuaZipFile::atEnd(): file is not open");
287 if(openMode()&ReadOnly
)
288 return unzeof(zip
->getUnzFile())==1;
293 qint64
QuaZipFile::size()const
296 qWarning("QuaZipFile::atEnd(): file is not open");
299 if(openMode()&ReadOnly
)
300 return raw
?csize():usize();
305 qint64
QuaZipFile::csize()const
307 unz_file_info info_z
;
309 if(zip
==NULL
||zip
->getMode()!=QuaZip::mdUnzip
) return -1;
310 setZipError(unzGetCurrentFileInfo(zip
->getUnzFile(), &info_z
, NULL
, 0, NULL
, 0, NULL
, 0));
313 return info_z
.compressed_size
;
316 qint64
QuaZipFile::usize()const
318 unz_file_info info_z
;
320 if(zip
==NULL
||zip
->getMode()!=QuaZip::mdUnzip
) return -1;
321 setZipError(unzGetCurrentFileInfo(zip
->getUnzFile(), &info_z
, NULL
, 0, NULL
, 0, NULL
, 0));
324 return info_z
.uncompressed_size
;
327 bool QuaZipFile::getFileInfo(QuaZipFileInfo
*info
)
329 if(zip
==NULL
||zip
->getMode()!=QuaZip::mdUnzip
) return false;
330 zip
->getCurrentFileInfo(info
);
331 setZipError(zip
->getZipError());
332 return zipError
==UNZ_OK
;
335 void QuaZipFile::close()
338 if(zip
==NULL
||!zip
->isOpen()) return;
340 qWarning("QuaZipFile::close(): file isn't open");
343 if(openMode()&ReadOnly
)
344 setZipError(unzCloseCurrentFile(zip
->getUnzFile()));
345 else if(openMode()&WriteOnly
)
346 if(isRaw()) setZipError(zipCloseFileInZipRaw(zip
->getZipFile(), uncompressedSize
, crc
));
347 else setZipError(zipCloseFileInZip(zip
->getZipFile()));
349 qWarning("Wrong open mode: %d", (int)openMode());
352 if(zipError
==UNZ_OK
) setOpenMode(QIODevice::NotOpen
);
356 setZipError(zip
->getZipError());
360 qint64
QuaZipFile::readData(char *data
, qint64 maxSize
)
363 qint64 bytesRead
=unzReadCurrentFile(zip
->getUnzFile(), data
, (unsigned)maxSize
);
364 if(bytesRead
<0) setZipError((int)bytesRead
);
368 qint64
QuaZipFile::writeData(const char* data
, qint64 maxSize
)
371 setZipError(zipWriteInFileInZip(zip
->getZipFile(), data
, (uint
)maxSize
));
372 if(zipError
!=ZIP_OK
) return -1;