wyatt8740

unzipdir.sh v3

Feb 23rd, 2017
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.55 KB | None | 0 0
  1. #! /bin/bash
  2. #unzipdir
  3. #Version 3
  4. #safely extracts the zip equivalent of tarbombs
  5. #v2 works on zipfiles with extensions other than .zip (like .xpi and some .cbr files)
  6. #By Wyatt Ward, but with some things snatched from stack exchange answers
  7. #Requires info-zip
  8.  
  9. # version 3 allows you to immediately 'cd' into the created dir with this
  10. # syntax:
  11. #   cd $(unzipdir zipfile.zip)
  12. # or:
  13. #   cd `unzipdir zipfile.zip`
  14. # (depending on your shell.)
  15. #
  16. export INFILENAME=`echo "$@" | awk -F/ '$0=$NF'` #Looking back, i forget exactly what this does, but I think it cuts out a path.
  17. export INFILEBASENAME=$(basename "$INFILENAME")
  18. export INFILEEXTENSION="${INFILEBASENAME##*.}" #not sure how it works. thanks stack exchange
  19.  
  20. #original v1 line:
  21. #export OUTPUTDIR=`echo "$INFILENAME" | sed 's/.zip//g'`
  22. #new v2 line:
  23. export OUTPUTDIR=`echo "$INFILENAME" | sed "s/.$INFILEEXTENSION//g"`
  24.  
  25. # echo "OUTPUTDIR is"
  26. # echo "$OUTPUTDIR"
  27. export STARTINGDIR="$PWD"
  28. if [ -f "$1" ]
  29. then
  30. #        echo "Found file."
  31.         mkdir "$OUTPUTDIR"
  32.         cd "$OUTPUTDIR"
  33.         if [ `echo "$1" | grep [/]` ]
  34.         then
  35. #leading slash present, no need to find actual paths
  36. #                unzip "$1"
  37. # quiet unzip
  38.                 unzip -qq "$1"
  39.         else
  40. #no slash present, be paranoid and do it the harder way
  41. #                unzip "$STARTINGDIR/$1"
  42. # quiet unzip
  43.                 unzip -qq "$STARTINGDIR/$1"
  44.         fi
  45. # print destination directory name so we can cd into it
  46.         echo "$OUTPUTDIR"
  47. else
  48. # print to stderr
  49.         echo "$1 not found." >&2
  50. fi
Advertisement
Add Comment
Please, Sign In to add comment