您的位置:首页 > 编程语言 > Qt开发

Qt 使用QuaZip库生成MD5加密Zip格式压缩文件

2017-10-05 23:12 816 查看
QuaZip库下载地址: https://sourceforge.net/projects/quazip/files/latest/download

#include "quazip.h"

#include "quazipfile.h"

#include "quazipfileinfo.h"

void uploadAnswerPackege()
{
// 压缩文件
//JlCompress::compressDir(QString("%1.zip").arg(GMethod::instance()->getAnswerName()), DIR_RESULT);

const QString& zipName(QString("%1.zip").arg(DIR_RESULT + GMethod::instance()->getZkzh()));
QuaZip zip(zipName);
if (!zip.open(QuaZip::mdCreate))
{
qDebug("Could not create zip: %s", qPrintable(zipName));
return;
}

const QString& innerName = QString("%1.ans").arg(GMethod::instance()->getZkzh());
QuaZipNewInfo newInfo(innerName);

//生成MD5加密
QByteArray byteArray;
byteArray.append(innerName);
QByteArray hash = QCryptographicHash::hash(byteArray, QCryptographicHash::Md5);
QString strMD5 = hash.toHex();

QuaZipFile file(&zip);
bool ret = file.open(QIODevice::WriteOnly,
newInfo, // QuaZipNewInfo结构体引用
strMD5.toUtf8().constData(), // 密码
0, // CRC值(默认值是0)
8); // 写入方法(0为文件夹,8为普通文件)
if (ret)
{
QFile file2(GMethod::instance()->getAnswerName());
bool isopen = file2.open(QIODevice::ReadOnly);
if (isopen)
{
// 开始写入文件的数据了
file.write(file2.readAll());
}
file2.close();
file.close();
}
zip.close();

ui->stackWidgetExam->setCurrentIndex(PAGE_END);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  quazip MD5 加密 压缩