Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated QuaZip to 0.4.4

  • Loading branch information...
commit b749c5e42b781bb637400796a05806228e1b69f7 1 parent 1d058b0
@nikola-kocic authored
View
34 quazip/JlCompress.cpp
@@ -147,7 +147,13 @@ bool JlCompress::extractFile(QuaZip* zip, QString fileName, QString fileDest) {
if(!inFile.open(QIODevice::ReadOnly) || inFile.getZipError()!=UNZ_OK) return false;
// Controllo esistenza cartella file risultato
- QDir().mkpath(QFileInfo(fileDest).absolutePath());
+ QDir curDir;
+ if (!curDir.mkpath(QFileInfo(fileDest).absolutePath())) {
+ return false;
+ }
+
+ if (QFileInfo(fileDest).isDir())
+ return true;
// Apro il file risultato
QFile outFile;
@@ -156,9 +162,11 @@ bool JlCompress::extractFile(QuaZip* zip, QString fileName, QString fileDest) {
// Copio i dati
if (!copyData(inFile, outFile) || inFile.getZipError()!=UNZ_OK) {
+ outFile.close();
removeFile(QStringList(fileDest));
return false;
}
+ outFile.close();
// Chiudo i file
inFile.close();
@@ -166,7 +174,6 @@ bool JlCompress::extractFile(QuaZip* zip, QString fileName, QString fileDest) {
removeFile(QStringList(fileDest));
return false;
}
- outFile.close();
return true;
}
@@ -293,7 +300,7 @@ bool JlCompress::compressDir(QString fileCompressed, QString dir, bool recursive
}
// Aggiungo i file e le sotto cartelle
- if (!compressSubDir(zip,dir,dir,recursive)<0) {
+ if (!compressSubDir(zip,dir,dir,recursive)) {
delete zip;
QFile::remove(fileCompressed);
return false;
@@ -372,25 +379,27 @@ QStringList JlCompress::extractFiles(QString fileCompressed, QStringList files,
}
// Estraggo i file
+ QStringList extracted;
for (int i=0; i<files.count(); i++) {
- if (!extractFile(zip, files.at(i), QDir(dir).absoluteFilePath(files.at(i)))) {
+ QString absPath = QDir(dir).absoluteFilePath(files.at(i));
+ if (!extractFile(zip, files.at(i), absPath)) {
delete zip;
- removeFile(files);
+ removeFile(extracted);
return QStringList();
}
- files[i] = QDir(dir).absoluteFilePath(files.at(i));
+ extracted.append(absPath);
}
// Chiudo il file zip
zip->close();
if(zip->getZipError()!=0) {
delete zip;
- removeFile(files);
+ removeFile(extracted);
return QStringList();
}
delete zip;
- return files;
+ return extracted;
}
/**OK
@@ -416,26 +425,27 @@ QStringList JlCompress::extractDir(QString fileCompressed, QString dir) {
QStringList lst = getFileList(fileCompressed);
QDir directory(dir);
+ QStringList extracted;
for (int i=0; i<lst.count(); i++) {
QString absFilePath = directory.absoluteFilePath(lst.at(i));
if (!extractFile(zip, lst.at(i), absFilePath)) {
delete zip;
- removeFile(lst);
+ removeFile(extracted);
return QStringList();
}
- lst[i] = absFilePath;
+ extracted.append(absFilePath);
}
// Chiudo il file zip
zip->close();
if(zip->getZipError()!=0) {
delete zip;
- removeFile(lst);
+ removeFile(extracted);
return QStringList();
}
delete zip;
- return lst;
+ return extracted;
}
/**OK
View
86 quazip/JlCompress.h
@@ -9,23 +9,105 @@
#include <QFileInfo>
#include <QFile>
+/// Utility class for typical operations.
+/**
+ This class contains a number of useful static functions to perform
+ simple operations, such as mass ZIP packing or extraction.
+ */
class QUAZIP_EXPORT JlCompress {
private:
+ /// Compress a single file.
+ /**
+ \param zip Opened zip to compress the file to.
+ \param fileName The full path to the source file.
+ \param fileDest The full name of the file inside the archive.
+ \return true if success, false otherwise.
+ */
static bool compressFile(QuaZip* zip, QString fileName, QString fileDest);
+ /// Compress a subdirectory.
+ /**
+ \param parentZip Opened zip containing the parent directory.
+ \param dir The full path to the directory to pack.
+ \param parentDir The full path to the directory corresponding to
+ the root of the ZIP.
+ \param recursive Whether to pack sub-directories as well or only
+ files.
+ \return true if success, false otherwise.
+ */
static bool compressSubDir(QuaZip* parentZip, QString dir, QString parentDir, bool recursive = true);
+ /// Extract a single file.
+ /**
+ \param zip The opened zip archive to extract from.
+ \param fileName The full name of the file to extract.
+ \param fileDest The full path to the destination file.
+ \return true if success, false otherwise.
+ */
static bool extractFile(QuaZip* zip, QString fileName, QString fileDest);
-
+ /// Remove some files.
+ /**
+ \param listFile The list of files to remove.
+ \return true if success, false otherwise.
+ */
static bool removeFile(QStringList listFile);
public:
+ /// Compress a single file.
+ /**
+ \param fileCompressed The name of the archive.
+ \param file The file to compress.
+ \return true if success, false otherwise.
+ */
static bool compressFile(QString fileCompressed, QString file);
+ /// Compress a list of files.
+ /**
+ \param fileCompressed The name of the archive.
+ \param files The file list to compress.
+ \return true if success, false otherwise.
+ */
static bool compressFiles(QString fileCompressed, QStringList files);
+ /// Compress a whole directory.
+ /**
+ \param fileCompressed The name of the archive.
+ \param dir The directory to compress.
+ \param recursive Whether to pack the subdirectories as well, or
+ just regular files.
+ \return true if success, false otherwise.
+ */
static bool compressDir(QString fileCompressed, QString dir = QString(), bool recursive = true);
public:
- static QString extractFile(QString fileCompressed, QString file, QString fileDest = QString());
+ /// Extract a single file.
+ /**
+ \param fileCompressed The name of the archive.
+ \param fileName The file to extract.
+ \param fileDest The destination file, assumed to be identical to
+ \a file if left empty.
+ \return The list of the full paths of the files extracted, empty on failure.
+ */
+ static QString extractFile(QString fileCompressed, QString fileName, QString fileDest = QString());
+ /// Extract a list of files.
+ /**
+ \param fileCompressed The name of the archive.
+ \param files The file list to extract.
+ \param dir The directory to put the files to, the current
+ directory if left empty.
+ \return The list of the full paths of the files extracted, empty on failure.
+ */
static QStringList extractFiles(QString fileCompressed, QStringList files, QString dir = QString());
+ /// Extract a whole archive.
+ /**
+ \param fileCompressed The name of the archive.
+ \param dir The directory to extract to, the current directory if
+ left empty.
+ \return The list of the full paths of the files extracted, empty on failure.
+ */
static QStringList extractDir(QString fileCompressed, QString dir = QString());
+ /// Get the file list.
+ /**
+ \return The list of the files in the archive, or, more precisely, the
+ list of the entries, including both files and directories if they
+ are present separately.
+ */
static QStringList getFileList(QString fileCompressed);
};
View
13 quazip/qioapi.cpp
@@ -38,8 +38,6 @@ voidpf ZCALLBACK qiodevice_open_file_func (
int mode)
{
QIODevice *iodevice = reinterpret_cast<QIODevice*>(file);
- if(iodevice->isSequential())
- return NULL;
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
iodevice->open(QIODevice::ReadOnly);
else
@@ -49,9 +47,14 @@ voidpf ZCALLBACK qiodevice_open_file_func (
if (mode & ZLIB_FILEFUNC_MODE_CREATE)
iodevice->open(QIODevice::WriteOnly);
- if(iodevice->isOpen())
- return iodevice;
- else
+ if (iodevice->isOpen()) {
+ if (iodevice->isSequential()) {
+ iodevice->close();
+ return NULL;
+ } else {
+ return iodevice;
+ }
+ } else
return NULL;
}
View
35 quazip/quazip.cpp
@@ -26,22 +26,44 @@ quazip/(un)zip.h files for details, basically it's zlib license.
#include "quazip.h"
+/// All the internal stuff for the QuaZip class.
+/**
+ \internal
+
+ This class keeps all the private stuff for the QuaZip class so it can
+ be changed without breaking binary compatibility, according to the
+ Pimpl idiom.
+ */
class QuaZipPrivate {
friend class QuaZip;
private:
- QuaZip *q;
- QTextCodec *fileNameCodec, *commentCodec;
+ /// The pointer to the corresponding QuaZip instance.
+ QuaZip *q;
+ /// The codec for file names.
+ QTextCodec *fileNameCodec;
+ /// The codec for comments.
+ QTextCodec *commentCodec;
+ /// The archive file name.
QString zipName;
+ /// The device to access the archive.
QIODevice *ioDevice;
+ /// The global comment.
QString comment;
+ /// The open mode.
QuaZip::Mode mode;
union {
+ /// The internal handle for UNZIP modes.
unzFile unzFile_f;
+ /// The internal handle for ZIP modes.
zipFile zipFile_f;
};
+ /// Whether a current file is set.
bool hasCurrentFile_f;
+ /// The last error.
int zipError;
+ /// Whether \ref QuaZip::setDataDescriptorWritingEnabled() "the data descriptor writing mode" is enabled.
bool dataDescriptorWritingEnabled;
+ /// The constructor for the corresponding QuaZip constructor.
inline QuaZipPrivate(QuaZip *q):
q(q),
fileNameCodec(QTextCodec::codecForLocale()),
@@ -51,6 +73,7 @@ class QuaZipPrivate {
hasCurrentFile_f(false),
zipError(UNZ_OK),
dataDescriptorWritingEnabled(true) {}
+ /// The constructor for the corresponding QuaZip constructor.
inline QuaZipPrivate(QuaZip *q, const QString &zipName):
q(q),
fileNameCodec(QTextCodec::codecForLocale()),
@@ -61,6 +84,7 @@ class QuaZipPrivate {
hasCurrentFile_f(false),
zipError(UNZ_OK),
dataDescriptorWritingEnabled(true) {}
+ /// The constructor for the corresponding QuaZip constructor.
inline QuaZipPrivate(QuaZip *q, QIODevice *ioDevice):
q(q),
fileNameCodec(QTextCodec::codecForLocale()),
@@ -70,6 +94,7 @@ class QuaZipPrivate {
hasCurrentFile_f(false),
zipError(UNZ_OK),
dataDescriptorWritingEnabled(true) {}
+ /// Returns either a list of file names or a list of QuaZipFileInfo.
template<typename TFileInfo>
bool getFileInfoList(QList<TFileInfo> *result) const;
};
@@ -173,8 +198,10 @@ void QuaZip::close()
return;
}
// opened by name, need to delete the internal IO device
- if (!p->zipName.isEmpty())
- delete p->ioDevice;
+ if (!p->zipName.isEmpty()) {
+ delete p->ioDevice;
+ p->ioDevice = NULL;
+ }
if(p->zipError==UNZ_OK)
p->mode=mdNotOpen;
}
View
45 quazip/quazipfile.cpp
@@ -25,30 +25,64 @@ quazip/(un)zip.h files for details, basically it's zlib license.
using namespace std;
+/// 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:
+ /// 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;
- // these two are for writing raw files
+ /// Uncompressed size to write along with a raw file.
ulong 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);}
- // const, but sets zipError!
+ /// 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):
q(q), zip(NULL), internal(true), zipError(UNZ_OK) {}
+ /// The constructor for the corresponding QuaZipFile constructor.
inline QuaZipFilePrivate(QuaZipFile *q, const QString &zipName):
q(q), internal(true), zipError(UNZ_OK)
{
zip=new QuaZip(zipName);
}
+ /// The constructor for the corresponding QuaZipFile constructor.
inline QuaZipFilePrivate(QuaZipFile *q, const QString &zipName, const QString &fileName,
QuaZip::CaseSensitivity cs):
q(q), internal(true), zipError(UNZ_OK)
@@ -57,8 +91,10 @@ class QuaZipFilePrivate {
this->fileName=fileName;
this->caseSensitivity=cs;
}
+ /// The constructor for the QuaZipFile constructor accepting a file name.
inline QuaZipFilePrivate(QuaZipFile *q, QuaZip *zip):
q(q), zip(zip), internal(false), zipError(UNZ_OK) {}
+ /// The destructor.
inline ~QuaZipFilePrivate()
{
if (internal)
@@ -108,6 +144,11 @@ QString QuaZipFile::getZipName() const
return p->zip==NULL ? QString() : p->zip->getZipName();
}
+QuaZip *QuaZipFile::getZip() const
+{
+ return p->internal ? NULL : p->zip;
+}
+
QString QuaZipFile::getActualFileName()const
{
p->setZipError(UNZ_OK);
View
1  quazip/zip.c
@@ -617,6 +617,7 @@ extern zipFile ZEXPORT zipOpen2 (file, append, globalcomment, pzlib_filefunc_def
if (err!=ZIP_OK)
{
ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
+ TRYFREE(zi);
return NULL;
}
Please sign in to comment.
Something went wrong with that request. Please try again.