zip档案
更多...
#include <DAZipArchive.h>
|
|
virtual void | saveAll (const QString &filePath) override |
| |
|
virtual void | loadAll (const QString &filePath) override |
| |
|
virtual void | saveAll (const QString &filePath)=0 |
| |
|
virtual void | loadAll (const QString &filePath)=0 |
| |
|
|
| DAZipArchive (QObject *par=nullptr) |
| |
|
| DAZipArchive (const QString &zipPath, QObject *par=nullptr) |
| |
| virtual bool | setBaseFilePath (const QString &path) override |
| | 设置基础路径
|
| |
|
bool | setZipFileName (const QString &fileName) |
| |
| bool | open () |
| | 打开一个压缩包,主要为了读取
|
| |
| bool | create () |
| | 创建一个压缩包
|
| |
| bool | isOpened () const |
| | 判断是否打开文件
|
| |
|
bool | close () |
| |
| bool | write (const QString &relatePath, const QByteArray &byte) override |
| | 写数据
|
| |
| bool | writeFileToZip (const QString &relatePath, const QString &localFilePath, std::size_t chunk_mb=4) |
| | 将本地文件写入ZIP压缩包中的指定路径
|
| |
| QByteArray | read (const QString &relatePath) override |
| | 读数据
|
| |
|
bool | readToFile (const QString &zipRelatePath, const QString &localFilePath, std::size_t chunk_mb=4) |
| |
| bool | remove (const QString &fileToRemove) override |
| | 删除文件
|
| |
| QStringList | getAllFiles () const |
| | 获取所有文件
|
| |
| bool | contains (const QString &relatePath) const |
| | 检查文件是否存在
|
| |
| qint64 | getFileSize () const |
| | 获取文件大小
|
| |
| int | getFileCount () const |
| | 获取文件数量,此函数会遍历zip的所有文件
|
| |
| QString | getComment () const |
| | 获取注释内容
|
| |
|
void | setComment (const QString &comment) |
| |
| bool | extractToDirectory (const QString &extractDir) |
| | 压缩包整体解压到目录下
|
| |
|
bool | compressDirectory (const QString &folderPath) |
| |
|
QStringList | getFileNameList () const |
| |
| QStringList | getFolderFileNameList (const QString &zipFolderPath) const |
| | 获取zip中一个文件夹下的所有文件列表
|
| |
|
QuaZip * | quazip () const |
| |
| QString | getLastErrorString () const |
| | 获取最后的错误内容
|
| |
|
| DAAbstractArchive (QObject *par=nullptr) |
| |
|
QString | getBaseFilePath () const |
| |
|
void | appendTask (const std::shared_ptr< DAAbstractArchiveTask > &task) |
| |
|
int | getTaskCount () const |
| |
|
bool | isTaskQueueEmpty () const |
| |
|
| static bool | isCorrectFile (const QString &filePath) |
| | 判断是否是正确的工程
|
| |
|
static bool | extractToDirectory (const QString &zipFilePath, const QString &extractDir) |
| |
|
static bool | extractToDirectory (QuaZip *zip, const QString &extractDir) |
| |
|
static bool | compressDirectory (const QString &folderPath, const QString &zipFilePath) |
| |
|
static bool | compressDirectory (const QString &folderPath, QuaZip *zip, const QString &relativeBase=QString("./")) |
| |
| static bool | writeFileToZip (QuaZip *zip, const QString &relatePath, const QString &localFilePath, std::size_t chunk_mb=4) |
| | 将本地文件写入ZIP压缩包中的指定路径
|
| |
| static bool | readToFile (QuaZip *zip, const QString &zipRelatePath, const QString &localFilePath, std::size_t chunk_mb=4) |
| | 从 ZIP 归档中提取指定文件到本地路径
|
| |
| static QString | toTemporaryPath (const QString &path) |
| | 转换为临时路径
|
| |
| static bool | replaceFile (const QString &file, const QString &beReplaceFile) |
| | 替换文件
|
| |
◆ contains()
| bool DA::DAZipArchive::contains |
( |
const QString & |
relatePath | ) |
const |
◆ create()
| bool DA::DAZipArchive::create |
( |
| ) |
|
◆ extractToDirectory()
| bool DA::DAZipArchive::extractToDirectory |
( |
const QString & |
extractDir | ) |
|
◆ getAllFiles()
| QStringList DA::DAZipArchive::getAllFiles |
( |
| ) |
const |
◆ getComment()
| QString DA::DAZipArchive::getComment |
( |
| ) |
const |
◆ getFileCount()
| int DA::DAZipArchive::getFileCount |
( |
| ) |
const |
◆ getFileSize()
| qint64 DA::DAZipArchive::getFileSize |
( |
| ) |
const |
◆ getFolderFileNameList()
| QStringList DA::DAZipArchive::getFolderFileNameList |
( |
const QString & |
zipFolderPath | ) |
const |
获取zip中一个文件夹下的所有文件列表
- 注解
- 注意不包括这个文件下的子目录及子目录下的文件
- 参数
-
- 返回
◆ getLastErrorString()
| QString DA::DAZipArchive::getLastErrorString |
( |
| ) |
const |
◆ isCorrectFile()
| bool DA::DAZipArchive::isCorrectFile |
( |
const QString & |
filePath | ) |
|
|
static |
◆ isOpened()
| bool DA::DAZipArchive::isOpened |
( |
| ) |
const |
◆ open()
| bool DA::DAZipArchive::open |
( |
| ) |
|
◆ read()
| QByteArray DA::DAZipArchive::read |
( |
const QString & |
relatePath | ) |
|
|
overridevirtual |
◆ readToFile()
| bool DA::DAZipArchive::readToFile |
( |
QuaZip * |
zip, |
|
|
const QString & |
zipRelatePath, |
|
|
const QString & |
localFilePath, |
|
|
std::size_t |
chunk_mb = 4 |
|
) |
| |
|
static |
从 ZIP 归档中提取指定文件到本地路径
- 参数
-
| [in] | zip | 已打开的 QuaZip 实例指针,需确保在函数调用期间保持打开状态 |
| [in] | zipRelatePath | ZIP 归档内的相对文件路径(例如:"documents/report.pdf") |
| [in] | localFilePath | 要写入的本地文件绝对路径(例如:"/home/user/report.pdf") |
| [in] | chunk_mb | 读写缓冲区大小(单位:MB),建议值 1-64,默认 4MB |
- 返回
- bool
- 返回值
-
| true | 文件提取成功 |
| false | 提取失败,可能原因包括:
- 参数无效
- ZIP 内找不到指定文件
- 磁盘空间不足
- 文件读写错误
|
- 注解
- 函数特性:
- 支持大文件处理(恒定内存占用)
- 自动创建目标目录结构
- 失败时自动清理不完整文件
- 线程安全性:非线程安全,需外部同步
- 警告
- 需确保传入的 QuaZip 实例已处于打开状态(QuaZip::mdUnzip 模式)
- chunk_mb 值过大会增加内存占用,过小会影响IO效率
◆ remove()
| bool DA::DAZipArchive::remove |
( |
const QString & |
fileToRemove | ) |
|
|
overridevirtual |
◆ setBaseFilePath()
| bool DA::DAZipArchive::setBaseFilePath |
( |
const QString & |
path | ) |
|
|
overridevirtual |
设置基础路径
对于文件来说,基础路径就是要保存的目录,所有文件的写入都基于此目录,对应zip来说,这个路径就是zip文件路径
- 注解
- 对于一些单文件类操作,例如zip文件,设置路径后需要打开文件的,应该继承此函数,把打开的操作也叫上,例如:
- 参数
-
重载 DA::DAAbstractArchive .
◆ write()
| bool DA::DAZipArchive::write |
( |
const QString & |
relatePath, |
|
|
const QByteArray & |
byte |
|
) |
| |
|
overridevirtual |
◆ writeFileToZip() [1/2]
| bool DA::DAZipArchive::writeFileToZip |
( |
const QString & |
relatePath, |
|
|
const QString & |
localFilePath, |
|
|
std::size_t |
chunk_mb = 4 |
|
) |
| |
将本地文件写入ZIP压缩包中的指定路径
本函数将本地文件(localFilePath)的内容写入到已打开的ZIP对象(zip)中, 文件在ZIP内的路径由relatePath指定。函数会根据文件大小自动选择一次性写入或分块写入策略, 以避免内存占用过高,并确保大文件处理的稳定性。
- 参数
-
| [in] | relatePath | 文件在ZIP内的相对路径(如:"doc/report.txt"), 路径格式不应以斜杠开头,目录层级使用正斜杠"/"分隔。 |
| [in] | localFilePath | 待写入的本地文件路径,必须为有效可读的常规文件, 符号链接或其他特殊文件将被拒绝。 |
| [in] | chunk_mb | 分块大小,默认为4mb |
- 返回
- bool
- true : 文件成功写入ZIP
- false : 失败(可能原因:文件不存在/不可读、ZIP写入错误、内存不足等)
- 注解
- 分块策略: 文件大小超过chunk_mb MB时启用分块写入,每次读取chunk_mb MB数据以减少内存峰值
- 元数据保留: 使用原文件的修改时间和权限信息(通过QuaZipNewInfo实现)
基本用法示例
QuaZip zip("archive.zip");
zip.open(QuaZip::mdCreate);
if (ok) {
qDebug() << "写入成功";
} else {
qDebug() << "写入失败";
}
bool writeFileToZip(const QString &relatePath, const QString &localFilePath, std::size_t chunk_mb=4)
将本地文件写入ZIP压缩包中的指定路径
Definition DAZipArchive.cpp:363
- 警告
- 需确保:
- ZIP对象已通过open()方法打开且未关闭
- 写入过程中本地文件内容不被修改
- 多线程场景需自行处理同步
◆ writeFileToZip() [2/2]
| bool DA::DAZipArchive::writeFileToZip |
( |
QuaZip * |
zip, |
|
|
const QString & |
relatePath, |
|
|
const QString & |
localFilePath, |
|
|
std::size_t |
chunk_mb = 4 |
|
) |
| |
|
static |
将本地文件写入ZIP压缩包中的指定路径
本函数将本地文件(localFilePath)的内容写入到已打开的ZIP对象(zip)中, 文件在ZIP内的路径由relatePath指定。函数会根据文件大小自动选择一次性写入或分块写入策略, 以避免内存占用过高,并确保大文件处理的稳定性。
- 参数
-
| [in] | zip | 指向已打开的QuaZip对象的指针,函数不会接管该对象的所有权, 调用者需确保zip对象在函数调用期间有效且处于打开状态。 |
| [in] | relatePath | 文件在ZIP内的相对路径(如:"doc/report.txt"), 路径格式不应以斜杠开头,目录层级使用正斜杠"/"分隔。 |
| [in] | localFilePath | 待写入的本地文件路径,必须为有效可读的常规文件, 符号链接或其他特殊文件将被拒绝。 |
| [in] | chunk_mb | 分块大小,默认为4mb |
- 返回
- bool
- true : 文件成功写入ZIP
- false : 失败(可能原因:文件不存在/不可读、ZIP写入错误、内存不足等)
- 注解
- 分块策略: 文件大小超过chunk_mb MB时启用分块写入,每次读取chunk_mb MB数据以减少内存峰值
- 元数据保留: 使用原文件的修改时间和权限信息(通过QuaZipNewInfo实现)
基本用法示例
QuaZip zip("archive.zip");
zip.open(QuaZip::mdCreate);
if (ok) {
qDebug() << "写入成功";
} else {
qDebug() << "写入失败";
}
- 警告
- 需确保:
- ZIP对象已通过open()方法打开且未关闭
- 写入过程中本地文件内容不被修改
- 多线程场景需自行处理同步
该类的文档由以下文件生成:
- /home/runner/work/data-workbench/data-workbench/src/DAGui/DAZipArchive.h
- /home/runner/work/data-workbench/data-workbench/src/DAGui/DAZipArchive.cpp