update rar2zip
This commit is contained in:
parent
580f139ba2
commit
e31761a54c
@ -5,7 +5,7 @@ cdfe049ec07f02a1893cda29c13085d06709e09a30b0c2e1111585278315f03139d61080c883cb3f
|
||||
2036a79215a5434e31f3406bea3f2ffa7e94ffef86c2d1ceb8865db29f19fe7f342f9cab93288f57c75daed36ef146f85d15f8d633931a27d55c3983f55ef15b ./core/git.sh
|
||||
7447d3e167ab207d3ef4218e201a06bf5a3fc23281639f16f7f405f1d66b73923845d450fdb0a94672757866a9da0324f728564a1b61b2ed1678fe576eb565cf ./core/autocomplete.sh
|
||||
f3e00b2aa8ab9f3ab44570adaa2520408ed66fd00f551654d60b64a4be3546ec781b7efa39bcd774937e654b6ffb4c7af3f21eeb36caf9c01f82f85cf28e2b4d ./core/languages.sh
|
||||
88b215a6c2df22bc84bda981b3ff1d27ba391f03e2b84b95adefe1e8885b079b0da7c885ec0ad3256b60b8da9efa9ba8ab28906ece76781b192ea474d579d143 ./tools/rar2zip.sh
|
||||
b846a929844e74fc76ce65d2bd7aefcdeb03e058d9ce68a7f3f6bce6080a843d90eae78cecc2faac0c5b066a739a8328dfcd042cd25cb5aaa856e956a0c4d0c2 ./tools/rar2zip.sh
|
||||
73becd983f15d68b3c459adb4fe847bbbd6343519640aa5e03bb530e61a59ed0545dd3b3621ad82da378bbf15c4d9ee63984004d3bfed26d9d9df643f1524de5 ./tools/proxy.sh
|
||||
0b9671c851278cd6a5484ab95b62606b0b925f9606f4de400c5e15a66e35e86bb6bb15e4e1b599ca819c230604bce0ca755d599ec9cd59a14b41f352ef897997 ./tools/aliases.sh
|
||||
fab9d339a99c7d2e1809d1c44f533523c6bfcdcc8d63c62b335ce7d4c666c8bdd7ac319316bf71f043163a3a0184e25ecfe1ee32724627424d042a05fa80ce77 ./tools/vconverter.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."
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user