2018-03-10 04:04:29 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
print_help() {
|
|
|
|
echo ""
|
2019-08-29 00:54:49 +00:00
|
|
|
echo "Convert an archive from one format to another"
|
2018-03-10 04:04:29 +00:00
|
|
|
echo ""
|
|
|
|
echo "Usage: to7zip [options] ...files"
|
|
|
|
echo ""
|
|
|
|
echo "Options:"
|
|
|
|
echo " -h, --help Print this help text"
|
|
|
|
echo " -d, --delete-old Delete converted file"
|
|
|
|
echo " -f, --force Replace the file with the converted file even if the file exists"
|
|
|
|
echo " -r, --rezip Extract and archive again even if the converted file is already a 7zip archive"
|
2019-08-29 00:54:49 +00:00
|
|
|
echo " -F, --format The format of the output"
|
2018-03-10 04:04:29 +00:00
|
|
|
echo " -t, --thread Set the number of threads for archive extraction and creation (default: $thread_count)"
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
|
2019-08-29 00:54:49 +00:00
|
|
|
short_args=hdfrF:t:
|
|
|
|
long_args=help,delete-old,force,rezip,format:,thread:
|
2018-03-10 04:04:29 +00:00
|
|
|
|
|
|
|
# default
|
|
|
|
rezip=
|
|
|
|
delete_old=
|
|
|
|
force=
|
2019-08-29 00:54:49 +00:00
|
|
|
zip_args=
|
2018-03-10 04:04:29 +00:00
|
|
|
thread_count=8
|
|
|
|
params=""
|
|
|
|
|
|
|
|
# Parse arguments
|
|
|
|
parsed_args=$(getopt --options $short_args --longoptions $long_args --name "$0" -- "$@")
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo "Failed to parse arguments"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
eval set -- "$parsed_args"
|
|
|
|
while true; do
|
|
|
|
case "$1" in
|
|
|
|
-h|--help)
|
|
|
|
print_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-d|--delete-old)
|
|
|
|
delete_old=y
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-f|--force)
|
|
|
|
force=y
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-r|--rezip)
|
|
|
|
rezip=y
|
|
|
|
shift
|
|
|
|
;;
|
2019-08-29 00:54:49 +00:00
|
|
|
-F|--format)
|
|
|
|
case "$2" in
|
|
|
|
7z)
|
|
|
|
;;
|
|
|
|
zip)
|
|
|
|
zip_args="-tzip"
|
|
|
|
output_ext="zip"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown format: $2"
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
shift 2
|
|
|
|
;;
|
2018-03-10 04:04:29 +00:00
|
|
|
-t|--thread)
|
|
|
|
thread_count="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
out_dir="$PWD"
|
|
|
|
|
2019-08-29 00:54:49 +00:00
|
|
|
if [[ -z "$zip_args" ]]; then
|
|
|
|
zip_args="-t7z -m0=lzma2 -ms=on"
|
|
|
|
output_ext=7z
|
|
|
|
fi
|
|
|
|
|
2018-03-10 04:04:29 +00:00
|
|
|
for f in "$@"; do
|
2018-11-21 14:23:42 +00:00
|
|
|
tmp_dir="$(mktemp -d "$PWD/to7zip.XXX")"
|
2019-08-29 00:54:49 +00:00
|
|
|
output="$(echo "$f" | sed -E 's/\.(7z|tar\.gz|zip|rar)$//g').$output_ext"
|
2018-03-10 04:04:29 +00:00
|
|
|
backup_file="$output".backup
|
|
|
|
do_backup=
|
|
|
|
|
|
|
|
if [[ ! -r "$f" ]]; then
|
|
|
|
echo "$f : No such file or directory"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$f -> $output"
|
|
|
|
|
|
|
|
if [[ -f "$output" ]]; then
|
|
|
|
if [[ -n "$force" ]] || [[ "$f" =~ .7z$ && -n "$rezip" ]]; then
|
|
|
|
do_backup=y
|
|
|
|
else
|
|
|
|
echo "$output : file exists, refusing to continue (-f to force)"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-08-29 00:54:49 +00:00
|
|
|
if [[ "$f" =~ \.tar[^/]*$ ]]; then
|
|
|
|
if ! tar -xf "$f" -C "$tmp_dir" >/dev/null; then
|
|
|
|
echo "$f : extract failed"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
elif [[ "$f" =~ \.tar|gz|bz2|zip|7z$ ]]; then
|
2018-03-10 04:04:29 +00:00
|
|
|
if ! 7z e -mmt="$thread_count" -o"$tmp_dir" "$f" >/dev/null; then
|
|
|
|
echo "$f : extract failed"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
elif [[ "$f" =~ \.rar$ ]]; then
|
|
|
|
if ! unrar x "$f" "$tmp_dir" >/dev/null; then
|
|
|
|
echo "$f : unrar failed"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
cp -r "$f" "$tmp_dir"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "$do_backup" ]]; then
|
|
|
|
mv "$output" "$backup_file"
|
|
|
|
fi
|
|
|
|
|
|
|
|
pushd "$tmp_dir" >/dev/null
|
2019-08-29 00:54:49 +00:00
|
|
|
7z a $zip_args -mx=9 -mmt="$thread_count" "$out_dir/$output" >/dev/null
|
2018-03-10 04:04:29 +00:00
|
|
|
return_val=$?
|
|
|
|
popd >/dev/null
|
|
|
|
if [[ $return_val -eq 0 ]]; then
|
|
|
|
if [[ -n "$do_backup" ]]; then
|
|
|
|
rm -r "$backup_file"
|
|
|
|
elif [[ -n "$delete_old" ]]; then
|
|
|
|
rm -r "$f"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [[ -n "$do_backup" ]]; then
|
|
|
|
mv "$backup_file" "$output"
|
|
|
|
fi
|
|
|
|
echo "$f : 7z archive failed"
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -r "$tmp_dir"
|
|
|
|
done
|
|
|
|
|