QT QZipReader改进,以支持大于2G的zip文件

2024-04-18 20:52

本文主要是介绍QT QZipReader改进,以支持大于2G的zip文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

QZipReader对ZIP文件读取非常方便好用。即使在最新版的QT 6.6.1里,仍然存在一些问题:对于大于2G的zip文件不支持。

虽然有标准zlib可调用,但包装成一个易用且功能成熟的zip解压功能库,还是有很大的工作量,也需要有一定的经验。

于是,直接找到QT的QZipReader相关的源码文件,单独做成一个Compress工具包,方便BUG调试,其次,将来再整合进7z解压等功能。

把原来的两个头文件和一个Cpp文件,整合成两个文件:QZip.h和QZip.cpp。

把QZipReader改名为ZipReader,以避免与QT自带的头文件和DLL产生冲突。

代码工程结构如下图:

代码调试过程,就不截图了。主要修改了两个方法:

1、void ZipReaderPrivate::scanFiles()

把这些变量的类型由int纠正为正确的数据型,这就是不支持超过2G的zip文件原因。

        qint64 i = 0;
    uint start_of_directory = -1;
    ushort num_dir_entries = 0;

说明原来代码还是有一些基本规范质量问题,也不知道是有意的,还是无意的,或者是发现问题了,但没有人手去分析与修改。

2、QByteArray ZipReader::fileData(const QString &fileName)

问题跟前一个方法一样,把一些变量的类型纠正为正确的数据型。

修改后的源代码:

void ZipReaderPrivate::scanFiles()

void ZipReaderPrivate::scanFiles()
{if (!dirtyFileTree)return;if (! (device->isOpen() || device->open(QIODevice::ReadOnly))) {status = ZipReader::FileOpenError;return;}if ((device->openMode() & QIODevice::ReadOnly) == 0) { // only read the index from readable files.status = ZipReader::FileReadError;return;}dirtyFileTree = false;uchar tmp[4];device->read((char *)tmp, 4);if (readUInt(tmp) != 0x04034b50) {qWarning("QZip: not a zip file!");return;}// find EndOfDirectory headerqint64 i = 0;uint start_of_directory = -1;ushort num_dir_entries = 0;EndOfDirectory eod;while (true) {const qint64 pos = device->size() - qint64(sizeof(EndOfDirectory)) - i;if (pos < 0 || i > 65535) {debugx("Zip: EndOfDirectory not found。 ");return;}device->seek(pos);device->read((char *)&eod, sizeof(EndOfDirectory));if (readUInt(eod.signature) == 0x06054b50)break;++i;}// have the eodstart_of_directory = readUInt(eod.dir_start_offset);num_dir_entries = readUShort(eod.num_dir_entries);debugx("start_of_directory at %u, num_dir_entries=%d", start_of_directory, num_dir_entries);int comment_length = readUShort(eod.comment_length);if (comment_length != i)debugx("QZip: failed to parse zip file.");comment = device->read(qMin(comment_length, i));device->seek(start_of_directory);for (i = 0; i < num_dir_entries; ++i) {FileHeader header;auto read = device->read((char *) &header.h, sizeof(CentralFileHeader));if (read < (qint64)sizeof(CentralFileHeader)) {debugx("QZip: Failed to read complete header, index may be incomplete,read:%lld,num_dir_entries:%d",read,num_dir_entries);break;}if (readUInt(header.h.signature) != 0x02014b50) {debugx("QZip: invalid header signature, index may be incomplete");break;}int l = readUShort(header.h.file_name_length);header.file_name = device->read(l);if (header.file_name.size() != l) {debugx("QZip: Failed to read filename from zip index, index may be incomplete");break;}l = readUShort(header.h.extra_field_length);header.extra_field = device->read(l);if (header.extra_field.size() != l) {debugx("QZip: Failed to read extra field in zip file, skipping file, index may be incomplete");break;}l = readUShort(header.h.file_comment_length);header.file_comment = device->read(l);if (header.file_comment.size() != l) {debugx("QZip: Failed to read read file comment, index may be incomplete");break;}//  debugx("found file '%s'", header.file_name.data());fileHeaders.append(header);}
}

QByteArray ZipReader::fileData(const QString &fileName)

QByteArray ZipReader::fileData(const QString &fileName) const
{d->scanFiles();int i;for (i = 0; i < d->fileHeaders.size(); ++i) {if (QString::fromLocal8Bit(d->fileHeaders.at(i).file_name) == fileName)break;}if (i == d->fileHeaders.size())return QByteArray();FileHeader header = d->fileHeaders.at(i);ushort version_needed = readUShort(header.h.version_needed);if (version_needed > ZIP_VERSION) {debugx("QZip: .ZIP specification version %d implementationis needed to extract the data.", version_needed);return QByteArray();}ushort general_purpose_bits = readUShort(header.h.general_purpose_bits);uint compressed_size = readUInt(header.h.compressed_size);uint uncompressed_size = readUInt(header.h.uncompressed_size);uint start = readUInt(header.h.offset_local_header);//qDebug("uncompressing file %d: local header at %d", i, start);d->device->seek(start);LocalFileHeader lh;d->device->read((char *)&lh, sizeof(LocalFileHeader));uint skip = readUShort(lh.file_name_length) + readUShort(lh.extra_field_length);d->device->seek(d->device->pos() + skip);ushort compression_method = readUShort(lh.compression_method);//qDebug("file=%s: compressed_size=%d, uncompressed_size=%d", fileName.toLocal8Bit().data(), compressed_size, uncompressed_size);if ((general_purpose_bits & Encrypted) != 0) {debugx("QZip: Unsupported encryption method is needed to extract the data.");return QByteArray();}//qDebug("file at %lld", d->device->pos());QByteArray compressed = d->device->read(compressed_size);if (compression_method == CompressionMethodStored) {// no compressioncompressed.truncate(uncompressed_size);return compressed;} else if (compression_method == CompressionMethodDeflated) {// Deflate//qDebug("compressed=%d", compressed.size());compressed.truncate(compressed_size);QByteArray baunzip;ulong len = qMax(uncompressed_size,  1u);int res;do {baunzip.resize(len);res = inflate((uchar*)baunzip.data(), &len,(const uchar*)compressed.constData(), compressed_size);switch (res) {case Z_OK:if ((int)len != baunzip.size())baunzip.resize(len);break;case Z_MEM_ERROR:debugx("QZip: Z_MEM_ERROR: Not enough memory");break;case Z_BUF_ERROR:len *= 2;break;case Z_DATA_ERROR:debugx("QZip: Z_DATA_ERROR: Input data is corrupted");break;}} while (res == Z_BUF_ERROR);return baunzip;}debugx("Zip: Unsupported compression method %d is needed to extract the data.", compression_method);return QByteArray();
}

调用示例:(功能,直接读取zip包里的指定的图像文件的内容)

Mat readImage(QString filePath,bool isZip, int flags)
{if(isZip){auto index= filePath.indexOf(".zip");QString zipFile=filePath.left(index+4);QString imgName=filePath.right(filePath.length()-index-5);//      debugx2(zipFile<<"\n"<<imgName);ZipReader zipreader(zipFile);for(auto fileInfo : zipreader.fileInfoList()){if(fileInfo.isFile){if(fileInfo.filePath==imgName){debugx2(fileInfo.filePath);//注意编码问题
//                    QByteArray dt = fileInfo.filePath.toUtf8();
//                    QString strtemp = QString::fromLocal8Bit(dt);QByteArray array = zipreader.fileData(fileInfo.filePath);//   debugx2(array.size());//   imshow("image",image);zipreader.close();if(array.size()>0)return  imdecode(std::vector<char>(array.constData(),array.constData()+array.size()),flags);else{debugx2(zipFile<<" read fail: "<<imgName);return Mat();}}}}zipreader.close();return Mat();}Mat src = cv::imread(filePath.toLocal8Bit().data(),flags);return src;
}

插件运行效果:不用解压zip,就可以直接浏览zip包里的图片。

这篇关于QT QZipReader改进,以支持大于2G的zip文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/915820

相关文章

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

macOS Sequoia 15.5 发布: 改进邮件和屏幕使用时间功能

《macOSSequoia15.5发布:改进邮件和屏幕使用时间功能》经过常规Beta测试后,新的macOSSequoia15.5现已公开发布,但重要的新功能将被保留到WWDC和... MACOS Sequoia 15.5 正式发布!本次更新为 Mac 用户带来了一系列功能强化、错误修复和安全性提升,进一步增

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自