diff options
author | Jesús <heckyel@hyperbola.info> | 2021-02-10 13:52:52 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2021-02-10 13:52:52 -0500 |
commit | 7e95c3b667a1b353f8275db886203f272bfcce34 (patch) | |
tree | aefe3b7db69ac78b83b79a70cf43401679812d20 /hyperterm/tools/rar2zip.sh | |
download | hyperterm-7e95c3b667a1b353f8275db886203f272bfcce34.tar.lz hyperterm-7e95c3b667a1b353f8275db886203f272bfcce34.tar.xz hyperterm-7e95c3b667a1b353f8275db886203f272bfcce34.zip |
first commit
Diffstat (limited to 'hyperterm/tools/rar2zip.sh')
-rw-r--r-- | hyperterm/tools/rar2zip.sh | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/hyperterm/tools/rar2zip.sh b/hyperterm/tools/rar2zip.sh new file mode 100644 index 0000000..eb13d80 --- /dev/null +++ b/hyperterm/tools/rar2zip.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# +# rar2zip conversion script +# +# Usage: rar2zip file [file ...] +# Example: rar2zip file.rar + +function rar2zip() { + + echo "Converting RARs to ZIPs" + + # Use RAM disk for temporary files. + WORKDIR="/dev/shm/" + + for INFILE in "$@"; do + # Absolute path to old file + OLDFILE=$(realpath "${INFILE}") + + # Get the file name without the extension + BASENAME=$(basename "${OLDFILE%.*}") + + # Path for the file. The ".zip" file will be written there. + DIRNAME=$(dirname "$OLDFILE") + + # Name of the .zip file + NEWNAME="${DIRNAME}/$BASENAME.zip" + + if [ ! -e "${NEWNAME}" ]; then + # Set name for the temp dir. This directory will be created under WORKDIR + TEMPDIR=$(mktemp -p "$WORKDIR" -d) + + # Create a temporary folder for unRARed files + echo "Extracting $OLDFILE" + + unar "$OLDFILE" -o "${TEMPDIR}/" + + # Zip the files with maximum compression + 7z a -tzip -mx=9 "$NEWNAME" "${TEMPDIR}/*" + # Alternative. MUCH SLOWER, but better compression + # 7z a -mm=Deflate -mfb=258 -mpass=15 -r "$NEWNAME" * + + # Preserve file modification time + touch -r "$OLDFILE" "$NEWNAME" + + # Delete the temporary directory + rm -r "$TEMPDIR" + + # OPTIONAL. Safe-remove the old file + # Restore from "$HOME/.local/share/Trash" + gio trash "$OLDFILE" + echo "${OLDFILE}: A backup was made on $HOME/.local/share/Trash" + else + echo "${NEWNAME}: File exists!" + fi + done + + echo "Conversion Done" +} |