Add getFileNameList() and getFileInfoList()

This commit is contained in:
alqualos 2011-09-04 18:14:49 +00:00
parent 6868cee84a
commit d33af3a600
12 changed files with 212 additions and 32 deletions

View file

@ -11,6 +11,5 @@ set(LIB_DESTINATION "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}" CACHE STRING "Lib
include_directories(${CMAKE_SOURCE_DIR})
add_subdirectory(quazip)
add_subdirectory(test)
install(FILES FindQuaZip.cmake DESTINATION ${CMAKE_ROOT}/Modules)

View file

@ -659,7 +659,7 @@ EXCLUDE_SYMLINKS = NO
# against the file with absolute path, so to exclude all test directories
# for example use the pattern */test/*
EXCLUDE_PATTERNS = */.moc/*
EXCLUDE_PATTERNS = */.moc/* */release/* */debug/*
# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
# (namespaces, classes, functions, etc.) that should be excluded from the

View file

@ -1,3 +0,0 @@
QuaZIP does not use autoconf (and probably will not, because I do not
want to mess with it), so see README file or doc/html for the
installation details.

View file

@ -18,7 +18,7 @@ You must make sure that:
* "qmake" command runs Qt 4's qmake, not some other version (you'll have
to type full path to qmake otherwise).
To install compiled static library, just type:
To install compiled shared library, just type:
$ make install
@ -33,6 +33,15 @@ project and use them.
See doc/html or, if you do not have a browser, quazip/*.h and
quazip/doc/* files for the more detailed documentation.
For Windows, it's essentially the same, but you may have to adjust
settings for different environments.
If you want to include QuaZIP sources directly into your project or if
you want to use QuaZIP compiled as a static library using
"qmake CONFIG+=statliclib", you have to define the QUAZIP_STATIC macro,
otherwise you're likely to run into problems as QuaZIP symbols will be
marked as dllimported.
Copyright notice:
Copyright (C) 2005-2011 Sergey A. Tachenov

View file

@ -1,5 +1,19 @@
#include "JlCompress.h"
#include <QDebug>
static bool copyData(QIODevice &inFile, QIODevice &outFile)
{
while (!inFile.atEnd()) {
char buf[4096];
qint64 readLen = inFile.read(buf, 4096);
if (readLen <= 0)
return false;
if (outFile.write(buf, readLen) != readLen)
return false;
}
return true;
}
/**OK
* Comprime il file fileName, nell'oggetto zip, con il nome fileDest.
*
@ -32,18 +46,10 @@ bool JlCompress::compressFile(QuaZip* zip, QString fileName, QString fileDest) {
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, inFile.fileName()))) return false;
// Copio i dati
for (qint64 pos = 0, len = inFile.size(); pos < len; ) {
char buf[4096];
qint64 readLen = qMin((qint64) 4096, len - pos);
if (inFile.read(buf, readLen) != readLen)
return false;
if (outFile.write(buf, readLen) != readLen)
return false;
pos += readLen;
if (!copyData(inFile, outFile) || outFile.getZipError()!=UNZ_OK) {
return false;
}
if(outFile.getZipError()!=UNZ_OK) return false;
// Chiudo i file
outFile.close();
if (outFile.getZipError()!=UNZ_OK) return false;
@ -149,9 +155,7 @@ bool JlCompress::extractFile(QuaZip* zip, QString fileName, QString fileDest) {
if(!outFile.open(QIODevice::WriteOnly)) return false;
// Copio i dati
char c;
while(inFile.getChar(&c)) outFile.putChar(c);
if (inFile.getZipError()!=UNZ_OK) {
if (!copyData(inFile, outFile) || inFile.getZipError()!=UNZ_OK) {
removeFile(QStringList(fileDest));
return false;
}

View file

@ -60,12 +60,11 @@
*
* \note Instructions given in this section assume that you are
* using some UNIX dialect, but the build process should be very similar
* on win32-g++ platform too. Sorry, but other platforms are
* undocumented. I do not think it is a big deal, though - it is
* standard usage of the Qt's qmake, so you most probably already know
* everything that is required.
* on win32-g++ platform too. On other platforms it's essentially the
* same process, maybe with some qmake adjustments not specific to
* QuaZIP itself.
*
* To build it on some UNIX dialect:
* To build the library, run:
\verbatim
$ cd /wherever/quazip/source/is/quazip-x.y.z/quazip
$ qmake [PREFIX=where-to-install]

View file

@ -29,6 +29,7 @@ quazip/(un)zip.h files for details, basically it's zlib license.
class QuaZipPrivate {
friend class QuaZip;
private:
QuaZip *q;
QTextCodec *fileNameCodec, *commentCodec;
QString zipName;
QIODevice *ioDevice;
@ -41,7 +42,8 @@ class QuaZipPrivate {
bool hasCurrentFile_f;
int zipError;
bool dataDescriptorWritingEnabled;
inline QuaZipPrivate():
inline QuaZipPrivate(QuaZip *q):
q(q),
fileNameCodec(QTextCodec::codecForLocale()),
commentCodec(QTextCodec::codecForLocale()),
ioDevice(NULL),
@ -49,7 +51,8 @@ class QuaZipPrivate {
hasCurrentFile_f(false),
zipError(UNZ_OK),
dataDescriptorWritingEnabled(true) {}
inline QuaZipPrivate(const QString &zipName):
inline QuaZipPrivate(QuaZip *q, const QString &zipName):
q(q),
fileNameCodec(QTextCodec::codecForLocale()),
commentCodec(QTextCodec::codecForLocale()),
zipName(zipName),
@ -58,7 +61,8 @@ class QuaZipPrivate {
hasCurrentFile_f(false),
zipError(UNZ_OK),
dataDescriptorWritingEnabled(true) {}
inline QuaZipPrivate(QIODevice *ioDevice):
inline QuaZipPrivate(QuaZip *q, QIODevice *ioDevice):
q(q),
fileNameCodec(QTextCodec::codecForLocale()),
commentCodec(QTextCodec::codecForLocale()),
ioDevice(ioDevice),
@ -66,20 +70,22 @@ class QuaZipPrivate {
hasCurrentFile_f(false),
zipError(UNZ_OK),
dataDescriptorWritingEnabled(true) {}
template<typename TFileInfo>
bool getFileInfoList(QList<TFileInfo> *result) const;
};
QuaZip::QuaZip():
p(new QuaZipPrivate())
p(new QuaZipPrivate(this))
{
}
QuaZip::QuaZip(const QString& zipName):
p(new QuaZipPrivate(zipName))
p(new QuaZipPrivate(this, zipName))
{
}
QuaZip::QuaZip(QIODevice *ioDevice):
p(new QuaZipPrivate(ioDevice))
p(new QuaZipPrivate(this, ioDevice))
{
}
@ -440,3 +446,74 @@ bool QuaZip::isDataDescriptorWritingEnabled() const
{
return p->dataDescriptorWritingEnabled;
}
template<typename TFileInfo>
static TFileInfo getFileInfo(QuaZip *zip, bool *ok);
template<>
QuaZipFileInfo getFileInfo(QuaZip *zip, bool *ok)
{
QuaZipFileInfo info;
*ok = zip->getCurrentFileInfo(&info);
return info;
}
template<>
QString getFileInfo(QuaZip *zip, bool *ok)
{
QString name = zip->getCurrentFileName();
*ok = !name.isEmpty();
return name;
}
template<typename TFileInfo>
bool QuaZipPrivate::getFileInfoList(QList<TFileInfo> *result) const
{
QuaZipPrivate *fakeThis=const_cast<QuaZipPrivate*>(this);
fakeThis->zipError=UNZ_OK;
if (mode!=QuaZip::mdUnzip) {
qWarning("QuaZip::getFileNameList/getFileInfoList(): "
"ZIP is not open in mdUnzip mode");
return false;
}
QString currentFile;
if (q->hasCurrentFile()) {
currentFile = q->getCurrentFileName();
}
if (q->goToFirstFile()) {
do {
bool ok;
result->append(getFileInfo<TFileInfo>(q, &ok));
if (!ok)
return false;
} while (q->goToNextFile());
}
if (zipError != UNZ_OK)
return false;
if (currentFile.isEmpty()) {
if (!q->setCurrentFile(currentFile))
return false;
} else {
if (!q->goToFirstFile())
return false;
}
return true;
}
QStringList QuaZip::getFileNameList() const
{
QStringList list;
if (p->getFileInfoList(&list))
return list;
else
return QStringList();
}
QList<QuaZipFileInfo> QuaZip::getFileInfoList() const
{
QList<QuaZipFileInfo> list;
if (p->getFileInfoList(&list))
return list;
else
return QList<QuaZipFileInfo>();
}

View file

@ -25,6 +25,7 @@ quazip/(un)zip.h files for details, basically it's zlib license.
**/
#include <QString>
#include <QStringList>
#include <QTextCodec>
#include "zip.h"
@ -384,6 +385,22 @@ class QUAZIP_EXPORT QuaZip {
\sa setDataDescriptorWritingEnabled()
*/
bool isDataDescriptorWritingEnabled() const;
/// Returns a list of files inside the archive.
/**
\return A list of file names or an empty list if there
was an error or if the archive is empty (call getZipError() to
figure out which).
\sa getFileInfoList()
*/
QStringList getFileNameList() const;
/// Returns information list about all files inside the archive.
/**
\return A list of QuaZipFileInfo objects or an empty list if there
was an error or if the archive is empty (call getZipError() to
figure out which).
\sa getFileNameList()
*/
QList<QuaZipFileInfo> getFileInfoList() const;
};
#endif

View file

@ -1,3 +1,4 @@
#include "testquazip.h"
#include "testquazipfile.h"
#include "testquachecksum32.h"
#include "testjlcompress.h"
@ -54,6 +55,8 @@ int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
int err = 0;
TestQuaZip testQuaZip;
err = qMax(err, QTest::qExec(&testQuaZip, app.arguments()));
TestQuaZipFile testQuaZipFile;
err = qMax(err, QTest::qExec(&testQuaZipFile, app.arguments()));
TestQuaChecksum32 testQuaChecksum32;

View file

@ -7,11 +7,13 @@ INCLUDEPATH += . ..
LIBS += -lquazip
# Input
HEADERS += testquazipfile.h \
HEADERS += testquazip.h \
testquazipfile.h \
testquachecksum32.h \
testjlcompress.h \
qztest.h
SOURCES += testquazipfile.cpp \
SOURCES += testquazip.cpp \
testquazipfile.cpp \
testquachecksum32.cpp \
testjlcompress.cpp \
qztest.cpp

View file

@ -0,0 +1,60 @@
#include "testquazip.h"
#include "qztest.h"
#include <QDir>
#include <QFileInfo>
#include <QHash>
#include <QtTest/QtTest>
#include <quazip/quazip.h>
#include <quazip/jlcompress.h>
void TestQuaZip::getFileList_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QStringList>("fileNames");
QTest::newRow("simple") << "qzfilelist.zip" << (
QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt");
}
void TestQuaZip::getFileList()
{
QFETCH(QString, zipName);
QFETCH(QStringList, fileNames);
qSort(fileNames);
QDir curDir;
if (curDir.exists(zipName)) {
if (!curDir.remove(zipName))
QFAIL("Can't remove zip file");
}
if (!createTestFiles(fileNames)) {
QFAIL("Can't create test file");
}
if (!JlCompress::compressDir(zipName, "tmp")) {
QFAIL("Can't create test archive");
}
QuaZip testZip(zipName);
QVERIFY(testZip.open(QuaZip::mdUnzip));
QVERIFY(testZip.goToFirstFile());
QString firstFile = testZip.getCurrentFileName();
QStringList fileList = testZip.getFileNameList();
qSort(fileList);
QCOMPARE(fileList, fileNames);
QHash<QString, QFileInfo> srcInfo;
foreach (QString fileName, fileNames) {
srcInfo[fileName] = QFileInfo("tmp/" + fileName);
}
QList<QuaZipFileInfo> destList = testZip.getFileInfoList();
QCOMPARE(destList.size(), srcInfo.size());
for (int i = 0; i < destList.size(); i++) {
QCOMPARE(static_cast<qint64>(destList[i].uncompressedSize),
srcInfo[destList[i].name].size());
}
// test that we didn't mess up the current file
QCOMPARE(testZip.getCurrentFileName(), firstFile);
removeTestFiles(fileNames);
curDir.remove(zipName);
}

13
quazip/test/testquazip.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef QUAZIP_TEST_QUAZIP_H
#define QUAZIP_TEST_QUAZIP_H
#include <QObject>
class TestQuaZip: public QObject {
Q_OBJECT
private slots:
void getFileList_data();
void getFileList();
};
#endif // QUAZIP_TEST_QUAZIP_H