Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # This script is public domain. You may use this for any purpose.
- #
- # By using this script, you acknowledge that the author is not liable
- # for any damages or lost data.
- # recursiveunzip
- # 0.0.1
- #
- # Unzips a zip file containing other zip files into a directory
- # structure, with optional deletion of the interior zip files.
- IllegalArgs=1
- FileNotFound=2
- CannotCreateFolder=3
- main()
- {
- fileName=$(basename -s .zip "$1")
- mkdir $fileName 2> /dev/null
- folderCreationFailure=$?
- if [ $folderCreationFailure != 0 ]; then
- # there's already a folder with this name, or you don't have
- # write permissions. For the sake of safety, we exit.
- echo "Can't create the folder $fileName in $(pwd)"
- exit $CannotCreateFolder
- fi
- unzip -d "$fileName" "$fileName".zip
- cd "$fileName"
- oldIFS="$IFS"; IFS=$'\n'
- zipFiles=( $(ls *.zip 2> /dev/null) ); IFS="$oldIFS"
- if [ $zipFiles ]; then
- for file in "$zipFiles"; do
- main "$file"
- done
- fi
- }
- # User interface starts here.
- if [ $# != 1 ]; then
- echo "Please pass in a single valid zip file."
- exit $IllegalArgs
- fi
- if [ ! -r $1 ]; then
- echo "You don't have read permissions for the file $1, or it doesn't exist."
- exit $FileNotFound
- fi
- isZip=$(file "$1" | grep -e "Zip archive data" -c)
- # This uses the `file` utility to make sure it is a zip file. `grep`
- # counts the matches and then we put this value in the status
- # variable.
- if [ ! $isZip ]; then # the file is not a zip file
- echo "You must pass in a .zip file."
- exit $IllegalArgs
- else
- fileNameOriginal=$(basename -s .zip "$1")
- oldpwd="$(pwd)"
- main "$1"
- cd "$oldpwd"
- prompt="Would you like to remove all zip files inside $fileNameOriginal? [yes/no] "
- tree "$fileNameOriginal"
- while [ ! $answer ]; do
- read -p "$prompt" answer
- if [ "$answer" != "yes" ] && [ "$answer" != "no" ]; then
- unset answer
- fi
- done
- if [ "$answer" == "yes" ]; then
- find "$fileNameOriginal" -type 'f' -name '*.zip' -delete
- fi
- tree "$fileNameOriginal"
- exit 0
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement