Beispiel #1
0
void TestJlCompress::compressFile()
{
    QFETCH(QString, zipName);
    QFETCH(QString, fileName);
    QDir curDir;
    if (curDir.exists(zipName)) {
        if (!curDir.remove(zipName))
            QFAIL("Can't remove zip file");
    }
    if (!createTestFiles(QStringList() << fileName)) {
        QFAIL("Can't create test file");
    }
    QVERIFY(JlCompress::compressFile(zipName, "tmp/" + fileName));
    // get the file list and check it
    QStringList fileList = JlCompress::getFileList(zipName);
    QCOMPARE(fileList.count(), 1);
    QVERIFY(fileList[0] == fileName);
    // now test the QIODevice* overload of getFileList()
    QFile zipFile(zipName);
    QVERIFY(zipFile.open(QIODevice::ReadOnly));
    fileList = JlCompress::getFileList(zipName);
    QCOMPARE(fileList.count(), 1);
    QVERIFY(fileList[0] == fileName);
    zipFile.close();
    removeTestFiles(QStringList() << fileName);
    curDir.remove(zipName);
}
Beispiel #2
0
void TestQuaZipDir::entryList()
{
    QFETCH(QString, zipName);
    QFETCH(QStringList, fileNames);
    QFETCH(QString, dirName);
    QFETCH(int, filter);
    QFETCH(int, sort);
    QDir::Filters filters = static_cast<QDir::Filters>(filter);
    QDir::SortFlags sorting = static_cast<QDir::SortFlags>(sort);
    QFETCH(QStringList, entries);
    QDir curDir;
    if (!createTestFiles(fileNames)) {
        QFAIL("Couldn't create test files");
    }
    if (!createTestArchive(zipName, fileNames)) {
        QFAIL("Couldn't create test archive");
    }
    removeTestFiles(fileNames);
    QuaZip zip(zipName);
    QVERIFY(zip.open(QuaZip::mdUnzip));
    QuaZipDir dir(&zip, dirName);
    QCOMPARE(dir.entryList(filters, sorting), entries);
    zip.close();
    curDir.remove(zipName);
}
Beispiel #3
0
void TestQuaZipDir::operators()
{
    QString zipName = "zipDirOperators.zip";
    QStringList fileNames;
    fileNames << "dir/test.txt" << "root.txt";
    if (!createTestFiles(fileNames)) {
        QFAIL("Couldn't create test files");
    }
    if (!createTestArchive(zipName, fileNames)) {
        QFAIL("Couldn't create test archive");
    }
    removeTestFiles(fileNames);
    QuaZip zip(zipName);
    QDir curDir;
    QVERIFY(zip.open(QuaZip::mdUnzip));
    QuaZipDir dir(&zip, "dir");
    QuaZipDir dir2 = dir; // Copy constructor
    QCOMPARE(dir2.path(), QString::fromLatin1("dir"));
    dir2.cdUp();
    QCOMPARE(dir2.path(), QString::fromLatin1(""));
    QCOMPARE(dir.path(), QString::fromLatin1("dir"));
    dir2 = dir; // operator=()
    QCOMPARE(dir2.path(), QString::fromLatin1("dir"));
    QVERIFY(dir2 == dir); // opertor==
    dir.cd("/");
    QCOMPARE(dir[0], QString::fromLatin1("dir/"));
    QCOMPARE(dir[1], QString::fromLatin1("root.txt"));
    zip.close();
    curDir.remove(zipName);
}
Beispiel #4
0
void TestQuaZipDir::cd()
{
    QFETCH(QString, zipName);
    QFETCH(QStringList, fileNames);
    QFETCH(QString, dirName);
    QFETCH(QString, targetDirName);
    QFETCH(QString, result);
    QDir curDir;
    if (!createTestFiles(fileNames)) {
        QFAIL("Couldn't create test files");
    }
    if (!createTestArchive(zipName, fileNames)) {
        QFAIL("Couldn't create test archive");
    }
    removeTestFiles(fileNames);
    QuaZip zip(zipName);
    QVERIFY(zip.open(QuaZip::mdUnzip));
    QuaZipDir dir(&zip, dirName);
    if (dirName.startsWith('/')) {
        dirName = dirName.mid(1);
    }
    if (dirName.endsWith('/')) {
        dirName.chop(1);
    }
    QCOMPARE(dir.path(), dirName);
    {
        int lastSlash = dirName.lastIndexOf('/');
        if (lastSlash == -1) {
            // dirName is just a single name
            if (dirName.isEmpty()) {
                QCOMPARE(dir.dirName(), QString::fromLatin1("."));
            } else {
                QCOMPARE(dir.dirName(), dirName);
            }
        } else {
            // dirName is a sequence
            QCOMPARE(dir.dirName(), dirName.mid(lastSlash + 1));
        }
    }
    if (targetDirName == "..") {
        QVERIFY(dir.cdUp());
    } else {
        QVERIFY(dir.cd(targetDirName));
    }
    QCOMPARE(dir.path(), result);
    // Try to go back
    dir.setPath(dirName);
    QCOMPARE(dir.path(), dirName);
    zip.close();
    curDir.remove(zipName);
}
Beispiel #5
0
void TestQuaZipDir::entryList()
{
    QFETCH(QString, zipName);
    QFETCH(QStringList, fileNames);
    QFETCH(QString, dirName);
    QFETCH(QStringList, nameFilters);
    QFETCH(int, filter);
    QFETCH(int, sort);
    QDir::Filters filters = static_cast<QDir::Filters>(filter);
    QDir::SortFlags sorting = static_cast<QDir::SortFlags>(sort);
    QFETCH(QStringList, entries);
    QFETCH(int, caseSensitivity);
    QDir curDir;
    if (!createTestFiles(fileNames)) {
        QFAIL("Couldn't create test files");
    }
    if (!createTestArchive(zipName, fileNames)) {
        QFAIL("Couldn't create test archive");
    }
    removeTestFiles(fileNames);
    QuaZip zip(zipName);
    QVERIFY(zip.open(QuaZip::mdUnzip));
    QuaZipDir dir(&zip, dirName);
    QVERIFY(dir.exists());
    if (caseSensitivity != -1) {
        dir.setCaseSensitivity(static_cast<QuaZip::CaseSensitivity>(
                                   caseSensitivity));
        QCOMPARE(dir.caseSensitivity(), static_cast<QuaZip::CaseSensitivity>(
                     caseSensitivity));
    }
    QCOMPARE(dir.entryList(nameFilters, filters, sorting), entries);
    // Test default sorting setting.
    dir.setSorting(sorting);
    QCOMPARE(dir.sorting(), sorting);
    QCOMPARE(dir.entryList(nameFilters, filters), entries);
    // Test default name filter setting.
    dir.setNameFilters(nameFilters);
    QCOMPARE(dir.nameFilters(), nameFilters);
    QCOMPARE(dir.entryList(filters), entries);
    // Test default filters.
    dir.setFilter(filters);
    QCOMPARE(dir.filter(), filters);
    QCOMPARE(dir.entryList(), entries);
    QCOMPARE(dir.entryList().count(), static_cast<int>(dir.count()));
    zip.close();
    curDir.remove(zipName);
}
Beispiel #6
0
void TestJlCompress::compressFile()
{
    QFETCH(QString, zipName);
    QFETCH(QString, fileName);
    QDir curDir;
    if (curDir.exists(zipName)) {
        if (!curDir.remove(zipName))
            QFAIL("Can't remove zip file");
    }
    if (!createTestFiles(QStringList() << fileName)) {
        QFAIL("Can't create test file");
    }
    QVERIFY(JlCompress::compressFile(zipName, "tmp/" + fileName));
    // get the file list and check it
    QStringList fileList = JlCompress::getFileList(zipName);
    QCOMPARE(fileList.count(), 1);
    QVERIFY(fileList[0] == fileName);
    removeTestFiles(QStringList() << fileName);
    curDir.remove(zipName);
}
Beispiel #7
0
void TestQuaZipDir::entryInfoList()
{
    QString zipName = "entryInfoList.zip";
    QStringList fileNames;
    fileNames << "test.txt";
    if (!createTestFiles(fileNames)) {
        QFAIL("Couldn't create test files");
    }
    if (!createTestArchive(zipName, fileNames)) {
        QFAIL("Couldn't create test archive");
    }
    removeTestFiles(fileNames);
    QuaZip zip(zipName);
    QDir curDir;
    QVERIFY(zip.open(QuaZip::mdUnzip));
    QuaZipDir dir(&zip, "/");
    QCOMPARE(dir.entryInfoList().size(), 1);
    QCOMPARE(dir.entryInfoList64().size(), 1);
    zip.close();
    curDir.remove(zipName);
}
Beispiel #8
0
void TestQuaZipDir::cd()
{
    QFETCH(QString, zipName);
    QFETCH(QStringList, fileNames);
    QFETCH(QString, dirName);
    QFETCH(QString, targetDirName);
    QFETCH(QString, result);
    QDir curDir;
    if (!createTestFiles(fileNames)) {
        QFAIL("Couldn't create test files");
    }
    if (!createTestArchive(zipName, fileNames)) {
        QFAIL("Couldn't create test archive");
    }
    removeTestFiles(fileNames);
    QuaZip zip(zipName);
    QVERIFY(zip.open(QuaZip::mdUnzip));
    QuaZipDir dir(&zip, dirName);
    QVERIFY(dir.cd(targetDirName));
    QCOMPARE(dir.path(), result);
    zip.close();
    curDir.remove(zipName);
}
Beispiel #9
0
void TestQuaZipDir::filePath()
{
    QString zipName = "entryInfoList.zip";
    QStringList fileNames;
    fileNames << "root.txt" << "dir/test.txt";
    if (!createTestFiles(fileNames)) {
        QFAIL("Couldn't create test files");
    }
    if (!createTestArchive(zipName, fileNames)) {
        QFAIL("Couldn't create test archive");
    }
    removeTestFiles(fileNames);
    QuaZip zip(zipName);
    QDir curDir;
    QVERIFY(zip.open(QuaZip::mdUnzip));
    QuaZipDir dir(&zip);
    QVERIFY(dir.cd("dir"));
    QCOMPARE(dir.relativeFilePath("/root.txt"),
             QString::fromLatin1("../root.txt"));
    QCOMPARE(dir.filePath("test.txt"),
             QString::fromLatin1("dir/test.txt"));
    zip.close();
    curDir.remove(zipName);
}
void TestQuaZipNewInfo::setFileNTFSTimes()
{
    QString zipName = "newtimes.zip";
    QStringList testFiles;
    testFiles << "test.txt";
    QDir curDir;
    if (curDir.exists(zipName)) {
        if (!curDir.remove(zipName))
            QFAIL("Can't remove zip file");
    }
    if (!createTestFiles(testFiles)) {
        QFAIL("Can't create test file");
    }
    QDateTime base(QDate(1601, 1, 1), QTime(0, 0), Qt::UTC);
    quint64 mTicks, aTicks, cTicks;
    {
        // create
        QuaZip zip(zipName);
        QVERIFY(zip.open(QuaZip::mdCreate));
        QuaZipFile zipFile(&zip);
        QFileInfo fileInfo("tmp/test.txt");
        mTicks = base.msecsTo(fileInfo.lastModified()) * 10000;
        aTicks = base.msecsTo(fileInfo.lastRead()) * 10000;
        cTicks = base.msecsTo(fileInfo.created()) * 10000;
        QuaZipNewInfo newInfo("test.txt", "tmp/test.txt");
        newInfo.setFileNTFSTimes("tmp/test.txt");
        QVERIFY(zipFile.open(QIODevice::WriteOnly, newInfo));
        zipFile.close();
        zip.close();
    }
    {
        // check
        QuaZip zip(zipName);
        QVERIFY(zip.open(QuaZip::mdUnzip));
        zip.goToFirstFile();
        QuaZipFileInfo64 fileInfo;
        QVERIFY(zip.getCurrentFileInfo(&fileInfo));
        zip.close();
        QByteArray &extra = fileInfo.extra;
        bool ntfsFound = false;
        int timesFound = 0;
        for (int i = 0; i <= extra.size() - 4; ) {
            unsigned type = static_cast<unsigned>(static_cast<unsigned char>(
                                                      extra.at(i)))
                    | (static_cast<unsigned>(static_cast<unsigned char>(
                                                 extra.at(i + 1))) << 8);
            i += 2;
            unsigned length = static_cast<unsigned>(static_cast<unsigned char>(
                                                        extra.at(i)))
                    | (static_cast<unsigned>(static_cast<unsigned char>(
                                                 extra.at(i + 1))) << 8);
            i += 2;
            if (type == 0x000Au && length >= 32) {
                ntfsFound = true;
                i += 4; // reserved
                while (i <= extra.size() - 4) {
                    unsigned tag = static_cast<unsigned>(
                                static_cast<unsigned char>(extra.at(i)))
                            | (static_cast<unsigned>(
                                   static_cast<unsigned char>(extra.at(i + 1)))
                               << 8);
                    i += 2;
                    unsigned tagsize = static_cast<unsigned>(
                                static_cast<unsigned char>(extra.at(i)))
                            | (static_cast<unsigned>(
                                   static_cast<unsigned char>(extra.at(i + 1)))
                               << 8);
                    i += 2;
                    QCOMPARE(tagsize, static_cast<unsigned>(24));
                    if (tag == 0x0001u && tagsize >= 24) {
                        for (int timesPos = i;
                             i < timesPos + 24;
                             i += 8, tagsize -= 8) {
                            quint64 time = static_cast<quint64>(
                                static_cast<unsigned char>(extra.at(i)))
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 1))) << 8)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 2))) << 16)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 3))) << 24)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 4))) << 32)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 5))) << 40)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 6))) << 48)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 7))) << 56);
                            ++timesFound;
                            if (i - timesPos == 0) {
                                QCOMPARE(time, mTicks);
                            } else if (i - timesPos == 8) {
                                QCOMPARE(time, aTicks);
                            } else if (i - timesPos == 16) {
                                QCOMPARE(time, cTicks);
                            } else {
                                QFAIL("Wrong position");
                            }
                        }
                        i += tagsize;
                    } else {
                        i += tagsize;
                    }

                }
            } else {
                i += length;
            }
        }
        QVERIFY(ntfsFound);
        QCOMPARE(timesFound, 3);
    }
    removeTestFiles(testFiles);
    curDir.remove(zipName);
}