32 lines
1.1 KiB
Fish
Executable File
32 lines
1.1 KiB
Fish
Executable File
#!/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)
|
|
|
|
# 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 是空的"
|
|
continue
|
|
end
|
|
|
|
# 3. 智能判断执行
|
|
if test $item_count -gt 1
|
|
# 如果根部有多个文件/文件夹,创建同名目录解压
|
|
echo "📦 检测到多个根项目 ($item_count),解压至目录: $base_name/"
|
|
mkdir -p "$base_name"
|
|
7z x -mmt=on "$file" -o"$base_name"
|
|
else
|
|
# 如果根部只有一个项目,直接解压到当前目录
|
|
echo "📄 检测到单个根项目,直接解压..."
|
|
7z x -mmt=on "$file"
|
|
end
|
|
end |