要压缩的几个文件中含有中文文件名文件,经过quazip压缩后,压缩包里对应的文件名存在乱码,只要将QuaZip的编码格式设为gbk即可,直接上完整代码:
//将多个文件压缩并保存
int compressFile(QStringList filePathList, QString savePath)
{
QStringList wrapper = filePathList;
QString zipLocation = savePath;
QuaZip zip(zipLocation);
zip.setFileNameCodec("gbk");//gbk,IBM866
if (!zip.open(QuaZip::mdCreate)) {
return -1;//错误1
}
QuaZipFile outFile(&zip);
for (int i = 0; i < wrapper.size(); i++) {
QFileInfo file(wrapper.at(i));
if (file.exists()) {
QFile inFile;
QFile inFileTemp;
inFileTemp.setFileName(file.fileName());
inFile.setFileName(file.filePath());
if (!inFile.open(QIODevice::ReadOnly)) {
qDebug() << inFile.errorString().toLocal8Bit().constData();
return -2;
}
if (!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(inFileTemp.fileName(), inFile.fileName()))) {
return -3;;
}
outFile.write(inFile.readAll());
if (outFile.getZipError() != UNZ_OK) {
return -4;
}
outFile.close();
if (outFile.getZipError() != UNZ_OK) {
return -5;
}
inFile.close();
}
}
zip.close();
if (zip.getZipError()) {
return -6;
}
return 1;
}
以下是问题解决前后的图片: