-
Notifications
You must be signed in to change notification settings - Fork 2
/
gz2bz
executable file
·333 lines (285 loc) · 8.82 KB
/
gz2bz
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/sh
##
## gzip to bzip2 converter
## Copyright (c) 2005-2010 by Michal Nazareicz (mina86/AT/mina86.com)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
set -eu
##
## Version
##
version () {
echo 'gz2bz 0.8 (c) 2005-2007 by Michal Nazarewicz (mina86/AT/mina86.com)'
echo
}
##
## Usage
##
usage () {
cat <<EOF
usage: <program> <options> [ [--] file ...]
<program> is of the form <from>2<to> where <from> and <to> is one of:
z or Z Meaning compress compression (experimental)
gz or gzip Meaning gzip compression
bz, bz2, bzip or bzip2 Meaning bzip2 compression
lz or lzma Meaning lzma compression
xz Meaning xz compression
<other> -C or -D must be specified and it specifies extension
of in/out file (experimental, tricky)
<options> are:
-h --help Prints this help screen and exits
-V --verson Prints version information and exits
-v --verbose Becomes more verbose
-vv Becomes even more verbose
-q --quiet Supresses all noncritical messages
-i --interactive Prompt before overwrite
--never-overwrite Never overwrite files
-1 .. -9 Set compression method
--fast Alias of -1
--best Alias of -9
-C --compress=<tool> Uses <tool> as compression executable
-c --comp-opts=<opts> Adds additional arguments to compression tool
-D --decompress=<tool> Uses <tool> as decompression executable
-d --decomp-opts=<opts> Adds additional arguments to decompression tool
-k --keep Keeps uncompressed file
-k --keep=<dir> Save uncompressed file in <dir>
-t --tmp Same as --keep=/tmp
-K --keep-orig Keeps original file file
--keep-both Alias of --keep --keep-orig
-s --keep-smaller --smaller Keep smaller of the original and compressed files
example: gz2bz -9 *.gz
gz2xz -9 *.gz
bz2xz -t -9 *.tar.bz2; cd /tmp; cat *.tar | tar x
EOF
}
##
## Prints message if -f given or verbose
##
msg () {
if [ X"$1" = X-f ]; then
shift
echo "${0##*/}: $*" >&2
elif $verbose; then
echo "${0##*/}: $*" >&2
fi
}
##
## Variables
##
false () { return 1; }
compress=auto
decompress=auto
compress_opts=
decompress_opts=
keep_uncompressed=false
keep_orig=false
keep_smaller=false
verbose=false
verb_opts=
overwrite=y
tmp=
##
## Parse args
##
while [ $# -ne 0 ]; do
case "$1" in
-h|--help) usage ; exit;;
-V|--version) version; exit;;
-v|--verbose) verbose=: ; verb_opts=-v ;;
-vv) verbose=: ; verb_opts=-vv;;
-q|--quiet) verbose=false; verb_opts=-q ;;
-i|--interactive) overwrite=i;;
--never-overwrite) overwrite=n;;
-[0-9]) compress_opts="$compress_opts $1";;
--fast) compress_opts="$compress_opts -1";;
--best) compress_opts="$compress_opts -9";;
-[GD]) decompress="$2" ; shift;;
-[GD]*) decompress="${1#-?}" ;;
--gzip=*|--decompress=*)
decompress="${1#*=}" ;;
-[gd]) decompress_opts="$decompress_opts $2" ; shift;;
-[gd]*) decompress_opts="$decompress_opts ${1#-?}" ;;
--gzip-opts=*|--decomp-opts=*)
decompress_opts="$decompress_opts ${1#*=}" ;;
-[BC]) compress="$2" ; shift;;
-[BC]*) compress="${1#-?}" ;;
--bzip2=*|--compress=*)
compress="${1#*=}" ;;
-[bc]) compress_opts="$compress_opts $2" ; shift;;
-[bc]*) compress_opts="$compress_opts ${1#-?}" ;;
--bzip2-opts=*|--comp-opts=*)
compress_opts="$compress_opts ${1#*=}" ;;
-k|--keep) keep_uncompressed=: ;;
-k*) keep_uncompressed=:; tmp=${1#-?};;
--keep=*) keep_uncompressed=:; tmp=${1#*=};;
-t|--tmp) keep_uncompressed=:; tmp=/tmp ;;
-K|--keep-gz|--keep-orig) keep_orig=:;;
--keep-both) keep_uncompressed=:; keep_orig=:;;
-s|--keep-smaller|--smaller) keep_smaller=true;;
--) shift; break;;
-) break;;
-*) echo "${0##*/}: Invalid argument: $1" >&2; exit 1;;
*) break;;
esac
shift
done
decompress_opts="$decompress_opts $verb_opts"
compress_opts="$compress_opts $verb_opts"
unset verb_opts
##
## Find executables
##
find_exec () {
__var=$1
eval __command=\$$__var
case $__command in auto)
case $2 in -[CD])
msg -f "missing $2 option" >&2
exit 1
esac
__command=$2
esac
__found=`which "$__command"`
if [ -z "$__found" ]; then
msg -f "could not find $__command executable" >&2
exit 1
fi
shift 2
eval $__var=\"\$__found \$* \$${__var}_opts\"
unset ${__var}_opts __var __command __found
}
__prog=${0##*/}
case "$__prog" in
[zZ]2*) find_exec decompress uncompress; in_ext='[Zz]';;
gz2*|gzip2*) find_exec decompress gzip -d; in_ext=gz ;;
bz2*|bz22*|bzip2*|bzip22*) find_exec decompress bzip2 -d; in_ext=bz2 ;;
lz2*|lzma2*) find_exec decompress lzma -d; in_ext=lzma ;;
xz2*) find_exec decompress xz -d; in_ext=xz ;;
*) find_exec decompress -D -d; in_ext= ;;
esac
case "$__prog" in
*2[zZ]) find_exec compress compress ; out_ext='[Zz]';;
*2gz|*2gzip) find_exec compress gzip ; out_ext=gz ;;
*2bz|*2bz2|*2bzip|*2bzip2) find_exec compress bzip2 ; out_ext=bz2 ;;
*2lz|*2lzma) find_exec compress lzma ; out_ext=lzma ;;
*2xz) find_exec compress xz ; out_ext=xz ;;
*) find_exec compress -C ; out_ext=${0#**2};;
esac
unset __prog
##
## Ask whether to overwrite a file
##
overwrite () {
[ -e "$1" ] || return 0
if [ -d "$1" ]; then
msg -f "$1: is a directory"
return 1
fi
__overwrite=$overwrite
while :; do
case "$__overwrite" in
[yY]|[yY][eE][sS]|'')
unset __overwrite
return 0
;;
[nN]|[nN][oO])
msg -f "$1: exists; not overwriting"
unset __overwrite
return 1
;;
*)
printf '%s: %s: exists; overwrite? [Y/n]: ' "${0##*/}" "$1"
read __overwrite || __overwrite=n
esac
done
}
##
## Do the job
##
[ $# -ne 0 ] || set -- -
for file; do
## standard input -> standard output
if [ X"$file" = X- ]; then
msg Converting stdin to stdout
$decompress | $compress || msg -f "$file: failed"
continue
fi
## Get output file name
case "$file" in
*.tgz) output=${file%.tgz}.tar.gz ;;
*.tbz) output=${file%.tbz}.tar.bz2 ;;
*.txz) output=${file%.txz}.tar.xz ;;
*.tlz) output=${file%.tlz}.tar.lzma;;
*) output=$file
esac
case "$output" in
*.$in_ext) output="${output%.$in_ext}" ;;
*) msg -f "$file: invalid extension, skipping"; continue
esac
output_compressed=$output.$out_ext
if [ -n "$tmp" ]; then
output=$tmp/${output##*/}
fi
## Convert
ok=false
overwrite "$output_compressed" || continue
if $keep_uncompressed; then
overwrite "$output" || continue
msg decompressing "$file" to "$output"
if $decompress <"$file" >"$output"; then
msg compressing "$output" to "$output_compressed"
if ! $compress <"$output" >"$output_compressed"; then
rm -f -- "$output_compressed"
else
ok=:
fi
else
rm -f -- "$output"
fi
else
msg converting "$file" to "$output_compressed"
if $decompress <"$file" | $compress >"$output_compressed"; then
ok=:
else
rm -f -- "$output_compressed"
fi
fi
## Finish
if ! $ok; then
msg -f "$file: failed"
continue
fi
if $keep_smaller; then
orig_size=$(stat -c %s -- "$file")
compressed_size=$(stat -c %s -- "$output_compressed")
if [ $orig_size -le $compressed_size ]; then
msg original no bigger then recompressed \
"($orig_size <= $compressed_size)"
msg removing "$output_compressed"
rm -f -- "$output_compressed"
continue
else
msg original bigger then recompressed \
"($orig_size > $compressed_size)"
fi
fi
if ! $keep_orig; then
msg removing "$file"
rm -f -- "$file"
fi
done