diff --git a/bin/uzp b/bin/uzp index d3ad676..33da7c1 100755 --- a/bin/uzp +++ b/bin/uzp @@ -1,32 +1,55 @@ #!/usr/bin/env fish + for file in $argv if not test -f "$file" echo "错误: $file 不是有效文件" continue end - # 1. 获取不带后缀的文件名作为备选文件夹名 - set -l base_name (string replace -r '\.[^.]+$' '' $file) + # 1. 获取不带后缀的文件名 (例如 temp.zip -> temp) + set -l base_name (string replace -r '\.[^.]+$' '' (basename "$file")) + + # 2. 预检:获取压缩包顶级路径,并过滤掉系统垃圾 + set -l all_paths (7z l -slt "$file" | grep "^Path = " | string replace -r '^Path = ' '') + set -l root_items + for p in $all_paths + # 只取路径的第一级 + set -l root (string split -m 1 / $p)[1] + # 排除 macOS 垃圾和空字符,且去重 + if not contains -- "$root" $root_items + if not string match -qr '^(__MACOSX|\.DS_Store|archive_temp)$' "$root" + set -a root_items "$root" + end + end + end - # 2. 预检:获取压缩包根路径下的唯一项目数量 - # 逻辑:列出所有路径 -> 提取第一级目录/文件名 -> 去重 -> 计数 - set -l root_items (7z l -slt "$file" | grep "^Path = " | string replace -r '^Path = ' '' | string replace -r '/.*$' '' | sort -u) set -l item_count (count $root_items) if test $item_count -eq 0 - echo "跳过: $file 是空的" + echo "跳过: $file 是空的或仅包含系统垃圾文件" continue end - # 3. 智能判断执行 - if test $item_count -gt 1 - # 如果根部有多个文件/文件夹,创建同名目录解压 + # 3. 核心智能判断逻辑 + set -l need_folder true + + if test $item_count -eq 1 + # 情况 1: 只有一个根项目,直接解压 + set need_folder false + else if contains -- "$base_name" $root_items + # 情况 2: 有多个项目,但其中一个文件夹的名字和压缩包同名 + # 这通常意味着“自带容器”,为了防止嵌套,直接解压 + set need_folder false + echo "💡 检测到包内已包含同名容器 '$base_name/',将跳过额外目录创建..." + end + + # 4. 执行解压 + if test "$need_folder" = true echo "📦 检测到多个根项目 ($item_count),解压至目录: $base_name/" mkdir -p "$base_name" 7z x -mmt=on "$file" -o"$base_name" else - # 如果根部只有一个项目,直接解压到当前目录 - echo "📄 检测到单个根项目,直接解压..." + echo "📄 结构清晰,直接解压..." 7z x -mmt=on "$file" end end \ No newline at end of file