fixed a lot of stuff
This commit is contained in:
+120
-15
@@ -1,14 +1,18 @@
|
||||
function extract
|
||||
function extract --description "Extract various archive formats"
|
||||
if test (count $argv) -lt 1
|
||||
echo "Usage: extract <archive(s)> [output_directory]"
|
||||
return 1
|
||||
end
|
||||
|
||||
if not command -q file
|
||||
echo "extract: 'file' command is required. Install it via your package manager."
|
||||
return 1
|
||||
end
|
||||
|
||||
set archives $argv
|
||||
set output_dir "."
|
||||
set use_custom_dir 0
|
||||
|
||||
# If exactly 2 args and second is a directory (or doesn't exist yet), treat it as output dir
|
||||
if test (count $argv) -eq 2
|
||||
set maybe_dir $argv[2]
|
||||
if test -d "$maybe_dir"; or not test -e "$maybe_dir"
|
||||
@@ -19,7 +23,6 @@ function extract
|
||||
end
|
||||
end
|
||||
|
||||
# Detect multiple archives
|
||||
set multi 0
|
||||
if test (count $archives) -gt 1
|
||||
set multi 1
|
||||
@@ -31,10 +34,8 @@ function extract
|
||||
continue
|
||||
end
|
||||
|
||||
# Determine base name (strip extensions)
|
||||
set base (string replace -r '\.(tar\.gz|tgz|tar\.bz2|tbz2|tar\.xz|txz|gz|bz2|xz|zst|zip|7z|rar)$' '' (basename "$archive"))
|
||||
set base (string replace -r '\.(tar\.(gz|bz2|xz|zst)|tar|tgz|tbz2|txz|tzst|gz|bz2|xz|zst|zip|7z|rar)$' '' (basename "$archive"))
|
||||
|
||||
# Decide target directory
|
||||
if test $multi -eq 1
|
||||
set target_dir "$base"
|
||||
else if test $use_custom_dir -eq 1
|
||||
@@ -43,30 +44,134 @@ function extract
|
||||
set target_dir "$base"
|
||||
end
|
||||
|
||||
mkdir -p "$target_dir"
|
||||
|
||||
set mime (file --mime-type -b "$archive")
|
||||
|
||||
switch $mime
|
||||
case 'application/x-tar'
|
||||
if not command -q tar
|
||||
echo "extract: 'tar' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
mkdir -p "$target_dir"
|
||||
tar -xvf "$archive" -C "$target_dir"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
|
||||
case 'application/gzip'
|
||||
tar xvzf "$archive" -C "$target_dir"
|
||||
if string match -q '*.tar.gz' -- "$archive"; or string match -q '*.tgz' -- "$archive"
|
||||
if not command -q tar
|
||||
echo "extract: 'tar' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
mkdir -p "$target_dir"
|
||||
tar -xzf "$archive" -C "$target_dir"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
else
|
||||
if not command -q gzip
|
||||
echo "extract: 'gzip' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
set outname (string replace -r '\.gz$' '' (basename "$archive"))
|
||||
gzip -dkc "$archive" > "$output_dir/$outname"
|
||||
echo "extract: decompressed '$archive' to '$output_dir/$outname'"
|
||||
end
|
||||
|
||||
case 'application/x-bzip2'
|
||||
tar xvjf "$archive" -C "$target_dir"
|
||||
if string match -q '*.tar.bz2' -- "$archive"; or string match -q '*.tbz2' -- "$archive"
|
||||
if not command -q tar
|
||||
echo "extract: 'tar' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
mkdir -p "$target_dir"
|
||||
tar -xjf "$archive" -C "$target_dir"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
else
|
||||
if not command -q bzip2
|
||||
echo "extract: 'bzip2' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
set outname (string replace -r '\.bz2$' '' (basename "$archive"))
|
||||
bzip2 -dkc "$archive" > "$output_dir/$outname"
|
||||
echo "extract: decompressed '$archive' to '$output_dir/$outname'"
|
||||
end
|
||||
|
||||
case 'application/x-xz'
|
||||
tar xvJf "$archive" -C "$target_dir"
|
||||
if string match -q '*.tar.xz' -- "$archive"; or string match -q '*.txz' -- "$archive"
|
||||
if not command -q tar
|
||||
echo "extract: 'tar' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
mkdir -p "$target_dir"
|
||||
tar -xJf "$archive" -C "$target_dir"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
else
|
||||
if not command -q xz
|
||||
echo "extract: 'xz' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
set outname (string replace -r '\.xz$' '' (basename "$archive"))
|
||||
xz -dkc "$archive" > "$output_dir/$outname"
|
||||
echo "extract: decompressed '$archive' to '$output_dir/$outname'"
|
||||
end
|
||||
|
||||
case 'application/zstd'
|
||||
tar --zstd -xvf "$archive" -C "$target_dir"
|
||||
if string match -q '*.tar.zst' -- "$archive"; or string match -q '*.tzst' -- "$archive"
|
||||
if not command -q tar
|
||||
echo "extract: 'tar' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
mkdir -p "$target_dir"
|
||||
tar --zstd -xvf "$archive" -C "$target_dir"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
else
|
||||
if not command -q zstd
|
||||
echo "extract: 'zstd' is not installed. Install it via your package manager."
|
||||
continue
|
||||
end
|
||||
set outname (string replace -r '\.zst$' '' (basename "$archive"))
|
||||
zstd -dkc "$archive" > "$output_dir/$outname"
|
||||
echo "extract: decompressed '$archive' to '$output_dir/$outname'"
|
||||
end
|
||||
|
||||
case 'application/zip'
|
||||
if not command -q unzip
|
||||
echo "extract: 'unzip' is not installed. Install it via your package manager (e.g., 'unzip' on most distros)."
|
||||
continue
|
||||
end
|
||||
mkdir -p "$target_dir"
|
||||
unzip "$archive" -d "$target_dir"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
|
||||
case 'application/x-7z-compressed'
|
||||
7z x "$archive" -o"$target_dir"
|
||||
if command -q 7z
|
||||
mkdir -p "$target_dir"
|
||||
7z x "$archive" -o"$target_dir"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
else if command -q 7zz
|
||||
mkdir -p "$target_dir"
|
||||
7zz x "$archive" -o"$target_dir"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
else
|
||||
echo "extract: '7z' or '7zz' is not installed. Install p7zip via your package manager."
|
||||
continue
|
||||
end
|
||||
|
||||
case 'application/x-rar' 'application/vnd.rar'
|
||||
unrar x -o+ "$archive" "$target_dir"
|
||||
if command -q unrar
|
||||
mkdir -p "$target_dir"
|
||||
unrar x -o+ "$archive" "$target_dir/"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
else if command -q rar
|
||||
mkdir -p "$target_dir"
|
||||
rar x -o+ "$archive" "$target_dir/"
|
||||
echo "extract: extracted '$archive' to '$target_dir'"
|
||||
else
|
||||
echo "extract: 'unrar' or 'rar' is not installed. Install unrar via your package manager."
|
||||
continue
|
||||
end
|
||||
|
||||
case '*'
|
||||
echo "extract: unknown file type for '$archive' ($mime)"
|
||||
continue
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user