mirror of
https://github.com/QRouland/UTPass.git
synced 2026-01-10 19:36:57 +00:00
Initial Commit : 0.0.1
This commit is contained in:
62
plugins/Utils/utils.cpp
Normal file
62
plugins/Utils/utils.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QUrl>
|
||||
#include <QtCore/QStandardPaths>
|
||||
#include <quazip5/JlCompress.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
Utils::Utils() {};
|
||||
|
||||
bool Utils::unzip(QUrl zip_url, QString dir_out_path)
|
||||
{
|
||||
auto tmp_dir_path = QStandardPaths::writableLocation(
|
||||
QStandardPaths::CacheLocation).append("/unzip");
|
||||
|
||||
QDir tmp_dir(tmp_dir_path);
|
||||
tmp_dir.removeRecursively();
|
||||
tmp_dir.mkpath(".");
|
||||
|
||||
qDebug() << "Temp dir path is " << tmp_dir_path;
|
||||
auto status = !JlCompress::extractDir(
|
||||
zip_url.toLocalFile(),
|
||||
tmp_dir_path
|
||||
).isEmpty();
|
||||
|
||||
if (!status) {
|
||||
tmp_dir.removeRecursively();
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "Guessing if it should remove a single root folder";
|
||||
QStringList files_in_tmp_dir = tmp_dir.entryList(QDir::AllEntries | QDir::Hidden |
|
||||
QDir::NoDotAndDotDot);
|
||||
|
||||
auto dir_import_path =
|
||||
files_in_tmp_dir.length() == 1 ?
|
||||
tmp_dir_path.append("/" + files_in_tmp_dir.first()) : tmp_dir_path;
|
||||
qDebug() << "Final imported tmp path dir is " << dir_import_path;
|
||||
|
||||
qDebug() << "Removing destination";
|
||||
QDir dir_out(dir_out_path);
|
||||
dir_out.removeRecursively();
|
||||
|
||||
qDebug() << "Moving zip content to destination";
|
||||
QDir dir;
|
||||
qDebug() << dir_import_path << " to " << dir_out_path;
|
||||
auto ret = dir.rename(dir_import_path, dir_out_path);
|
||||
tmp_dir.removeRecursively();;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool Utils::rmFile(QUrl file_url)
|
||||
{
|
||||
return QFile::remove(file_url.toLocalFile());
|
||||
}
|
||||
|
||||
bool Utils::rmDir(QUrl dir_url)
|
||||
{
|
||||
QDir dir(dir_url.toLocalFile());
|
||||
return dir.removeRecursively();
|
||||
}
|
||||
Reference in New Issue
Block a user