Skip to content

Commit

Permalink
Update extract.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
kelexine committed Dec 19, 2023
1 parent 11f9007 commit 583bc9e
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions extract.sh
@@ -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."

0 comments on commit 583bc9e

Please sign in to comment.