Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,60 @@ | ||
#!/bin/bash | ||
|
||
# Create a directory for extracting files | ||
# Create directory for extracted files | ||
extract_dir="rom" | ||
mkdir -p "$extract_dir" | ||
|
||
# Function to install required tools | ||
install_tools() { | ||
if command -v unzip &>/dev/null; then | ||
archive_tool="unzip" | ||
elif command -v tar &>/dev/null; then | ||
archive_tool="tar" | ||
else | ||
echo "Error: Could not find suitable archive extraction tool (zip or tar)." >&2 | ||
exit 1 | ||
fi | ||
# Function to attempt extraction with specific tool | ||
attempt_extract() { | ||
local archive="$1" | ||
local tool="$2" | ||
local options="$3" | ||
|
||
if ! command -v 7z &>/dev/null; then | ||
if command -v apt-get &>/dev/null; then | ||
echo "Installing p7zip..." | ||
sudo apt-get update | ||
sudo apt-get install -y p7zip | ||
elif command -v yum &>/dev/null; then | ||
echo "Installing p7zip..." | ||
sudo yum install -y p7zip | ||
if [ -f "$archive" ]; then | ||
echo "Attempting to extract '$archive' with '$tool'..." | ||
if $tool $options "$archive" -C "$extract_dir"; then | ||
echo "Extracted successfully!" | ||
return 0 | ||
else | ||
echo "Error: Unsupported package manager." >&2 | ||
exit 1 | ||
echo "Failed to extract with '$tool'." | ||
fi | ||
fi | ||
return 1 | ||
} | ||
|
||
# Install required tools | ||
install_tools | ||
# Try extracting based on file extension | ||
for archive in *.{zip,tar.gz,tar.xz,7z}; do | ||
case "$archive" in | ||
*.zip) attempt_extract "$archive" unzip ;; | ||
*.tar.gz | *.tar) attempt_extract "$archive" tar -xf ;; | ||
*.tar.xz) attempt_extract "$archive" tar -xJf ;; | ||
*.7z) attempt_extract "$archive" 7z x -o"$extract_dir" ;; | ||
*) echo "Unsupported archive format: $archive" ;; | ||
esac | ||
|
||
# Extract files from archives | ||
for archive in *.{zip,tar.gz,tar,xz,7z}; do | ||
if [ -f "$archive" ]; then | ||
echo "Extracting $archive..." | ||
case "$archive" in | ||
*.zip) $archive_tool "$archive" -d "$extract_dir" ;; | ||
*.tar.gz | *.tar) $archive_tool -xf "$archive" -C "$extract_dir" ;; | ||
*.tar.xz) $archive_tool -xJf "$archive" -C "$extract_dir" ;; | ||
*.7z) 7z x "$archive" -o"$extract_dir" ;; | ||
*) echo "Unsupported archive format: $archive" ;; | ||
esac | ||
# Stop processing if extraction succeeds | ||
if [[ $? -eq 0 ]]; then | ||
break | ||
fi | ||
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 | ||
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 x -o"$extract_dir" | ||
done | ||
fi | ||
|
||
echo "Extraction complete. Files are in the '$extract_dir' directory." |