forked from gongjianbo/MyTestCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
89 lines (82 loc) · 2.57 KB
/
mainwindow.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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QFileDialog>
#include <QFileInfo>
#include <QFile>
#include <QDir>
#include <QDebug>
#include <functional>
#include "ZipTool.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->btnZipFile, &QPushButton::clicked, this, &MainWindow::zipFile);
connect(ui->btnZipDir, &QPushButton::clicked, this, &MainWindow::zipDir);
connect(ui->btnUnzip, &QPushButton::clicked, this, &MainWindow::unzip);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::zipFile()
{
QString select_path = QFileDialog::getOpenFileName(this);
if (select_path.isEmpty())
return;
QZipWriter zip(QApplication::applicationDirPath() + "/test.zip");
QFile f(select_path);
if (f.open(QIODevice::ReadOnly)) {
QFileInfo info(select_path);
zip.addFile(info.fileName(), f.readAll());
f.close();
}
zip.close();
}
void MainWindow::zipDir()
{
QString select_path = QFileDialog::getExistingDirectory(this);
if (select_path.isEmpty())
return;
QZipWriter zip(QApplication::applicationDirPath() + "/test.zip");
// 递归目录
std::function<void(const QString &, const QString &)> find =
[&](const QString &path, const QString &folder)
{
QDir path_dir(path);
path_dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
path_dir.setSorting(QDir::DirsLast);
auto &&info_list = path_dir.entryInfoList();
for (int i = 0; i < info_list.size(); i++)
{
auto &&info = info_list.at(i);
if (info.isDir()) {
// qDebug()<<"dir"<<info.fileName()<<info.filePath();
const QString sub_dir = folder + info.fileName() + "/";
zip.addDirectory(sub_dir);
find(info.filePath(), sub_dir);
} else {
// qDebug()<<"file"<<info.fileName()<<info.filePath();
QFile f(info.filePath());
if (f.open(QIODevice::ReadOnly)) {
zip.addFile(folder + info.fileName(), f.readAll());
f.close();
}
}
}
};
find(select_path, "");
zip.close();
}
void MainWindow::unzip()
{
QString unzip_path = QApplication::applicationDirPath() + "/unzip";
QZipReader zip(QApplication::applicationDirPath() + "/test.zip");
QDir dir(unzip_path);
if (!dir.exists()) {
dir.mkpath(unzip_path);
}
zip.extractAll(unzip_path);
}