ea 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. # a command line utility intended to simplify bulk decompression
  3. # by automating a pool sized by the processor of parallel operations
  4. # each of which creates a folder, decompresses, and if successful
  5. # removes the compressed file, finally checking each directory and
  6. # pulling out single items to avoid redundant parent directories.
  7. # execute extraction
  8. extract_locally() {
  9. local extension="${1##*.}"
  10. [ -d "$1" ] && return # skip/ignore directories
  11. ! [[ "$extension" =~ (7z|zip|rar) ]] && return # ignore unsupported types
  12. # handle each case independently
  13. local name="${1%.*}"
  14. mkdir -p "$name"
  15. case "$extension" in
  16. zip)
  17. if ! unzip -o "$1" -d "$name"; then
  18. return
  19. fi
  20. ;;
  21. rar)
  22. if ! unrar x -o+ -idp "$1" "$name"; then
  23. return
  24. fi
  25. ;;
  26. 7z)
  27. if ! 7za x -aoa -bd "$1" -o"$name"; then
  28. return
  29. fi
  30. ;;
  31. esac
  32. # cleanup
  33. rm "$1"
  34. # @todo: fix redundant parent folders
  35. if [ $(find "$name" -mindepth 1 -maxdepth 1 | wc -l) -eq 1 ]; then
  36. mv "$name" "${name}.tmp"
  37. mv "${name}.tmp"/* .
  38. rmdir "${name}.tmp"
  39. fi
  40. }
  41. # capture all files in directory
  42. shopt -s nullglob
  43. export files=(*)
  44. # execute up to nproc parallel extractions
  45. for file in "${files[@]}"; do
  46. while ! [ $(jobs -p | wc -l) -lt $(nproc) ]; do sleep 1; done # this should wait for available threads
  47. extract_locally "$file" &
  48. done
  49. # notify the process is complete
  50. wait
  51. echo "done"