Skip to content

Commit

Permalink
Add support for other Linux Distros and support for additional archiv…
Browse files Browse the repository at this point in the history
…e types
  • Loading branch information
kelexine authored Mar 28, 2024
1 parent ccb99d9 commit 37c364e
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions extract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,41 @@
extract_dir="rom"
mkdir -p "$extract_dir"

# Function to attempt extraction with specific tool
# Function to attempt extraction with 7z
attempt_extract() {
local archive="$1"
local tool="$2"
local archive="$1"

if [ -f "$archive" ]; then
echo "Attempting to extract '$archive' with '$tool'..."
if $tool "$archive" -C "$extract_dir" >/dev/null 2>&1; then
if [ -f "$archive" ]; then
echo "Attempting to extract '$archive' with 7z..."
if 7z x "$archive" -o"$extract_dir" >/dev/null 2>&1; then
echo "Extracted successfully!"
return 0
else
echo "Failed to extract with '$tool'."
echo "Failed to extract with 7z."
fi
fi
return 1
fi
return 1
}

# Supported archive extensions
archive_extensions=("zip" "tar.gz" "tar" "tar.xz" "7z")

# Try extracting based on file extension
for extension in "${archive_extensions[@]}"; do
archive=*.$extension
attempt_extract "$archive" "unzip" && break
attempt_extract "$archive" "tar" && break
attempt_extract "$archive" "7z" && break
done

# Install p7zip if no extraction succeeded
if [ $? -eq 1 ]; then
echo "No built-in tools found. Installing p7zip..."
if command -v apt-get &>/dev/null; then
sudo apt-get update
sudo apt-get install -y p7zip
elif command -v yum &>/dev/null; then
sudo yum install -y p7zip
else
# Install p7zip-full if not already installed
if ! command -v 7z &>/dev/null; then
echo "7z not found. Installing p7zip-full..."
if command -v apt &>/dev/null; then
sudo apt update
sudo apt install -y p7zip-full
elif command -v dnf &>/dev/null; then
sudo dnf install -y p7zip*
elif command -v pacman &>/dev/null; then
sudo pacman -S p7zip
else
echo "Error: Unsupported package manager." >&2
exit 1
fi

# Retry extraction with p7zip for all archives
for archive in *.{zip,tar.gz,tar.xz,7z}; do
attempt_extract "$archive" "7z"
done
fi
fi

# Attempt extraction for all archives in the current directory
for archive in *; do
attempt_extract "$archive"
done

echo "Extraction complete. Files are in the '$extract_dir' directory."

0 comments on commit 37c364e

Please sign in to comment.