Repository navigation
Expand file tree
/
Copy pathzipdir
More file actions
executable file
·100 lines (81 loc) · 1.82 KB
/
Copy pathzipdir
File metadata and controls
executable file
·100 lines (81 loc) · 1.82 KB
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
#! /usr/bin/env bash
# shellcheck disable=SC2317
set -ueE -o pipefail
if [ "$(uname -s)" = Darwin ]; then
tar=gtar
else
tar
fi
prog="$(basename "$0")"
function usage {
[ "${*-}" ] && echo "$prog: $*" 1>&2
cat 1>&2 <<EOF
Usage: $prog { --gzip | --lzip | --xz } src-directory dest-file
Compress the contents of SRC-DIRECTOR to DEST-FILE.
Relative size/speed for a 3.1G source directory of 1,603
files (text and binary):
arg time output size
--gzip 1m3s 3.0G
--xz 2m6s 3.0G
--lzip 18m3s 3.1G DO NOT USE!!!!!!!
EOF
exit 1
}
function errordie {
[ "${*-}" ] && echo "$prog: $*" 1>&2
exit 1
}
type -p "$tar" > /dev/null || errordie "Cannot find $tar"
debug=
function d {
if [ "$debug" ]; then
echo "would: $*" 1>&2
else
echo "+ $*" 1>&2
"$@"
fi
}
gzip=
lzip=
xz=
while [ $# -gt 0 ]; do
case $1 in
--debug) debug=$1 ;;
--gzip) gzip=$1 ;;
--lzip) lzip=$1 ;;
--xz) xz=$1 ;;
--) ;;
*) break ;;
esac
shift
done
[ $# -eq 2 ] || usage missing arguments
[ "$gzip" ] || [ "$lzip" ] || [ "$xz" ] ||
usage "--gzip or --lzip or --xz must be supplied"
[ -d "$1" ] || errordie "source directory $1 does not exist"
src=$1
[ -f "$2" ] && errordie "output file $2 exists"
dst=$2
tempfile="$(dirname "$dst")/${prog}temp$$"
rm -f "$tempfile"
function exit_cleanup {
/bin/rm -f "$tempfile"
}
function err_report {
echo "Error on line $(caller)" 1>&2
}
trap err_report ERR
trap exit_cleanup EXIT
{
if [ "$gzip" ]; then
d tar --gzip -cf "$tempfile" "$src"
elif [ "$lzip" ]; then
d tar --lzip -cf "$tempfile" "$src"
elif [ "$xz" ]; then
d tar --xz -cf "$tempfile" "$src"
else
errordie should not get here
fi
d mv "$tempfile" "$dst"
exit 0
}