diff options
| author | Pavel Egorov <[email protected]> | 2025-06-15 00:03:09 +0300 |
|---|---|---|
| committer | Pavel Egorov <[email protected]> | 2025-06-15 00:03:09 +0300 |
| commit | c91c1d588b4a60416f10e4e2c28b6fc0ea67c1ab (patch) | |
| tree | f84060378b44c719c25f07377b8f3e6affdd1679 /.local/bin | |
| parent | 725e92e43455bc455a1b2913b907a173b622babe (diff) | |
| download | dotfiles-c91c1d588b4a60416f10e4e2c28b6fc0ea67c1ab.tar.gz dotfiles-c91c1d588b4a60416f10e4e2c28b6fc0ea67c1ab.zip | |
[script] Add decompress script
Diffstat (limited to '')
| -rwxr-xr-x | .local/bin/decompress | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/.local/bin/decompress b/.local/bin/decompress new file mode 100755 index 0000000..a2023c3 --- /dev/null +++ b/.local/bin/decompress | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | #!/usr/bin/env fish | ||
| 2 | |||
| 3 | set options 'h/help' | ||
| 4 | set archiver_formats tar | ||
| 5 | set bzip2 bz2 tar.bz2 tbz | ||
| 6 | set gzip gz tar.gz tgz | ||
| 7 | set lz4 lz4 tar.lz4 | ||
| 8 | set xz xz tar.xz txz | ||
| 9 | set zstd zst tar.zst tzst | ||
| 10 | set compressor_formats $bzip2 $gzip $lz4 $xz $zstd | ||
| 11 | set arch_and_comp_formats 7z rar zip | ||
| 12 | set formats $archiver_formats $compressor_formats $arch_and_comp_formats | ||
| 13 | |||
| 14 | function print_help | ||
| 15 | echo -es "Unarchive and uncompress wrapper\n" \ | ||
| 16 | "Supported formats: $formats\n" \ | ||
| 17 | "Also able to use MIME types and detect ZIP magic sequence" | ||
| 18 | echo "Usage: uncompress" \ | ||
| 19 | "[-h | --help]" \ | ||
| 20 | "file1 [file2, file3, ...]" >&2 | ||
| 21 | echo -es \ | ||
| 22 | "-h, --help\t\tPrint this help" | ||
| 23 | end | ||
| 24 | |||
| 25 | function get_extension -a file | ||
| 26 | set bname (basename $file) | ||
| 27 | set extension (string join '.' (string split '.' $bname)[2..]) | ||
| 28 | echo $extension | ||
| 29 | end | ||
| 30 | |||
| 31 | function extract_by_extension -a archive extension | ||
| 32 | switch $extension | ||
| 33 | case tar | ||
| 34 | tar --force-local -xvf $archive | ||
| 35 | case tar.bz2 tbz | ||
| 36 | bzcat $archive | tar xf - | ||
| 37 | case bz2 | ||
| 38 | bzip2 -d $archive | ||
| 39 | case tar.gz tgz | ||
| 40 | zcat $archive | tar xf - | ||
| 41 | case gz | ||
| 42 | gzip -d $archive | ||
| 43 | case tar.lz4 | ||
| 44 | lz4cat $archive | tar xf - | ||
| 45 | case lz4 | ||
| 46 | lz4 $archive | ||
| 47 | case tar.xz txz | ||
| 48 | xzcat $archive | tar xf - | ||
| 49 | case xz | ||
| 50 | xz -d $archive | ||
| 51 | case tar.zst tzst | ||
| 52 | zstdcat $archive | tar xf - | ||
| 53 | case zst | ||
| 54 | zstd -d $archive | ||
| 55 | case 7z | ||
| 56 | 7z x $archive | ||
| 57 | case rar | ||
| 58 | if type -q rar | ||
| 59 | rar x $archive | ||
| 60 | else | ||
| 61 | unrar x $archive | ||
| 62 | end | ||
| 63 | case zip | ||
| 64 | unzip $archive | ||
| 65 | end | ||
| 66 | or return 1 | ||
| 67 | end | ||
| 68 | |||
| 69 | function extract_by_mime -a archive | ||
| 70 | set possible_extensions (string split ' ' (file --extension $archive))[2] | ||
| 71 | set possible_extensions (string split '/' $possible_extensions) | ||
| 72 | |||
| 73 | for extension in $possible_extensions | ||
| 74 | extract_by_extension $archive $extension | ||
| 75 | and return | ||
| 76 | end | ||
| 77 | |||
| 78 | return 1 | ||
| 79 | end | ||
| 80 | |||
| 81 | # Script main code | ||
| 82 | |||
| 83 | argparse $options -- $argv | ||
| 84 | or return 1 | ||
| 85 | |||
| 86 | if set -q _flag_help; or test (count $argv) -eq 0 | ||
| 87 | print_help | ||
| 88 | return | ||
| 89 | end | ||
| 90 | |||
| 91 | set final_status 0 | ||
| 92 | |||
| 93 | for archive in $argv | ||
| 94 | set extension (get_extension $archive) | ||
| 95 | |||
| 96 | if contains $extension $formats | ||
| 97 | extract_by_extension $archive $extension | ||
| 98 | else | ||
| 99 | echo "Unable to match $archive extension, trying MIME-type detection" >&2 | ||
| 100 | extract_by_mime $archive | ||
| 101 | and continue | ||
| 102 | |||
| 103 | echo "Checking if $archive is secretly a ZIP archive" >&2 | ||
| 104 | if test (head -c 4 $archive | base64) = "UEsDBA==" | ||
| 105 | unzip $archive | ||
| 106 | continue | ||
| 107 | end | ||
| 108 | |||
| 109 | echo "Extraction of $archive failed" >&2 | ||
| 110 | set final_status 1 | ||
| 111 | end | ||
| 112 | end | ||
| 113 | |||
| 114 | return $final_status | ||
