Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman
Last active April 16, 2025 22:54
Show Gist options
  • Save gingerbeardman/edf5d728c3d5933224667819271bf53d to your computer and use it in GitHub Desktop.
Save gingerbeardman/edf5d728c3d5933224667819271bf53d to your computer and use it in GitHub Desktop.
decompress .tar and .tar.gz then run a 2-pass deark on all resulting files to extract all PICT and keep only the resulting PNG
#!/usr/bin/env zsh
# Check if source folder is provided
if [ -z "$1" ]; then
echo "Usage: $0 <source_folder>"
exit 1
fi
SOURCE_DIR="$1"
DEST_DIR="${SOURCE_DIR}_extracted"
DEARK=$(which deark)
# Validate deark executable
if [ -z "$DEARK" ] || [ ! -x "$DEARK" ]; then
echo "Error: deark executable not found. Please ensure deark is installed and in your PATH."
exit 1
fi
# Create destination folder
mkdir -p "$DEST_DIR" || { echo "Failed to create destination folder"; exit 1; }
# Copy contents to destination folder
cp -r "$SOURCE_DIR"/* "$DEST_DIR" || { echo "Failed to copy files"; exit 1; }
# Change to destination directory
cd "$DEST_DIR" || { echo "Failed to change to destination directory"; exit 1; }
# Extract all .tar and .tar.gz files in the destination folder
find . -type f -name "*.tar" -exec tar -xf {} \; || { echo "Failed to extract .tar files"; exit 1; }
find . -type f -name "*.tar.gz" -exec tar -xzf {} \; || { echo "Failed to extract .tar.gz files"; exit 1; }
# Remove .tar and .tar.gz files after extraction
find . -type f \( -name "*.tar" -o -name "*.tar.gz" \) -delete || { echo "Failed to remove .tar and .tar.gz files"; exit 1; }
# Extract all files with deark
find . -type f -print0 | xargs -0 -I {} "$DEARK" {} -k -od . || { echo "Extraction failed"; exit 1; }
# Remove .adf files (if any)
find . -type f -name "*.adf" -delete || { echo "Failed to remove .adf files"; exit 1; }
# Convert files to PNG using deark with -m pict
find . -type f -print0 | xargs -0 -I {} "$DEARK" {} -m pict -k -od . || { echo "Conversion to PNG failed"; exit 1; }
# Keep only PNG files, remove others
find . -type f ! -name "*.png" -delete || { echo "Failed to clean up non-PNG files"; exit 1; }
# Remove empty directories
find . -type d -empty -delete || { echo "Failed to remove empty directories"; exit 1; }
# Rename PNG files to keep only the part before the first dot
while IFS= read -r -d '' file; do
# Get the base name (without path) and extract part before first dot
base=$(basename "$file")
new_name=$(echo "$base" | cut -d'.' -f1).png
new_path="./$new_name"
# Handle naming conflicts by appending a counter
# counter=1
# while [ -e "$new_path" ]; do
# new_name=$(echo "$base" | cut -d'.' -f1)_$counter.png
# new_path="./$new_name"
# ((counter++))
# done
# Rename the file
mv "$file" "$new_path" || { echo "Failed to rename $file to $new_path"; exit 1; }
done < <(find . -type f -name "*.png" -print0)
echo "Processing complete. PNG files are in $DEST_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment