Created
January 5, 2024 23:43
-
-
Save frozolotl/12b61acd8b388b5985cabfb95836d8ce to your computer and use it in GitHub Desktop.
A utility to extract archives using 7-Zip more easily
This file contains 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
# Extract an archive using 7-Zip. | |
export def main [ | |
file: path | |
--output (-o): path | |
] { | |
let extension = $file | path parse | get extension | |
let compressor = match $extension { | |
bz2 | tb2 | tbz | tbz2 | tz2 => "bzip2" | |
gz | taz | tgz => "gzip" | |
lz => "lzip" | |
lzma | tlz => "lzma" | |
lzo => "lzop" | |
xz | txz => "xz" | |
Z | tZ | taZ => "compress" | |
zst | tzst => "zstd" | |
} | |
let file_no_ext = $file | path parse | reject extension | path join | |
mut output = if $output == null { | |
$file_no_ext | |
} else { | |
$output | |
} | |
let tmp_output = (mktemp --directory --tmpdir-path .) | |
if $compressor == null { | |
7z x $file $"-o($tmp_output)" | |
} else { | |
let inner_type = match $extension { | |
tb2 | tbz | tbz2 | tz2 => "tar" | |
taz | tgz => "tar" | |
tlz => "tar" | |
txz => "tar" | |
tZ | taZ => "tar" | |
tzst => "tar" | |
_ => ($file_no_ext | path parse | get extension) | |
} | |
if $inner_type == "" { | |
7z x $file $"-o($tmp_output)" | |
} else { | |
$output = ($output | path parse --extension $inner_type | reject extension | path join) | |
7z x $"-t($compressor)" -so $file | 7z x -si $"-t($inner_type)" $"-o($tmp_output)" | |
} | |
} | |
let file_increment = {|file| | |
mut next = $file | |
mut i = 1 | |
while ($next | path exists) { | |
$next = $"($file).($i)" | |
$i += 1 | |
} | |
$next | |
} | |
let actual_output = (do $file_increment $output) | |
let inner = ([$tmp_output ($output | path basename)] | path join) | |
if (ls $tmp_output | get name) == [$inner] { | |
mv $inner $actual_output | |
rm $tmp_output | |
} else { | |
mv $tmp_output $actual_output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment