Qt 自带的压缩QZipWriter和解压QZipReader详解~含Demo

目录

前言:

一、压缩

1. QZipWriter详解

1.1 构造方法:

1.2 设置压缩权限:

1.3 添加文件:

1.4 添加目录:

2. 代码组织

2.1 pro添加gui-private

2.2 头文件

2.3 代码分享

 3.压缩Demo下载

二、解压

 1、 代码组织

 2、demo分享


前言:

        QZipWriter是用来压缩文件的类,需要在安装Qt是选择Sources源码安装。否则,无法使用QZipWriter。

Qt 自带的压缩QZipWriter和解压QZipReader详解~含Demo_第1张图片

一、压缩

1. QZipWriter详解

1.1 构造方法:

QZipWriter(const QString &fileName, QIODevice::OpenMode mode = (QIODevice::WriteOnly | QIODevice::Truncate) );
QZipWriter(QIODevice *device);

        fileName:保存zip文件的地址。

        QIODevice 是 QFile 的父类,因此在构造 QZipWriter类的对象时,可以传递一个 QFile 类的对象。

1.2 设置压缩权限:

enum CompressionPolicy {
     AlwaysCompress,
     NeverCompress,
     AutoCompress
};
void setCompressionPolicy(CompressionPolicy policy);
CompressionPolicy compressionPolicy() const;

1.3 添加文件:

 void addFile(const QString &fileName, const QByteArray &data);

1.4 添加目录:

void addDirectory(const QString &dirName);

 如果需要将文件压缩到lib目录下,那么可以这样操作:

  writer->addDirectory("lib");
  writer->addFile("lib/" + fileName, const QByteArray &data);

 这样压缩,就可以在压缩包当中看到lib目录,并且文件在lib目录下。

2. 代码组织

2.1 pro添加gui-private

QT += core gui gui-private

2.2 头文件

#include "QtGui/private/qzipreader_p.h"
#include "QtGui/private/qzipwriter_p.h"

2.3 代码分享

界面:

Qt 自带的压缩QZipWriter和解压QZipReader详解~含Demo_第2张图片

.h文件:

#pragma once

#include 
#include "ui_widget.h"

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_startZipFileBtn_clicked();
    void on_startZipFolderBtn_clicked();
    void on_zipFileBtn_clicked();
    void on_zipPathBtn_clicked();

private:
    Ui::Widget ui;

    QString m_savePath;
    QString m_zipFile;
    QString m_zipFolder;

};

.cpp文件:

#include "widget.h"

#include "QtGui/private/qzipreader_p.h"
#include "QtGui/private/qzipwriter_p.h"

#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent) : QWidget(parent)
{
    ui.setupUi(this);
}

Widget::~Widget(){}

void Widget::on_zipFileBtn_clicked(){
    m_zipFile = QFileDialog::getOpenFileName(this, tr("选择压缩文件"),
                                                    QDir::currentPath(),
                                                    tr("*.*"));

    if (m_zipFile.isEmpty()) {
        QMessageBox::about(this, "提示", "压缩文件没有选择,请重新选择!");
    }
}

void Widget::on_zipPathBtn_clicked(){
    QString desktop_path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    m_savePath = QFileDialog::getExistingDirectory(this, tr("选择保存路径"),
                                                    desktop_path,
                                                    QFileDialog::ShowDirsOnly
                                                    | QFileDialog::DontResolveSymlinks);

    if (m_savePath.isEmpty()) {
        QMessageBox::about(this, "提示", "保存路径为空,请重新选择!");
    }
}

void Widget::on_startZipFileBtn_clicked(){
    if (m_zipFile.isEmpty()) {
        QMessageBox::about(this, "提示", "压缩文件没有选择,请重新选择!");
    }

    if (m_savePath.isEmpty()) {
        QMessageBox::about(this, "提示", "保存路径为空,请重新选择!");
    }

    QZipWriter* writer = new QZipWriter(m_savePath + "/QtZip1.zip");
    QZipWriter::CompressionPolicy policy = writer->compressionPolicy();

    qDebug()<< "policy:" << policy;
    writer->setCompressionPolicy(QZipWriter::AutoCompress);

    QFile file(m_zipFile);
    if (!file.exists()) {
        return;
    }
    QFileInfo fileInfo(m_zipFile);

    file.open(QIODevice::ReadOnly);
    writer->addFile(fileInfo.fileName(), file.readAll());
    file.close();

    if (writer) {
        delete writer;
        writer = NULL;
    }
}
void Widget::on_startZipFolderBtn_clicked(){
    if (m_zipFile.isEmpty()) {
        QMessageBox::about(this, "提示", "压缩文件没有选择,请重新选择!");
    }

    if (m_savePath.isEmpty()) {
        QMessageBox::about(this, "提示", "保存路径为空,请重新选择!");
    }

    QZipWriter* writer = new QZipWriter(m_savePath + "/QtZip2.zip");

    QFile file(m_zipFile);
    if (!file.exists()) {
        return;
    }
    QFileInfo fileInfo(m_zipFile);
    file.open(QIODevice::ReadOnly);

    writer->addDirectory("lib");
    writer->addFile("lib/" + fileInfo.fileName(), file.readAll());

    file.close();
    writer->close();
    if (writer) {
        delete writer;
        writer = NULL;
    }
}

 3.压缩Demo下载

    demo提供了QtCreator工程。下载即可编译使用。

    0积分免费下载地址:Qt自带的压缩QZipWriterDemo-C++文档类资源-CSDN下载1、完整的压缩Demo2、QtCreator工程3、demo详情,请参见博客:https://b更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/ManagerUser/85688586

二、解压

         解压主要注意的是:将文件内容读出来,写到新建文件中去,才能正确的解压出来文件。否则,会出现解压文件大小为0情况。

         本代码实现了对文件和文件夹的解压。demo中对文件夹的解压没有更新进去,但可以参考博客代码。

 1、 代码组织

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 

#include 
#include 
#include 

#pragma execution_character_set("utf-8")

bool CreateFolder(const QString& folderPath)
{
    bool state = false;

    //创建文件夹
    QDir dir(folderPath);
    if (!dir.exists())
    {
        return dir.mkdir(folderPath);
    }else{
        state = true;
    }

    return state;
}

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

void MainWindow::zipReader(QString filepath, QString savepath)
{
    QZipReader zipreader(filepath);
    //reader.extractAll(path); // //可加可不加没有什么影响

    for(auto fileInfo : zipreader.fileInfoList()){
        if(fileInfo.isFile){
            QFile file(savepath + "/" + fileInfo.filePath);
            file.open(QIODevice::WriteOnly);
            //注意编码问题
            QByteArray dt = fileInfo.filePath.toUtf8();
            QString strtemp = QString::fromLocal8Bit(dt);

            QByteArray array = zipreader.fileData(strtemp);
            file.write(array);
            file.close();
        }else if(fileInfo.isDir){
            CreateFolder(savepath + "/" + fileInfo.filePath);
        }
    }

    zipreader.close();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_zipPathBtn_clicked(){
    QString documentPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("选择zip文件"),
                                                    documentPath,
                                                    tr("zip (*.zip *.sanupkg)"));
    if(fileName.isEmpty()){
        return;
    }

    ui->zipPathLineEdit->setText(fileName);
}

void MainWindow::on_savePathBtn_clicked(){
    QString documentPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString folderPath = QFileDialog::getExistingDirectory(this, "选择保存路径", documentPath);
    if(folderPath.isEmpty()){
        return;
    }

    ui->savePathLineEdit->setText(folderPath);
}

void MainWindow::on_zipReaderBtn_clicked(){
    QString zipPath = ui->zipPathLineEdit->text();
    QString savePath = ui->savePathLineEdit->text();

    zipReader(zipPath, savePath);
}

 2、demo分享

        代码非常简单,但是在网上参考了很多博客,写的太烂了,骗取人积分,先免费分享出来。

https://download.csdn.net/download/ManagerUser/87462313https://download.csdn.net/download/ManagerUser/87462313

你可能感兴趣的:(Qt专栏,Qt,QZipWriter,QZipReader)