Qt-QtZipWriter压缩文件与解压文件(base64加密)

 

.pro

 1 QT       += core gui
 2 QT       += gui-private
 3 QT       += network
 4 
 5 
 6 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 7 
 8 CONFIG += c++11
 9 
10 # The following define makes your compiler emit warnings if you use
11 # any Qt feature that has been marked deprecated (the exact warnings
12 # depend on your compiler). Please consult the documentation of the
13 # deprecated API in order to know how to port your code away from it.
14 DEFINES += QT_DEPRECATED_WARNINGS
15 
16 # You can also make your code fail to compile if it uses deprecated APIs.
17 # In order to do so, uncomment the following line.
18 # You can also select to disable deprecated APIs only up to a certain version of Qt.
19 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
20 
21 SOURCES += \
22     ZZipWriter.cpp \
23     main.cpp \
24     mainwindow.cpp
25 
26 HEADERS += \
27     ZZipWriter.h \
28     mainwindow.h
29 
30 FORMS += \
31     mainwindow.ui
32 
33 # Default rules for deployment.
34 qnx: target.path = /tmp/$${TARGET}/bin
35 else: unix:!android: target.path = /opt/$${TARGET}/bin
36 !isEmpty(target.path): INSTALLS += target

 

 

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }

 

mainwindow.h 

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 #include <ZZipWriter.h>
 7 
 8 QT_BEGIN_NAMESPACE
 9 namespace Ui { class MainWindow; }
10 QT_END_NAMESPACE
11 
12 class MainWindow : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     MainWindow(QWidget *parent = nullptr);
18     ~MainWindow();
19 
20 private slots:
21     void on_pushButton_clicked();
22 
23     void on_pushButton_2_clicked();
24 
25     void on_pushButton_3_clicked();
26 
27     void on_pushButton_4_clicked();
28 
29 private:
30     Ui::MainWindow *ui;
31 };
32 #endif // MAINWINDOW_H

 

 

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     this->setWindowTitle(QStringLiteral("QtZipWriter压缩文件与解压文件"));
10 }
11 
12 MainWindow::~MainWindow()
13 {
14     delete ui;
15 }
16 
17 
18 void MainWindow::on_pushButton_clicked()
19 {
20     QString folderPath = "C:/zhujq/testzip"; // 文件夹路径
21     QString zipFilePath = "C:/zhujq/"; // 输出ZIP文件路径
22 
23     ZJQZipWriter *ZZipWriter = new ZJQZipWriter(nullptr);
24     ZZipWriter->CompressionFile(folderPath, zipFilePath);
25 }
26 
27 void MainWindow::on_pushButton_2_clicked()
28 {
29     QString filePath = "C:/zhujq/testzip.zjq";
30     QString zipPath = "C:/zhujq/testzip2";
31 
32     ZJQZipWriter *ZZipWriter = new ZJQZipWriter(nullptr);
33     ZZipWriter->DecompressFile(filePath, zipPath);
34 }
35 
36 void MainWindow::on_pushButton_3_clicked()
37 {
38     QString zjqath = "C:/zhujq/123.zjq"; // 文件
39     QString encryptPath = "C:/zhujq/123m.zjq"; // 输出加密文件路径
40 
41     ZJQZipWriter *ZZipWriter = new ZJQZipWriter(nullptr);
42     ZZipWriter->base64FileEncrypt(zjqath, encryptPath);
43 }
44 
45 void MainWindow::on_pushButton_4_clicked()
46 {
47     QString zjqath = "C:/zhujq/123m.zjq"; // 加密文件
48     QString encryptPath = "C:/zhujq/123J.zjq"; // 输出解密文件路径
49 
50     ZJQZipWriter *ZZipWriter = new ZJQZipWriter(nullptr);
51     ZZipWriter->base64FileDecrypt(zjqath, encryptPath);
52 }

 

 

mainwindow.ui

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <ui version="4.0">
  3  <class>MainWindow</class>
  4  <widget class="QMainWindow" name="MainWindow">
  5   <property name="geometry">
  6    <rect>
  7     <x>0</x>
  8     <y>0</y>
  9     <width>384</width>
 10     <height>189</height>
 11    </rect>
 12   </property>
 13   <property name="windowTitle">
 14    <string>MainWindow</string>
 15   </property>
 16   <widget class="QWidget" name="centralwidget">
 17    <layout class="QVBoxLayout" name="verticalLayout_3">
 18     <item>
 19      <layout class="QHBoxLayout" name="horizontalLayout">
 20       <item>
 21        <layout class="QVBoxLayout" name="verticalLayout">
 22         <item>
 23          <widget class="QLineEdit" name="lineEdit">
 24           <property name="text">
 25            <string>C:/zhujq/testzip</string>
 26           </property>
 27          </widget>
 28         </item>
 29         <item>
 30          <widget class="QLineEdit" name="lineEdit_2">
 31           <property name="text">
 32            <string>C:/zhujq/</string>
 33           </property>
 34          </widget>
 35         </item>
 36        </layout>
 37       </item>
 38       <item>
 39        <layout class="QVBoxLayout" name="verticalLayout_4">
 40         <item>
 41          <widget class="QPushButton" name="pushButton">
 42           <property name="text">
 43            <string>压缩</string>
 44           </property>
 45          </widget>
 46         </item>
 47         <item>
 48          <widget class="QPushButton" name="pushButton_3">
 49           <property name="text">
 50            <string>加密</string>
 51           </property>
 52          </widget>
 53         </item>
 54        </layout>
 55       </item>
 56      </layout>
 57     </item>
 58     <item>
 59      <layout class="QHBoxLayout" name="horizontalLayout_2">
 60       <item>
 61        <layout class="QVBoxLayout" name="verticalLayout_2">
 62         <item>
 63          <widget class="QLineEdit" name="lineEdit_3">
 64           <property name="text">
 65            <string>C:/zhujq/testzip.zip</string>
 66           </property>
 67          </widget>
 68         </item>
 69         <item>
 70          <widget class="QLineEdit" name="lineEdit_4">
 71           <property name="text">
 72            <string>C:/zhujq/testzip2</string>
 73           </property>
 74          </widget>
 75         </item>
 76        </layout>
 77       </item>
 78       <item>
 79        <layout class="QVBoxLayout" name="verticalLayout_5">
 80         <item>
 81          <widget class="QPushButton" name="pushButton_2">
 82           <property name="text">
 83            <string>解压</string>
 84           </property>
 85          </widget>
 86         </item>
 87         <item>
 88          <widget class="QPushButton" name="pushButton_4">
 89           <property name="text">
 90            <string>解密</string>
 91           </property>
 92          </widget>
 93         </item>
 94        </layout>
 95       </item>
 96      </layout>
 97     </item>
 98    </layout>
 99   </widget>
100  </widget>
101  <resources/>
102  <connections/>
103 </ui>
View Code

 

 

ZZipWriter.h 

 1 #ifndef ZZIPWRITER_H
 2 #define ZZIPWRITER_H
 3 
 4 #include <QObject>
 5 #include <QtGui/private/qzipreader_p.h>
 6 #include <QtGui/private/qzipwriter_p.h>
 7 #include <QDir>
 8 #include <QFileInfo>
 9 #include <QFile>
10 #include <QDebug>
11 // 加密
12 #include <QFile>
13 #include <QByteArray>
14 #include <QDebug>
15 #include <QCryptographicHash>
16 
17 class ZJQZipWriter : public QObject
18 {
19 public:
20     ZJQZipWriter(QObject *parent = nullptr);
21 
22     void CompressionFile(QString srcFolderPath, QString destFolderPath);
23     QFileInfoList ErgodicFile(QZipWriter *writer, QString rootPath, QString filePath);
24 
25 
26    void DecompressFile(QString srcFile, QString destFolderPath);
27    // 加密
28    QByteArray base64Encrypt(const QByteArray &data);// 加密
29    QByteArray base64Decrypt(const QByteArray &data);// 解密
30    bool base64FileEncrypt(const QString &inputPath, const QString &outputPath);
31    bool base64FileDecrypt(const QString &inputPath, const QString &outputPath);
32 private:
33    QString mFileSuffix = ".zjq";// 文件后缀
34    QString mSuffix = "zjq";// 后缀
35 
36 
37 };
38 
39 #endif // ZZIPWRITER_H

 

 

ZZipWriter.cpp 

  1 #include "ZZipWriter.h"
  2 
  3 ZJQZipWriter::ZJQZipWriter(QObject *parent)
  4     :QObject(parent)
  5 {
  6 
  7 }
  8 
  9 void ZJQZipWriter::CompressionFile(QString srcFolderPath, QString destFolderPath)
 10 {
 11        if(srcFolderPath == nullptr || destFolderPath == nullptr)
 12        {
 13            // 路径为空
 14            return;
 15        }
 16        QFileInfo fileDistInfo(destFolderPath);
 17        if(fileDistInfo.isFile())   //目的路径不能为文件名
 18        {
 19            // 路径不能为文件
 20            return;
 21        }
 22        QFileInfo fileInfo(srcFolderPath);
 23        if(fileInfo.isFile()) // 压缩的是一个文件
 24        {
 25            QString saveFileName = destFolderPath + "/" + fileInfo.baseName() + mFileSuffix; // ".zip";
 26            QZipWriter *oZipWriter = new QZipWriter(saveFileName);
 27            QFile file(srcFolderPath);
 28            if(!file.open(QIODevice::ReadOnly))
 29            {
 30                // 文件打开失败
 31                return;
 32            }
 33            oZipWriter->addFile(file.fileName(), file.readAll());
 34            oZipWriter->close();
 35            delete oZipWriter;
 36            oZipWriter = nullptr;
 37        }
 38        else    //压缩的是一个文件夹
 39        {
 40            QString folderStr = srcFolderPath.mid(srcFolderPath.lastIndexOf("/")).remove("/");
 41            if(folderStr == nullptr)
 42            {
 43                // 压缩文件路径错误
 44                return;
 45            }
 46            QString saveFileName = destFolderPath+"/"+folderStr + mFileSuffix; // ".zip";
 47            QZipWriter *oZipWriter = new QZipWriter(saveFileName);
 48            oZipWriter->addDirectory(folderStr);
 49            ErgodicFile(oZipWriter, folderStr, srcFolderPath);
 50            delete oZipWriter;
 51            oZipWriter = nullptr;
 52        }
 53 }
 54 
 55 QFileInfoList ZJQZipWriter::ErgodicFile(QZipWriter *writer, QString rootPath, QString filePath)
 56 {
 57     QDir dir(filePath);        //遍历各级子目录
 58     QDir fileDir(filePath);    //遍历子目录中所有文件
 59 
 60     //先遍历文件夹 添加进widget
 61     QFileInfoList fileList=dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
 62     QFileInfoList folderList = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);   //获取当前所有目录 QDir::Dirs 0x001 列出目录;
 63 
 64     for(int i = 0; i != folderList.size(); i++)         //自动递归添加各目录到上一级目录
 65     {
 66         QString namepath = folderList.at(i).absoluteFilePath();    //获取路径
 67         QFileInfo folderinfo= folderList.at(i);
 68         QString name=folderinfo.fileName();      //获取目录名
 69 
 70 
 71         QStringList list = folderinfo.absoluteFilePath().split(rootPath);
 72         //qDebug()<<"Dir:"<<folderinfo.absoluteFilePath().remove(list.at(0));
 73         writer->addDirectory(folderinfo.absoluteFilePath().remove(list.at(0)));
 74 
 75         QFileInfoList child_file_list = ErgodicFile(writer,rootPath,namepath);          //进行递归 递归这个文件夹
 76         fileList.append(child_file_list);
 77         //  file_list.append(name);
 78 
 79     }
 80     /*添加path路径文件*/
 81 
 82     fileDir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);        //获取当前所有文件
 83     fileDir.setSorting(QDir::Name);
 84     QFileInfoList _fileList = fileDir.entryInfoList();
 85     for (int i = 0; i < _fileList.size(); ++i) //将当前目录中所有文件添加到treewidget中
 86     {
 87         QFileInfo fileInfo = _fileList.at(i);
 88 
 89         QFile file(fileInfo.absoluteFilePath());
 90         if(file.open(QIODevice::ReadOnly))
 91         {
 92             QStringList list = fileInfo.absoluteFilePath().split(rootPath);
 93             //qDebug()<<"File:"<<fileInfo.absoluteFilePath().remove(list.at(0));
 94             writer->addFile(fileInfo.absoluteFilePath().remove(list.at(0)),file.readAll());
 95             file.close();
 96         }
 97 
 98     }
 99     return fileList;
100 }
101 
102 void ZJQZipWriter::DecompressFile(QString srcFile, QString destFolderPath)
103 {
104     if(srcFile == nullptr || destFolderPath == nullptr)
105     {
106         // 路径为空
107         return;
108     }
109     // 处理目录不存在时,创建一个
110     QDir dir;
111     dir.mkpath(destFolderPath);
112     // 判断目录是否存在
113     QFileInfo destInfo(destFolderPath);
114     if(destInfo.isFile()||!destInfo.exists())
115     {
116         // 目的路径异常
117         return;
118     }
119 
120     QFileInfo fileInfo(srcFile);
121     if(!fileInfo.isFile() || fileInfo.suffix() != mSuffix)
122     {
123         // 压缩文件为空
124         return;
125     }
126 
127     QZipReader *oZipReader = new QZipReader(srcFile);
128     bool res = oZipReader->extractAll(destFolderPath);
129     if(res) //解压缩成功
130     {
131          // 解压缩完成
132     }
133     else
134     {
135          // 解压缩失败
136     }
137 
138     oZipReader->close();
139     delete oZipReader;
140     oZipReader = nullptr;
141 }
142 
143 QByteArray ZJQZipWriter::base64Encrypt(const QByteArray &data)
144 {
145     return data.toBase64();
146 }
147 
148 QByteArray ZJQZipWriter::base64Decrypt(const QByteArray &data)
149 {
150     return QByteArray::fromBase64(data);
151 }
152 
153 bool ZJQZipWriter::base64FileEncrypt(const QString &inputPath, const QString &outputPath)
154 {
155     QFile inFile(inputPath);
156     QFile outFile(outputPath);
157     if (!inFile.open(QIODevice::ReadOnly) || !outFile.open(QIODevice::WriteOnly))
158     {
159         return false;
160     }
161 
162     QByteArray fileData = inFile.readAll();
163     QByteArray encryptedData = base64Encrypt(fileData);
164     outFile.write(encryptedData);
165     return true;
166 }
167 
168 bool ZJQZipWriter::base64FileDecrypt(const QString &inputPath, const QString &outputPath)
169 {
170     QFile inFile(inputPath);
171     QFile outFile(outputPath);
172     if (!inFile.open(QIODevice::ReadOnly) || !outFile.open(QIODevice::WriteOnly))
173     {
174         return false;
175     }
176 
177     QByteArray fileData = inFile.readAll();
178     QByteArray oData = base64Decrypt(fileData);
179     outFile.write(oData);
180     return true;
181 }

 

posted on 2026-02-02 15:20  疯狂delphi  阅读(1)  评论(0)    收藏  举报

导航