-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.plugins.bash
executable file
·47 lines (38 loc) · 1.15 KB
/
compress.plugins.bash
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
# shellcheck shell=bash
cite about-plugin
about-plugin 'Compression tools'
targz () {
about 'Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression'
group 'Compress'
local tmpFile="${*%/}.tar";
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || return 1;
size=$(
stat -f"%z" "${tmpFile}" 2> /dev/null; # OS X `stat`
stat -c"%s" "${tmpFile}" 2> /dev/null # GNU `stat`
);
local cmd="";
if (( size < 52428800 )) && hash zopfli 2> /dev/null; then
# the .tar file is smaller than 50 MB and Zopfli is available; use it
cmd="zopfli";
else
if hash pigz 2> /dev/null; then
cmd="pigz";
else
cmd="gzip";
fi;
fi;
echo "Compressing .tar using \`${cmd}\`…";
"${cmd}" -v "${tmpFile}" || return 1;
[[ -f "${tmpFile}" ]] && rm "${tmpFile}";
echo "${tmpFile}.gz created successfully.";
}
gz () {
about 'Compare original and gzipped file size'
group 'Compress'
declare -i origsize -i gzipsize -i ratio
origsize=$(wc -c < "$1");
gzipsize=$(gzip -c "$1" | wc -c);
ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l);
printf "orig: %d bytes\n" "$origsize";
printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio";
}