forked from palindromiq/YATE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipmanager.cpp
62 lines (53 loc) · 1.87 KB
/
zipmanager.cpp
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
#include <QFile>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include "zipmanager.h"
#include "miniz.h"
namespace Yate {
ZipManager::ZipManager(QObject *parent)
: QObject{parent}
{
}
bool ZipManager::unzip(QString sourceArchive, QString destDir)
{
qDebug() << "Unzipping archive " << sourceArchive << " to " << destDir;
mz_zip_archive zipArchive;
memset(&zipArchive, 0, sizeof(zipArchive));
mz_bool status;
auto sourceArchiveStd = sourceArchive.toStdString();
status = mz_zip_reader_init_file(&zipArchive, sourceArchiveStd.c_str(), 0);
if (!status)
{
qCritical() << "mz_zip_reader_init_file() failed!";
qCritical() << mz_zip_get_error_string(zipArchive.m_last_error);
return false;
}
for (int i = 0; i < (int)mz_zip_reader_get_num_files(&zipArchive); i++)
{
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&zipArchive, i, &file_stat))
{
qCritical() << "mz_zip_reader_file_stat() failed!";
mz_zip_reader_end(&zipArchive);
return false;
}
if (mz_zip_reader_is_file_a_directory(&zipArchive, i)) {
continue;
}
QString strippedFileName = QFileInfo(file_stat.m_filename).fileName();
auto extractionFileNameStd = QString(destDir + QDir::separator() + strippedFileName).toStdString();
status = mz_zip_reader_extract_file_to_file(&zipArchive, file_stat.m_filename, extractionFileNameStd.c_str(), 0);
if (!status)
{
qCritical() << "mz_zip_reader_extract_file_to_file() failed!";
qCritical() << mz_zip_get_error_string(zipArchive.m_last_error);
mz_zip_reader_end(&zipArchive);
return false;
}
}
mz_zip_reader_end(&zipArchive);
qDebug() << "Finished unzipping archive " << sourceArchive << " to " << destDir;
return true;
}
}