fix: update current filename display during p7zip compression#396
Open
LiHua000 wants to merge 1 commit intolinuxdeepin:release/eaglefrom
Open
fix: update current filename display during p7zip compression#396LiHua000 wants to merge 1 commit intolinuxdeepin:release/eaglefrom
LiHua000 wants to merge 1 commit intolinuxdeepin:release/eaglefrom
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: LiHua000 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
- Calculate current file based on progress percentage and file size - Fix issue where p7zip only displays the last filename during compression - Add weighted calculation by file size for accurate display timing - Separate compression and extraction logic to preserve extraction functionality log: fix bug Bug: https://pms.uniontech.com/bug-view-357565.html
deepin pr auto review这段代码主要针对压缩/解压工具的进度显示和文件名解析逻辑进行了优化。我将从语法逻辑、代码质量、代码性能和代码安全四个方面进行审查,并提出改进意见。 1. 语法逻辑问题点:
改进建议:
2. 代码质量问题点:
改进建议:
3. 代码性能优点:
潜在问题与建议:
4. 代码安全问题点:
改进建议:
综合改进代码示例针对上述分析,以下是重构后的代码片段建议,重点展示了逻辑优化和性能改进: // 在 cliinterface.h 中添加辅助成员
// QVector<qint64> m_accumulatedFileSizes; // 累计文件大小,用于二分查找
// 在 addFiles 中初始化
// m_totalFileSize = 0;
// m_accumulatedFileSizes.clear();
// for (const auto &file : m_files) {
// m_totalFileSize += file.qSize;
// m_accumulatedFileSizes.append(m_totalFileSize);
// }
// 在 handleProgress 中的 WT_Add 处理逻辑优化
void CliInterface::handleAddProgress(int percentage) {
if (m_files.isEmpty()) return;
QString strfilename;
int fileIndex = 0;
// 辅助 lambda:获取显示用的文件名
auto getDisplayName = [](const FileEntry& entry) {
return entry.strFileName.isEmpty() ?
QFileInfo(entry.strFullPath).fileName() :
entry.strFileName;
};
if (m_totalFileSize == 0) {
// 文件数量平均分配
fileIndex = (percentage * (m_files.size() - 1)) / 100;
} else {
// 使用二分查找根据大小定位文件
// 注意:这里需要防止 qint64 溢出,使用 double 进行中间计算
double compressedSize = (static_cast<double>(percentage) / 100.0) * m_totalFileSize;
// 使用 std::upper_bound 查找第一个大于 compressedSize 的累计大小位置
auto it = std::upper_bound(m_accumulatedFileSizes.begin(),
m_accumulatedFileSizes.end(),
static_cast<qint64>(compressedSize));
fileIndex = std::distance(m_accumulatedFileSizes.begin(), it);
// 边界修正:如果计算出的索引超出范围,取最后一个
if (fileIndex >= m_files.size()) {
fileIndex = m_files.size() - 1;
}
}
strfilename = getDisplayName(m_files[fileIndex]);
if (!strfilename.isEmpty()) {
emit signalCurFileName(strfilename);
}
}
// 在 handleProgress 主函数中调用
void CliInterface::handleProgress(const QString &line) {
// ... 前面的代码 ...
if (percentage > 0) {
if (line.contains("\b\b\b\b")) {
if (m_workStatus == WT_Extract) {
// ... 解压逻辑 ...
} else if (m_workStatus == WT_Add) {
handleAddProgress(percentage); // 调用拆分后的函数
} else {
// ... 删除/重命名逻辑 ...
}
emit signalprogress(percentage);
}
}
// ...
}总结这段代码的改进初衷是好的(缓存总大小以优化性能),但在实现细节上还有提升空间。主要建议包括:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
log: fix bug
Bug: https://pms.uniontech.com/bug-view-357565.html