summaryrefslogtreecommitdiff
path: root/rockworkd/libpebble/ziphelper.cpp
blob: f18b8aaafe51ac2abba2cd3dba5adac2d121f480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "ziphelper.h"

#include <QFileInfo>
#include <QDebug>
#include <QDir>

#include <quazip/quazipfile.h>
#include <quazip/quazip.h>

ZipHelper::ZipHelper()
{

}

bool ZipHelper::unpackArchive(const QString &archiveFilename, const QString &targetDir)
{
    QuaZip zipFile(archiveFilename);
    if (!zipFile.open(QuaZip::mdUnzip)) {
        qWarning() << "Failed to open zip file" << zipFile.getZipName();
        return false;
    }

    foreach (const QuaZipFileInfo &fi, zipFile.getFileInfoList()) {
        QuaZipFile f(archiveFilename, fi.name);
        if (!f.open(QFile::ReadOnly)) {
            qWarning() << "could not extract file" << fi.name;
            return false;
        }
        if (fi.name.endsWith("/")) {
            qDebug() << "skipping" << fi.name;
            continue;
        }
        qDebug() << "Inflating:" << fi.name;
        QFileInfo dirInfo(targetDir + "/" + fi.name);
        if (!dirInfo.absoluteDir().exists() && !dirInfo.absoluteDir().mkpath(dirInfo.absolutePath())) {
            qWarning() << "Error creating target dir" << dirInfo.absoluteDir();
            return false;
        }
        QFile of(targetDir + "/" + fi.name);
        if (!of.open(QFile::WriteOnly | QFile::Truncate)) {
            qWarning() << "Could not open output file for writing" << fi.name;
            f.close();
            return false;
        }
        of.write(f.readAll());
        f.close();
        of.close();
    }
    return true;
}

bool ZipHelper::packArchive(const QString &archiveFilename, const QString &sourceDir)
{
    QuaZip zip(archiveFilename);
    if (!zip.open(QuaZip::mdCreate)){
        qWarning() << "Error creating zip file";
        return false;
    }

    QDir dir(sourceDir);
    QuaZipFile outfile(&zip);

    foreach (const QFileInfo &fi, dir.entryInfoList()) {
        if (!fi.isFile()) {
            continue;
        }
        qDebug() << "have file" << fi.absoluteFilePath();
        QuaZipNewInfo newInfo(fi.fileName(), fi.absoluteFilePath());

        if (!outfile.open(QFile::WriteOnly, newInfo)) {
            qWarning() << "Error opening zipfile for writing";
            zip.close();
            return false;
        }

        QFile sourceFile(fi.absoluteFilePath());
        if (!sourceFile.open(QFile::ReadOnly)) {
            qWarning() << "Error opening log file for reading" << fi.absoluteFilePath();
            outfile.close();
            zip.close();
            return false;
        }
        outfile.write(sourceFile.readAll());
        outfile.close();
        sourceFile.close();

    }
    outfile.close();
    zip.close();
    return true;
}