7Z压缩是一款常用的压缩算法和工具,本文主要介绍一款在qt环境下进行编译的压缩方法。
本人测试是可以正常跑通的,具体代码部分请下载:下载链接,提取码:ev9t
7z源码网址:7-Zip
7z简介:
7z 是一种全新的压缩格式,它拥有极高的压缩比。
7z 格式的主要特征:
- 开放的结构
- 高压缩比
- 强大的 AES-256 加密
- 能够兼容任意压缩、转换、加密算法
- 最高支持 16000000000 GB 的文件压缩
- 以 Unicode 为标准的文件名
- 支持固实压缩
- 支持文件头压缩
7z 已公开了结构编辑功能,所以它可以支持任何一种新的压缩算法。到目前为止,下列压缩算法已被整合到了 7z 中:
本文分享的代码具备直接调用7z源码进行操作,具有较高的学习价值。
//列出文件
void MainWindow::on_pushButton_2_clicked()
{Q7z::initSevenZ();const QString path = "E:/ProjectTest/QT/LZMA/lz.7z";QFile file(path);file.open(QIODevice::ReadOnly);QVector<Q7z::File> files = Q7z::listArchive(&file);for(int i =0;i<files.count();i++){Q7z::File file = files.at(i);qDebug()<<file.path;}
}
QVector<File> listArchive(QFileDevice *archive)
{Q7Z_ASSERTS(archive, Readable)const qint64 initialPos = archive->pos();try {CCodecs codecs;if (codecs.Load() != S_OK)throw SevenZipException(QCoreApplication::translate("Q7z", "Cannot load codecs."));COpenOptions op;op.codecs = &codecs;CObjectVector<COpenType> types;op.types = &types; // Empty, because we use a stream.CIntVector excluded;op.excludedFormats = &excluded;const CMyComPtr<IInStream> stream = new QIODeviceInStream(archive);op.stream = stream; // CMyComPtr is needed, otherwise it crashes in OpenStream().CObjectVector<CProperty> properties;op.props = &properties;CArchiveLink archiveLink;if (archiveLink.Open2(op, nullptr) != S_OK) {throw SevenZipException(QCoreApplication::translate("Q7z","Cannot open archive \"%1\".").arg(archive->fileName()));}QVector<File> flat;for (unsigned i = 0; i < archiveLink.Arcs.Size(); ++i) {IInArchive *const arch = archiveLink.Arcs[i].Archive;UInt32 numItems = 0;if (arch->GetNumberOfItems(&numItems) != S_OK) {throw SevenZipException(QCoreApplication::translate("Q7z","Cannot retrieve number of items in archive."));}flat.reserve(flat.size() + numItems);for (uint item = 0; item < numItems; ++item) {UString s;if (archiveLink.Arcs[i].GetItemPath(item, s) != S_OK) {throw SevenZipException(QCoreApplication::translate("Q7z","Cannot retrieve path of archive item \"%1\".").arg(item));}File f;f.archiveIndex.setX(i);f.archiveIndex.setY(item);f.path = UString2QString(s).replace(QLatin1Char('\\'), QLatin1Char('/'));Archive_IsItem_Folder(arch, item, f.isDirectory);f.permissions = getPermissions(arch, item, nullptr);getDateTimeProperty(arch, item, kpidMTime, &(f.utcTime));f.uncompressedSize = getUInt64Property(arch, item, kpidSize, 0);f.compressedSize = getUInt64Property(arch, item, kpidPackSize, 0);flat.append(f);}}return flat;} catch (const char *err) {archive->seek(initialPos);throw SevenZipException(err);} catch (const SevenZipException &e) {archive->seek(initialPos);throw e; // re-throw unmodified} catch (...) {archive->seek(initialPos);throw SevenZipException(QCoreApplication::translate("Q7z","Unknown exception caught (%1).").arg(QString::fromLatin1(Q_FUNC_INFO)));}return QVector<File>(); // never reached
}
链接: https://pan.baidu.com/s/15VldB4LMVgl3XbOabGZ-og 提取码: ev9t
请按需提取