diff options
author | Jesus <heckyel@hyperbola.info> | 2025-05-18 19:12:22 -0500 |
---|---|---|
committer | Jesus <heckyel@hyperbola.info> | 2025-05-18 19:12:22 -0500 |
commit | e31761a54ce72d9bda4c7d32ce92b064bb7ab1c5 (patch) | |
tree | 9f2cd483bf1151597aa8d570f16cf5a0d41e66da /hyperterm/tools | |
parent | 580f139ba229684c93bae006fd08863a8ecf1106 (diff) | |
download | hyperterm-e31761a54ce72d9bda4c7d32ce92b064bb7ab1c5.tar.lz hyperterm-e31761a54ce72d9bda4c7d32ce92b064bb7ab1c5.tar.xz hyperterm-e31761a54ce72d9bda4c7d32ce92b064bb7ab1c5.zip |
update rar2zip
Diffstat (limited to 'hyperterm/tools')
-rw-r--r-- | hyperterm/tools/rar2zip.sh | 80 |
1 files changed, 58 insertions, 22 deletions
diff --git a/hyperterm/tools/rar2zip.sh b/hyperterm/tools/rar2zip.sh index eb13d80..54ec926 100644 --- a/hyperterm/tools/rar2zip.sh +++ b/hyperterm/tools/rar2zip.sh @@ -5,54 +5,90 @@ # Usage: rar2zip file [file ...] # Example: rar2zip file.rar +function check_and_install_7z() { + if command -v 7z &>/dev/null; then + return + fi + + echo "7z not found. Attempting to install..." + + INSTALLER="" + SUDO="" + USER_CMD=$(command -v sudo || command -v doas) + + [[ "$(id -u)" -ne 0 ]] && SUDO="$USER_CMD" + + OS_ID=$( + cat /etc/*release 2>/dev/null | + tr '[:upper:]' '[:lower:]' | + grep "^id=" | head -n1 | cut -d= -f2 | tr -d '"' + ) + + case "$OS_ID" in + arch|manjaro|artix|hyperbola) + INSTALLER="pacman -Sy --noconfirm p7zip" + ;; + debian|ubuntu|linuxmint|elementary|pop) + INSTALLER="apt-get update && apt-get install -y p7zip-full" + ;; + fedora) + INSTALLER="dnf install -y p7zip p7zip-plugins" + ;; + void) + INSTALLER="xbps-install -Sy p7zip" + ;; + gentoo) + INSTALLER="emerge app-arch/p7zip" + ;; + alpine) + INSTALLER="apk add p7zip" + ;; + *) + echo "Unsupported distro. Cannot install 7z automatically." + return 1 + ;; + esac + + if [ -n "$INSTALLER" ]; then + echo "Installing 7z using: $SUDO $INSTALLER" + $SUDO bash -c "$INSTALLER" + fi +} + function rar2zip() { + check_and_install_7z || { + echo "Failed to install 7z. Aborting." + exit 1 + } - echo "Converting RARs to ZIPs" + echo "Converting RAR files to ZIP..." - # 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" + echo "${OLDFILE}: Original file moved to trash" else - echo "${NEWNAME}: File exists!" + echo "${NEWNAME}: File already exists!" fi done - echo "Conversion Done" + echo "Conversion complete." } |