Skip to content

fix(compressor): fix MIME type detection failure for spanned ZIP files#428

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:fix-4-30
Jun 11, 2026
Merged

fix(compressor): fix MIME type detection failure for spanned ZIP files#428
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:fix-4-30

Conversation

@dengzhongyuan365-dev

@dengzhongyuan365-dev dengzhongyuan365-dev commented Jun 11, 2026

Copy link
Copy Markdown
Member
  • Some spanned ZIP files start with PK\x07\x08 signature (non-standard PK\x03\x04), which Qt MIME detection cannot recognize
  • When file extension is .zip but content detection returns non-ZIP type, fall back to file command for re-detection

Qt 无法识别以 PK\x07\x08 开头的分卷 ZIP 文件,当扩展名为 .zip
但内容检测结果非 ZIP 类型时,回退使用 file 命令二次检测以确保
正确识别。

PMS: ztgd2026060900011

Influence: spanned ZIP files can be correctly detected and extracted

Summary by Sourcery

Bug Fixes:

  • Ensure spanned ZIP files with non-standard signatures are correctly detected by falling back to the file command when Qt content detection is inconclusive or inconsistent with a .zip extension.

- Some spanned ZIP files start with PK\x07\x08 signature (non-standard
  PK\x03\x04), which Qt MIME detection cannot recognize
- When file extension is .zip but content detection returns non-ZIP type,
  fall back to file command for re-detection

Qt 无法识别以 PK\x07\x08 开头的分卷 ZIP 文件,当扩展名为 .zip
但内容检测结果非 ZIP 类型时,回退使用 file 命令二次检测以确保
正确识别。

PMS: ztgd2026060900011

Influence: spanned ZIP files can be correctly detected and extracted
@sourcery-ai

sourcery-ai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts MIME type detection logic to better handle ZIP files, particularly WinZip-style spanned ZIPs, by refining when the system falls back to the file command.

Flow diagram for updated ZIP MIME detection fallback logic

flowchart TD
    A[determineMimeType filename] --> B[mimeFromExtension]
    B --> C[extName = mimeFromExtension.name]
    C --> D[isZipExtension = extName == application/zip]
    D --> E[mimeFromContent]
    E --> F[isContentZip = mimeFromContent.inherits application/zip]
    F --> G["needFileCommandFallback = mimeFromContent.isDefault OR (isZipExtension AND NOT isContentZip)"]
    G -->|true| H[Run file command for detection]
    G -->|false| I[Use mimeFromContent]
    H --> J[Return MIME type]
    I --> J
Loading

File-Level Changes

Change Details Files
Refine MIME detection fallback logic to use the file command when ZIP extensions conflict with non-ZIP content detection.
  • Introduce extName, isZipExtension, and isContentZip variables derived from existing MIME detection results.
  • Define needFileCommandFallback to be true when content detection is default or when a .zip extension is present but content is not recognized as ZIP.
  • Reuse the existing file-command-based detection path, now guarded by the new needFileCommandFallback condition instead of only checking mimeFromContent.isDefault().
src/source/common/mimetypes.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Binding mimeFromExtension.name() to const QString &extName creates a dangling reference because name() returns a temporary; store it by value (QString extName = mimeFromExtension.name();) instead.
  • Since the string literal "application/zip" is used in multiple places, consider extracting it into a constexpr/constant or reusing the same QStringLiteral to avoid duplication and keep type checks consistent.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Binding `mimeFromExtension.name()` to `const QString &extName` creates a dangling reference because `name()` returns a temporary; store it by value (`QString extName = mimeFromExtension.name();`) instead.
- Since the string literal `"application/zip"` is used in multiple places, consider extracting it into a constexpr/constant or reusing the same QStringLiteral to avoid duplication and keep type checks consistent.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

我对这段代码的改进意见如下:

  1. 代码逻辑改进:
  • 当前的改进增加了对ZIP文件的特殊处理逻辑,这是一个很好的优化,可以处理WinZip分卷ZIP等特殊情况。
  • 使用明确的布尔变量(isZipExtension, isContentZip, needFileCommandFallback)提高了代码的可读性。
  1. 代码质量改进:
  • 建议将magic number(如"application/zip")定义为常量或枚举,以提高代码的可维护性。
  • 可以考虑将这部分逻辑封装为一个单独的函数,以提高代码的模块化和可重用性。
  1. 代码性能改进:
  • 当前代码在每次调用时都会创建QProcess对象,可以考虑将其作为类成员变量以避免重复创建。
  • 对于频繁调用的场景,可以考虑添加缓存机制,避免重复检测相同文件的MIME类型。
  1. 代码安全改进:
  • 在使用QProcess执行外部命令时,应该添加错误处理机制,确保命令执行失败时能够优雅地处理。
  • 应该限制可执行的file命令的参数,避免命令注入风险。
  1. 其他建议:
  • 建议添加注释说明为什么需要特殊处理ZIP文件,以便后续维护者理解这段代码的意图。
  • 考虑添加日志记录,记录MIME类型检测的决策过程,便于调试和问题追踪。

改进后的代码示例:

// 定义MIME类型常量
namespace {
    const QString APPLICATION_ZIP = QStringLiteral("application/zip");
}

// 将检测逻辑封装为单独的函数
bool shouldUseFileCommandFallback(const QString& mimeContent, const QString& mimeExtension) {
    bool isZipExtension = (mimeExtension == APPLICATION_ZIP);
    bool isContentZip = mimeContent.inherits(APPLICATION_ZIP);
    return mimeContent.isDefault() || (isZipExtension && !isContentZip);
}

CustomMimeType determineMimeType(const QString &filename) {
    // ... 前面的代码 ...
    
    const QString &extName = mimeFromExtension.name();
    if (shouldUseFileCommandFallback(mimeFromContent, extName)) {
        qDebug() << "Using file command to detect MIME type for:" << filename;
        QProcess process;
        QStringList args;
        // ... 后面的代码 ...
        
        // 添加错误处理
        if (!process.waitForFinished()) {
            qWarning() << "File command execution failed:" << process.errorString();
            return mimeFromExtension; // 或其他适当的错误处理
        }
        // ... 其余代码 ...
    }
    // ... 其余代码 ...
}

这些改进可以提高代码的可维护性、性能和安全性,同时保持原有功能的完整性。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot deepin-bot Bot merged commit 5f3bbab into linuxdeepin:master Jun 11, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants