2011-03-25 22:16:12 +00:00
|
|
|
/*
|
2015-03-08 18:07:42 +00:00
|
|
|
Copyright (C) 2005-2014 Sergey A. Tachenov
|
|
|
|
|
|
|
|
This file is part of QuaZIP.
|
|
|
|
|
|
|
|
QuaZIP is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
QuaZIP is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with QuaZIP. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
See COPYING file for the full LGPL text.
|
|
|
|
|
|
|
|
Original ZIP package is copyrighted by Gilles Vollant, see
|
|
|
|
quazip/(un)zip.h files for details, basically it's zlib license.
|
2011-03-25 22:16:12 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "quazipfile.h"
|
|
|
|
|
2020-06-08 18:25:36 +00:00
|
|
|
#include "quazipfileinfo.h"
|
|
|
|
|
2011-03-25 22:16:12 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2020-06-08 18:25:36 +00:00
|
|
|
#define QUAZIP_VERSION_MADE_BY 0x1Eu
|
|
|
|
|
2015-03-08 18:07:42 +00:00
|
|
|
/// The implementation class for QuaZip.
|
|
|
|
/**
|
|
|
|
\internal
|
|
|
|
|
|
|
|
This class contains all the private stuff for the QuaZipFile class, thus
|
|
|
|
allowing to preserve binary compatibility between releases, the
|
|
|
|
technique known as the Pimpl (private implementation) idiom.
|
|
|
|
*/
|
|
|
|
class QuaZipFilePrivate {
|
|
|
|
friend class QuaZipFile;
|
|
|
|
private:
|
2020-06-08 18:25:36 +00:00
|
|
|
Q_DISABLE_COPY(QuaZipFilePrivate)
|
2015-03-08 18:07:42 +00:00
|
|
|
/// The pointer to the associated QuaZipFile instance.
|
|
|
|
QuaZipFile *q;
|
|
|
|
/// The QuaZip object to work with.
|
|
|
|
QuaZip *zip;
|
|
|
|
/// The file name.
|
|
|
|
QString fileName;
|
|
|
|
/// Case sensitivity mode.
|
|
|
|
QuaZip::CaseSensitivity caseSensitivity;
|
|
|
|
/// Whether this file is opened in the raw mode.
|
|
|
|
bool raw;
|
|
|
|
/// Write position to keep track of.
|
|
|
|
/**
|
|
|
|
QIODevice::pos() is broken for non-seekable devices, so we need
|
|
|
|
our own position.
|
|
|
|
*/
|
|
|
|
qint64 writePos;
|
|
|
|
/// Uncompressed size to write along with a raw file.
|
|
|
|
quint64 uncompressedSize;
|
|
|
|
/// CRC to write along with a raw file.
|
|
|
|
quint32 crc;
|
|
|
|
/// Whether \ref zip points to an internal QuaZip instance.
|
|
|
|
/**
|
|
|
|
This is true if the archive was opened by name, rather than by
|
|
|
|
supplying an existing QuaZip instance.
|
|
|
|
*/
|
|
|
|
bool internal;
|
|
|
|
/// The last error.
|
|
|
|
int zipError;
|
|
|
|
/// Resets \ref zipError.
|
|
|
|
inline void resetZipError() const {setZipError(UNZ_OK);}
|
|
|
|
/// Sets the zip error.
|
|
|
|
/**
|
|
|
|
This function is marked as const although it changes one field.
|
|
|
|
This allows to call it from const functions that don't change
|
|
|
|
anything by themselves.
|
|
|
|
*/
|
|
|
|
void setZipError(int zipError) const;
|
|
|
|
/// The constructor for the corresponding QuaZipFile constructor.
|
|
|
|
inline QuaZipFilePrivate(QuaZipFile *q):
|
2020-06-08 18:25:36 +00:00
|
|
|
q(q),
|
|
|
|
zip(NULL),
|
|
|
|
caseSensitivity(QuaZip::csDefault),
|
|
|
|
raw(false),
|
|
|
|
writePos(0),
|
|
|
|
uncompressedSize(0),
|
|
|
|
crc(0),
|
|
|
|
internal(true),
|
|
|
|
zipError(UNZ_OK) {}
|
2015-03-08 18:07:42 +00:00
|
|
|
/// The constructor for the corresponding QuaZipFile constructor.
|
|
|
|
inline QuaZipFilePrivate(QuaZipFile *q, const QString &zipName):
|
2020-06-08 18:25:36 +00:00
|
|
|
q(q),
|
|
|
|
caseSensitivity(QuaZip::csDefault),
|
|
|
|
raw(false),
|
|
|
|
writePos(0),
|
|
|
|
uncompressedSize(0),
|
|
|
|
crc(0),
|
|
|
|
internal(true),
|
|
|
|
zipError(UNZ_OK)
|
2015-03-08 18:07:42 +00:00
|
|
|
{
|
|
|
|
zip=new QuaZip(zipName);
|
|
|
|
}
|
|
|
|
/// The constructor for the corresponding QuaZipFile constructor.
|
|
|
|
inline QuaZipFilePrivate(QuaZipFile *q, const QString &zipName, const QString &fileName,
|
|
|
|
QuaZip::CaseSensitivity cs):
|
2020-06-08 18:25:36 +00:00
|
|
|
q(q),
|
|
|
|
raw(false),
|
|
|
|
writePos(0),
|
|
|
|
uncompressedSize(0),
|
|
|
|
crc(0),
|
|
|
|
internal(true),
|
|
|
|
zipError(UNZ_OK)
|
2015-03-08 18:07:42 +00:00
|
|
|
{
|
|
|
|
zip=new QuaZip(zipName);
|
|
|
|
this->fileName=fileName;
|
2020-06-08 18:25:36 +00:00
|
|
|
if (this->fileName.startsWith(QLatin1String("/")))
|
2015-03-08 18:07:42 +00:00
|
|
|
this->fileName = this->fileName.mid(1);
|
|
|
|
this->caseSensitivity=cs;
|
|
|
|
}
|
|
|
|
/// The constructor for the QuaZipFile constructor accepting a file name.
|
|
|
|
inline QuaZipFilePrivate(QuaZipFile *q, QuaZip *zip):
|
2020-06-08 18:25:36 +00:00
|
|
|
q(q),
|
|
|
|
zip(zip),
|
|
|
|
raw(false),
|
|
|
|
writePos(0),
|
|
|
|
uncompressedSize(0),
|
|
|
|
crc(0),
|
|
|
|
internal(false),
|
|
|
|
zipError(UNZ_OK) {}
|
2015-03-08 18:07:42 +00:00
|
|
|
/// The destructor.
|
|
|
|
inline ~QuaZipFilePrivate()
|
|
|
|
{
|
|
|
|
if (internal)
|
|
|
|
delete zip;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-03-25 22:16:12 +00:00
|
|
|
QuaZipFile::QuaZipFile():
|
2015-03-08 18:07:42 +00:00
|
|
|
p(new QuaZipFilePrivate(this))
|
2011-03-25 22:16:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QuaZipFile::QuaZipFile(QObject *parent):
|
2015-03-08 18:07:42 +00:00
|
|
|
QIODevice(parent),
|
|
|
|
p(new QuaZipFilePrivate(this))
|
2011-03-25 22:16:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QuaZipFile::QuaZipFile(const QString& zipName, QObject *parent):
|
2015-03-08 18:07:42 +00:00
|
|
|
QIODevice(parent),
|
|
|
|
p(new QuaZipFilePrivate(this, zipName))
|
2011-03-25 22:16:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QuaZipFile::QuaZipFile(const QString& zipName, const QString& fileName,
|
|
|
|
QuaZip::CaseSensitivity cs, QObject *parent):
|
2015-03-08 18:07:42 +00:00
|
|
|
QIODevice(parent),
|
|
|
|
p(new QuaZipFilePrivate(this, zipName, fileName, cs))
|
2011-03-25 22:16:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QuaZipFile::QuaZipFile(QuaZip *zip, QObject *parent):
|
|
|
|
QIODevice(parent),
|
2015-03-08 18:07:42 +00:00
|
|
|
p(new QuaZipFilePrivate(this, zip))
|
2011-03-25 22:16:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QuaZipFile::~QuaZipFile()
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
if (isOpen())
|
|
|
|
close();
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QuaZipFile::getZipName() const
|
|
|
|
{
|
|
|
|
return p->zip==NULL ? QString() : p->zip->getZipName();
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 18:07:42 +00:00
|
|
|
QuaZip *QuaZipFile::getZip() const
|
2011-03-25 22:16:12 +00:00
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
return p->internal ? NULL : p->zip;
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QuaZipFile::getActualFileName()const
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
p->setZipError(UNZ_OK);
|
|
|
|
if (p->zip == NULL || (openMode() & WriteOnly))
|
|
|
|
return QString();
|
|
|
|
QString name=p->zip->getCurrentFileName();
|
2011-03-25 22:16:12 +00:00
|
|
|
if(name.isNull())
|
2015-03-08 18:07:42 +00:00
|
|
|
p->setZipError(p->zip->getZipError());
|
2011-03-25 22:16:12 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuaZipFile::setZipName(const QString& zipName)
|
|
|
|
{
|
|
|
|
if(isOpen()) {
|
|
|
|
qWarning("QuaZipFile::setZipName(): file is already open - can not set ZIP name");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip!=NULL && p->internal)
|
|
|
|
delete p->zip;
|
|
|
|
p->zip=new QuaZip(zipName);
|
|
|
|
p->internal=true;
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuaZipFile::setZip(QuaZip *zip)
|
|
|
|
{
|
|
|
|
if(isOpen()) {
|
|
|
|
qWarning("QuaZipFile::setZip(): file is already open - can not set ZIP");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip!=NULL && p->internal)
|
|
|
|
delete p->zip;
|
|
|
|
p->zip=zip;
|
|
|
|
p->fileName=QString();
|
|
|
|
p->internal=false;
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuaZipFile::setFileName(const QString& fileName, QuaZip::CaseSensitivity cs)
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip==NULL) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::setFileName(): call setZipName() first");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(!p->internal) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::setFileName(): should not be used when not using internal QuaZip");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(isOpen()) {
|
|
|
|
qWarning("QuaZipFile::setFileName(): can not set file name for already opened file");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
p->fileName=fileName;
|
2020-06-08 18:25:36 +00:00
|
|
|
if (p->fileName.startsWith(QLatin1String("/")))
|
2015-03-08 18:07:42 +00:00
|
|
|
p->fileName = p->fileName.mid(1);
|
|
|
|
p->caseSensitivity=cs;
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 18:07:42 +00:00
|
|
|
void QuaZipFilePrivate::setZipError(int zipError) const
|
2011-03-25 22:16:12 +00:00
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
QuaZipFilePrivate *fakeThis = const_cast<QuaZipFilePrivate*>(this); // non-const
|
2011-03-25 22:16:12 +00:00
|
|
|
fakeThis->zipError=zipError;
|
|
|
|
if(zipError==UNZ_OK)
|
2015-03-08 18:07:42 +00:00
|
|
|
q->setErrorString(QString());
|
2011-03-25 22:16:12 +00:00
|
|
|
else
|
2015-03-08 18:07:42 +00:00
|
|
|
q->setErrorString(QuaZipFile::tr("ZIP/UNZIP API error %1").arg(zipError));
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QuaZipFile::open(OpenMode mode)
|
|
|
|
{
|
|
|
|
return open(mode, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuaZipFile::open(OpenMode mode, int *method, int *level, bool raw, const char *password)
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
p->resetZipError();
|
2011-03-25 22:16:12 +00:00
|
|
|
if(isOpen()) {
|
|
|
|
qWarning("QuaZipFile::open(): already opened");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(mode&Unbuffered) {
|
|
|
|
qWarning("QuaZipFile::open(): Unbuffered mode is not supported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if((mode&ReadOnly)&&!(mode&WriteOnly)) {
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->internal) {
|
|
|
|
if(!p->zip->open(QuaZip::mdUnzip)) {
|
|
|
|
p->setZipError(p->zip->getZipError());
|
2011-03-25 22:16:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(!p->zip->setCurrentFile(p->fileName, p->caseSensitivity)) {
|
|
|
|
p->setZipError(p->zip->getZipError());
|
|
|
|
p->zip->close();
|
2011-03-25 22:16:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip==NULL) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::open(): zip is NULL");
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip->getMode()!=QuaZip::mdUnzip) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::open(): file open mode %d incompatible with ZIP open mode %d",
|
2015-03-08 18:07:42 +00:00
|
|
|
(int)mode, (int)p->zip->getMode());
|
2011-03-25 22:16:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(!p->zip->hasCurrentFile()) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::open(): zip does not have current file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
p->setZipError(unzOpenCurrentFile3(p->zip->getUnzFile(), method, level, (int)raw, password));
|
|
|
|
if(p->zipError==UNZ_OK) {
|
2011-03-25 22:16:12 +00:00
|
|
|
setOpenMode(mode);
|
2015-03-08 18:07:42 +00:00
|
|
|
p->raw=raw;
|
2011-03-25 22:16:12 +00:00
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qWarning("QuaZipFile::open(): open mode %d not supported by this function", (int)mode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuaZipFile::open(OpenMode mode, const QuaZipNewInfo& info,
|
|
|
|
const char *password, quint32 crc,
|
|
|
|
int method, int level, bool raw,
|
|
|
|
int windowBits, int memLevel, int strategy)
|
|
|
|
{
|
|
|
|
zip_fileinfo info_z;
|
2015-03-08 18:07:42 +00:00
|
|
|
p->resetZipError();
|
2011-03-25 22:16:12 +00:00
|
|
|
if(isOpen()) {
|
|
|
|
qWarning("QuaZipFile::open(): already opened");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if((mode&WriteOnly)&&!(mode&ReadOnly)) {
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->internal) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::open(): write mode is incompatible with internal QuaZip approach");
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip==NULL) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::open(): zip is NULL");
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip->getMode()!=QuaZip::mdCreate&&p->zip->getMode()!=QuaZip::mdAppend&&p->zip->getMode()!=QuaZip::mdAdd) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::open(): file open mode %d incompatible with ZIP open mode %d",
|
2015-03-08 18:07:42 +00:00
|
|
|
(int)mode, (int)p->zip->getMode());
|
2011-03-25 22:16:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info_z.tmz_date.tm_year=info.dateTime.date().year();
|
|
|
|
info_z.tmz_date.tm_mon=info.dateTime.date().month() - 1;
|
|
|
|
info_z.tmz_date.tm_mday=info.dateTime.date().day();
|
|
|
|
info_z.tmz_date.tm_hour=info.dateTime.time().hour();
|
|
|
|
info_z.tmz_date.tm_min=info.dateTime.time().minute();
|
|
|
|
info_z.tmz_date.tm_sec=info.dateTime.time().second();
|
|
|
|
info_z.dosDate = 0;
|
|
|
|
info_z.internal_fa=(uLong)info.internalAttr;
|
|
|
|
info_z.external_fa=(uLong)info.externalAttr;
|
2015-03-08 18:07:42 +00:00
|
|
|
if (p->zip->isDataDescriptorWritingEnabled())
|
|
|
|
zipSetFlags(p->zip->getZipFile(), ZIP_WRITE_DATA_DESCRIPTOR);
|
|
|
|
else
|
|
|
|
zipClearFlags(p->zip->getZipFile(), ZIP_WRITE_DATA_DESCRIPTOR);
|
2020-06-08 18:25:36 +00:00
|
|
|
p->setZipError(zipOpenNewFileInZip4_64(p->zip->getZipFile(),
|
|
|
|
p->zip->isUtf8Enabled()
|
|
|
|
? info.name.toUtf8().constData()
|
|
|
|
: p->zip->getFileNameCodec()->fromUnicode(info.name).constData(),
|
|
|
|
&info_z,
|
2011-03-25 22:16:12 +00:00
|
|
|
info.extraLocal.constData(), info.extraLocal.length(),
|
|
|
|
info.extraGlobal.constData(), info.extraGlobal.length(),
|
2020-06-08 18:25:36 +00:00
|
|
|
p->zip->isUtf8Enabled()
|
|
|
|
? info.comment.toUtf8().constData()
|
|
|
|
: p->zip->getCommentCodec()->fromUnicode(info.comment).constData(),
|
2011-03-25 22:16:12 +00:00
|
|
|
method, level, (int)raw,
|
|
|
|
windowBits, memLevel, strategy,
|
2020-06-08 18:25:36 +00:00
|
|
|
password, (uLong)crc,
|
|
|
|
(p->zip->getOsCode() << 8) | QUAZIP_VERSION_MADE_BY,
|
|
|
|
0,
|
|
|
|
p->zip->isZip64Enabled()));
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zipError==UNZ_OK) {
|
|
|
|
p->writePos=0;
|
2011-03-25 22:16:12 +00:00
|
|
|
setOpenMode(mode);
|
2015-03-08 18:07:42 +00:00
|
|
|
p->raw=raw;
|
2011-03-25 22:16:12 +00:00
|
|
|
if(raw) {
|
2015-03-08 18:07:42 +00:00
|
|
|
p->crc=crc;
|
|
|
|
p->uncompressedSize=info.uncompressedSize;
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qWarning("QuaZipFile::open(): open mode %d not supported by this function", (int)mode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuaZipFile::isSequential()const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 QuaZipFile::pos()const
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip==NULL) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::pos(): call setZipName() or setZip() first");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!isOpen()) {
|
|
|
|
qWarning("QuaZipFile::pos(): file is not open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(openMode()&ReadOnly)
|
2015-03-08 18:07:42 +00:00
|
|
|
// QIODevice::pos() is broken for sequential devices,
|
|
|
|
// but thankfully bytesAvailable() returns the number of
|
|
|
|
// bytes buffered, so we know how far ahead we are.
|
2020-06-08 18:25:36 +00:00
|
|
|
return unztell64(p->zip->getUnzFile()) - QIODevice::bytesAvailable();
|
2011-03-25 22:16:12 +00:00
|
|
|
else
|
2015-03-08 18:07:42 +00:00
|
|
|
return p->writePos;
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QuaZipFile::atEnd()const
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zip==NULL) {
|
2011-03-25 22:16:12 +00:00
|
|
|
qWarning("QuaZipFile::atEnd(): call setZipName() or setZip() first");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!isOpen()) {
|
|
|
|
qWarning("QuaZipFile::atEnd(): file is not open");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(openMode()&ReadOnly)
|
2015-03-08 18:07:42 +00:00
|
|
|
// the same problem as with pos()
|
|
|
|
return QIODevice::bytesAvailable() == 0
|
|
|
|
&& unzeof(p->zip->getUnzFile())==1;
|
2011-03-25 22:16:12 +00:00
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 QuaZipFile::size()const
|
|
|
|
{
|
|
|
|
if(!isOpen()) {
|
|
|
|
qWarning("QuaZipFile::atEnd(): file is not open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(openMode()&ReadOnly)
|
2015-03-08 18:07:42 +00:00
|
|
|
return p->raw?csize():usize();
|
2011-03-25 22:16:12 +00:00
|
|
|
else
|
2015-03-08 18:07:42 +00:00
|
|
|
return p->writePos;
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 QuaZipFile::csize()const
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
unz_file_info64 info_z;
|
|
|
|
p->setZipError(UNZ_OK);
|
|
|
|
if(p->zip==NULL||p->zip->getMode()!=QuaZip::mdUnzip) return -1;
|
|
|
|
p->setZipError(unzGetCurrentFileInfo64(p->zip->getUnzFile(), &info_z, NULL, 0, NULL, 0, NULL, 0));
|
|
|
|
if(p->zipError!=UNZ_OK)
|
2011-03-25 22:16:12 +00:00
|
|
|
return -1;
|
|
|
|
return info_z.compressed_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 QuaZipFile::usize()const
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
unz_file_info64 info_z;
|
|
|
|
p->setZipError(UNZ_OK);
|
|
|
|
if(p->zip==NULL||p->zip->getMode()!=QuaZip::mdUnzip) return -1;
|
|
|
|
p->setZipError(unzGetCurrentFileInfo64(p->zip->getUnzFile(), &info_z, NULL, 0, NULL, 0, NULL, 0));
|
|
|
|
if(p->zipError!=UNZ_OK)
|
2011-03-25 22:16:12 +00:00
|
|
|
return -1;
|
|
|
|
return info_z.uncompressed_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuaZipFile::getFileInfo(QuaZipFileInfo *info)
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
QuaZipFileInfo64 info64;
|
|
|
|
if (getFileInfo(&info64)) {
|
|
|
|
info64.toQuaZipFileInfo(*info);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuaZipFile::getFileInfo(QuaZipFileInfo64 *info)
|
|
|
|
{
|
|
|
|
if(p->zip==NULL||p->zip->getMode()!=QuaZip::mdUnzip) return false;
|
|
|
|
p->zip->getCurrentFileInfo(info);
|
|
|
|
p->setZipError(p->zip->getZipError());
|
|
|
|
return p->zipError==UNZ_OK;
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuaZipFile::close()
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
p->resetZipError();
|
|
|
|
if(p->zip==NULL||!p->zip->isOpen()) return;
|
2011-03-25 22:16:12 +00:00
|
|
|
if(!isOpen()) {
|
|
|
|
qWarning("QuaZipFile::close(): file isn't open");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(openMode()&ReadOnly)
|
2015-03-08 18:07:42 +00:00
|
|
|
p->setZipError(unzCloseCurrentFile(p->zip->getUnzFile()));
|
2011-03-25 22:16:12 +00:00
|
|
|
else if(openMode()&WriteOnly)
|
2015-03-08 18:07:42 +00:00
|
|
|
if(isRaw()) p->setZipError(zipCloseFileInZipRaw64(p->zip->getZipFile(), p->uncompressedSize, p->crc));
|
|
|
|
else p->setZipError(zipCloseFileInZip(p->zip->getZipFile()));
|
2011-03-25 22:16:12 +00:00
|
|
|
else {
|
|
|
|
qWarning("Wrong open mode: %d", (int)openMode());
|
|
|
|
return;
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->zipError==UNZ_OK) setOpenMode(QIODevice::NotOpen);
|
2011-03-25 22:16:12 +00:00
|
|
|
else return;
|
2015-03-08 18:07:42 +00:00
|
|
|
if(p->internal) {
|
|
|
|
p->zip->close();
|
|
|
|
p->setZipError(p->zip->getZipError());
|
2011-03-25 22:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 QuaZipFile::readData(char *data, qint64 maxSize)
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
p->setZipError(UNZ_OK);
|
|
|
|
qint64 bytesRead=unzReadCurrentFile(p->zip->getUnzFile(), data, (unsigned)maxSize);
|
|
|
|
if (bytesRead < 0) {
|
|
|
|
p->setZipError((int) bytesRead);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-25 22:16:12 +00:00
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 QuaZipFile::writeData(const char* data, qint64 maxSize)
|
|
|
|
{
|
2015-03-08 18:07:42 +00:00
|
|
|
p->setZipError(ZIP_OK);
|
|
|
|
p->setZipError(zipWriteInFileInZip(p->zip->getZipFile(), data, (uint)maxSize));
|
|
|
|
if(p->zipError!=ZIP_OK) return -1;
|
2011-03-25 22:16:12 +00:00
|
|
|
else {
|
2015-03-08 18:07:42 +00:00
|
|
|
p->writePos+=maxSize;
|
2011-03-25 22:16:12 +00:00
|
|
|
return maxSize;
|
|
|
|
}
|
|
|
|
}
|
2015-03-08 18:07:42 +00:00
|
|
|
|
|
|
|
QString QuaZipFile::getFileName() const
|
|
|
|
{
|
|
|
|
return p->fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuaZip::CaseSensitivity QuaZipFile::getCaseSensitivity() const
|
|
|
|
{
|
|
|
|
return p->caseSensitivity;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuaZipFile::isRaw() const
|
|
|
|
{
|
|
|
|
return p->raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QuaZipFile::getZipError() const
|
|
|
|
{
|
|
|
|
return p->zipError;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 QuaZipFile::bytesAvailable() const
|
|
|
|
{
|
|
|
|
return size() - pos();
|
|
|
|
}
|
2020-06-08 18:25:36 +00:00
|
|
|
|
|
|
|
QByteArray QuaZipFile::getLocalExtraField()
|
|
|
|
{
|
|
|
|
int size = unzGetLocalExtrafield(p->zip->getUnzFile(), NULL, 0);
|
|
|
|
QByteArray extra(size, '\0');
|
|
|
|
int err = unzGetLocalExtrafield(p->zip->getUnzFile(), extra.data(), static_cast<uint>(extra.size()));
|
|
|
|
if (err < 0) {
|
|
|
|
p->setZipError(err);
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
return extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime QuaZipFile::getExtModTime()
|
|
|
|
{
|
|
|
|
return QuaZipFileInfo64::getExtTime(getLocalExtraField(), QUAZIP_EXTRA_EXT_MOD_TIME_FLAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime QuaZipFile::getExtAcTime()
|
|
|
|
{
|
|
|
|
return QuaZipFileInfo64::getExtTime(getLocalExtraField(), QUAZIP_EXTRA_EXT_AC_TIME_FLAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime QuaZipFile::getExtCrTime()
|
|
|
|
{
|
|
|
|
return QuaZipFileInfo64::getExtTime(getLocalExtraField(), QUAZIP_EXTRA_EXT_CR_TIME_FLAG);
|
|
|
|
}
|