Skip to content

Commit c816dfe

Browse files
committed
fix: Fix Chinese password compression failure
Fix Chinese password compression failure Log: Fix Chinese password compression failure
1 parent e7b9c40 commit c816dfe

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

3rdparty/libzipplugin/libzipplugin.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ PluginFinishType LibzipPlugin::extractFiles(const QList<FileEntry> &files, const
216216
return PFT_Cancel;
217217
} else {
218218
setPassword(query.password());
219-
zip_set_default_password(archive, m_strPassword.toUtf8().constData());
219+
QByteArray passwordBytes = passwordUnicode(m_strPassword, 0);
220+
zip_set_default_password(archive, passwordBytes.constData());
220221
lastNeedPasswordIndex = i;
221222
i--;
222223
}
@@ -269,7 +270,8 @@ PluginFinishType LibzipPlugin::extractFiles(const QList<FileEntry> &files, const
269270
return PFT_Cancel;
270271
} else {
271272
setPassword(query.password());
272-
zip_set_default_password(archive, m_strPassword.toUtf8().constData());
273+
QByteArray passwordBytes = passwordUnicode(m_strPassword, 0);
274+
zip_set_default_password(archive, passwordBytes.constData());
273275
i--;
274276
}
275277
} else {
@@ -668,12 +670,16 @@ bool LibzipPlugin::writeEntry(zip_t *archive, const QString &entry, const Compre
668670
// 设置压缩的加密算法
669671
if (options.bEncryption && !options.strEncryptionMethod.isEmpty()) { //ReadOnlyArchiveInterface::password()
670672
int ret = 0;
673+
QByteArray passwordBytes = passwordUnicode(options.strPassword, 0);
674+
qInfo() << "Setting encryption for file, method:" << options.strEncryptionMethod
675+
<< "password length:" << options.strPassword.length()
676+
<< "encoded bytes length:" << passwordBytes.length();
671677
if (QLatin1String("AES128") == options.strEncryptionMethod) {
672-
ret = zip_file_set_encryption(archive, uindex, ZIP_EM_AES_128, options.strPassword.toUtf8().constData());
678+
ret = zip_file_set_encryption(archive, uindex, ZIP_EM_AES_128, passwordBytes.constData());
673679
} else if (QLatin1String("AES192") == options.strEncryptionMethod) {
674-
ret = zip_file_set_encryption(archive, uindex, ZIP_EM_AES_192, options.strPassword.toUtf8().constData());
680+
ret = zip_file_set_encryption(archive, uindex, ZIP_EM_AES_192, passwordBytes.constData());
675681
} else if (QLatin1String("AES256") == options.strEncryptionMethod) {
676-
ret = zip_file_set_encryption(archive, uindex, ZIP_EM_AES_256, options.strPassword.toUtf8().constData());
682+
ret = zip_file_set_encryption(archive, uindex, ZIP_EM_AES_256, passwordBytes.constData());
677683
}
678684
if (ret != 0) {
679685
emit error(("Failed to set compression options for entry: %1"));
@@ -1216,6 +1222,7 @@ QByteArray LibzipPlugin::passwordUnicode(const QString &strPassword, int iIndex)
12161222

12171223
// chinese
12181224
if (b) {
1225+
qInfo() << "Password contains Chinese characters, using codec:" << m_listCodecs[iIndex];
12191226
QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
12201227
QTextCodec *gbk = QTextCodec::codecForName(m_listCodecs[iIndex].toUtf8().data());
12211228
// QTextCodec *gbk = QTextCodec::codecForName("UTF-8");
@@ -1227,6 +1234,7 @@ QByteArray LibzipPlugin::passwordUnicode(const QString &strPassword, int iIndex)
12271234
QByteArray gb_bytes = gbk->fromUnicode(strUnicode);
12281235
return gb_bytes; //获取其char *115645 【专业版】【1060】【归档管理器】【5.12.0.2】无法解压中文密码的zip压缩包(含有长名称)
12291236
} else {
1237+
qInfo() << "Password is non-Chinese, using UTF-8 encoding";
12301238
return strPassword.toUtf8();
12311239
}
12321240
} else {

3rdparty/libzipplugin/libzipplugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ class LibzipPlugin : public ReadWriteArchiveInterface
198198
QMap<QString, int> m_mapRealDirValue; // 长文件真实文件统计
199199
QSet<QString> m_setLongName; // 存储被截取之后的文件名称(包含001之类的)
200200
bool m_bLnfs = false; //文件系统是否支持长文件
201+
QByteArray m_passwordData; // 存储编码后的密码数据,确保在压缩过程中保持有效
201202
};
202203

203204
#endif // LIBZIPPLUGIN_H

src/source/mainwindow.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,11 @@ void MainWindow::slotCompress(const QVariant &val)
12691269
// 判断zip格式是否使用了中文加密
12701270
bool zipPasswordIsChinese = false;
12711271
if ("application/zip" == m_stCompressParameter.strMimeType) {
1272-
if (m_stCompressParameter.strPassword.contains(REG_EXP("[\\x4e00-\\x9fa5]+"))) {
1273-
zipPasswordIsChinese = true;
1272+
for (const QChar &ch : m_stCompressParameter.strPassword) {
1273+
if (ch.unicode() >= 0x4E00 && ch.unicode() <= 0x9FA5) {
1274+
zipPasswordIsChinese = true;
1275+
break;
1276+
}
12741277
}
12751278
}
12761279

0 commit comments

Comments
 (0)