summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-x.local/bin/decompress114
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
3set options 'h/help'
4set archiver_formats tar
5set bzip2 bz2 tar.bz2 tbz
6set gzip gz tar.gz tgz
7set lz4 lz4 tar.lz4
8set xz xz tar.xz txz
9set zstd zst tar.zst tzst
10set compressor_formats $bzip2 $gzip $lz4 $xz $zstd
11set arch_and_comp_formats 7z rar zip
12set formats $archiver_formats $compressor_formats $arch_and_comp_formats
13
14function 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"
23end
24
25function get_extension -a file
26 set bname (basename $file)
27 set extension (string join '.' (string split '.' $bname)[2..])
28 echo $extension
29end
30
31function 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
67end
68
69function 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
79end
80
81# Script main code
82
83argparse $options -- $argv
84or return 1
85
86if set -q _flag_help; or test (count $argv) -eq 0
87 print_help
88 return
89end
90
91set final_status 0
92
93for 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
112end
113
114return $final_status