-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompressor.sh
executable file
·160 lines (146 loc) · 5.17 KB
/
compressor.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/compressor.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2024-08-03T05:19:26+0200
commands() {
cmds="7z bzip2 gzip lzip lzma lzop tar xz zstd"
printf "\n"
for cmd in $cmds; do
if command -v "$cmd" > /dev/null 2>&1; then
printf " [X] %s\n" "$cmd"
else
printf " [ ] %s\n" "$cmd"
fi
done
}
script=$(basename "$0")
help="$script [-h/--help] -- script to extract/compress/list files and folders
Usage:
$script [--add/--list] <quality> <file>.<ext> [file1.ext] [file2.ext]
Settings:
[--add] = compress files and folders to archive
[--list] = list archive files and folders
<ext> = compression extensions:
7z, tar, tar.bz2, tar.gz, tar.lz, tar.lzma, tar.lzo, tar.xz,
tar.z, tar.zst, taz, tbz, tbz2, tgz, tlz, txz, tz2, tzo, tzst,
zip
decompression extensions:
7z, a, alz, apk, arj, bz, bz2, bzip2, cab, cb7, cbr, cbt, cbz,
chm, chw, cpio, deb, dmg, doc, epub, exe, gz, gzip, hxs, iso,
jar, lha, lz, lzh, lzma, lzo, msi, pkg, ppt, rar, rpm, swm,
tar, taz, tbz, tbz2, tgz, tlz, txz, tz2, tzo, tzst, udf, war,
wim, xar, xls, xpi, xz, z, zip, zst
Examples:
$script file1.tar.gz file2.tar.bz2 file3.7z
$script --add archive.tar.gz file1.ext file2.ext file3.ext
$script --list file1.tar.gz file2.tar.bz2 file3.7z
Commands:
required for full functionality (X = available): $(commands)"
compress() {
archive="$1"
shift
case "$archive" in
*.tar \
| *.tar.bz2 | *.tar.gz | *.tar.lz | *.tar.lzma | *.tar.lzo \
| *.tar.xz | *.tar.z | *.tar.zst | *.taz | *.tbz | *.tbz2 \
| *.tgz | *.tlz | *.txz | *.tz2 | *.tzo | *.tzst)
tar cvfa "$archive" "$@"
;;
*.zip)
7z a -tzip "$archive" "$@"
;;
*.7z)
7z a "$archive" "$@"
;;
*)
printf "compress: unknown extension\n"
exit 1
;;
esac
}
extract() {
[ "$1" = "--list" ] \
&& list=1 \
&& shift
for archive in "$@"; do
if [ -f "$archive" ]; then
base="$(printf "%s" "${archive##*/}" \
| tr '[:upper:]' '[:lower:]')"
folder="${base%.*}"
case "$base" in
*.tar \
| *.tar.bz2 | *.tar.gz | *.tar.lz | *.tar.lzma | *.tar.lzo \
| *.tar.xz | *.tar.z | *.tar.zst | *.taz | *.tbz | *.tbz2 \
| *.tgz | *.tlz | *.txz | *.tz2 | *.tzo | *.tzst)
case "$list" in
1)
tar tvf "$archive"
;;
*)
folder="${folder%%.tar}"
printf "tar xvf \"%s\" -C \"%s\"\n" \
"$archive" "$folder"
mkdir -p "$folder"
tar xvf "$archive" -C "$folder" >/dev/null 2>&1
;;
esac
;;
*.7z \
| *.a | *.alz | *.apk | *.arj | *.bz | *.bz2 | *.bzip2 \
| *.cab | *.cb7 | *.cbr | *.cbt | *.cbz | *.chm | *.chw \
| *.cpio | *.deb | *.dmg | *.doc | *.epub | *.exe | *.gz \
| *.gzip | *.hxs | *.iso | *.jar | *.lha | *.lz | *.lzh \
| *.lzma | *.lzo | *.msi | *.pkg | *.ppt | *.rar | *.rpm \
| *.swm | *.udf | *.war | *.wim | *.xar | *.xls | *.xpi \
| *.xz | *.z | *.zip | *.zst)
case "$list" in
1)
7z l -p "$archive" \
| tail +2 # WORKAROUND: 7z first line empty
;;
*)
printf "7z x \"%s\" -o\"%s\"\n" \
"$archive" "$folder"
mkdir -p "$folder"
7z x "$archive" -o"$folder" >/dev/null 2>&1
;;
esac
;;
*)
case "$list" in
1) option="list";;
*) option="extract";;
esac
printf "%s \"%s\": unknown archive method\n" \
"$option" "$archive"
exit 1
;;
esac
else
case "$list" in
1) option="list";;
*) option="extract";;
esac
printf "%s \"%s\": file does not exist\n" \
"$option" "$archive"
exit 1
fi
done
}
case "$1" in
-h | --help | "")
printf "%s\n" "$help"
;;
--list)
shift
extract --list "$@"
;;
--add)
shift
compress "$@"
;;
*)
extract "$@"
;;
esac