DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Decompress Several Filetypes With A Single Script

07.17.2008
| 5589 views |
  • submit to reddit
        // Decompresses Z, gz, bz2, zip, rar, tar, and 7z with a single 'decompress' command

#!/bin/bash

# decompress - will decompress a file, regardless of compression type.

Z="compress -d"
gz="gunzip"
bz="bunzip2"
zip="unzip -qo"
rar="unrar x -id -y"
tar="tar xf"
7z="p7zip -d"

if [ $# -eq 0 ]; then
    echo "Usage: decompress file or files to decompress">&2
    exit 1
fi

for name
do
	if [ ! -f "$name" ] ; then
		echo "$0: file $name not found. Skipped." >&2
		continue
	fi

	if [ "$(echo $name | egrep '(\.Z$|\.gz$|\.bz2$|\.zip$|\.rar$|\.tar$|\.tgz$|\.7z$)')" = "" ] ; then
		echo "Skipped file ${name}: it's already decompressed." 
      		continue
	fi

	extension=${name##*.}

	case "$extension" in
		Z ) echo "Filetype is Z. Decompressing..."
		    $Z "$name"
		    ;;
		gz ) echo "Filetype is gz. Decompressing..."
		     $gz "$name"
		     ;;
		bz2 ) echo "Filetype is bz2. Decompressing..."
 		     $bz "$name"
		     ;;
		zip ) echo "Filetype is zip. Decompressing..."
		      $zip "$name"
		      ;;
		rar ) echo "Filetype is rar. Decompressing..."
		      $rar "$name"
		      ;;
		tar ) echo "Filetype is tar. Decompressing..."
              	      $tar "$name"
              	      ;;
		tgz ) echo "Filetype is tgz. Decompressing..."
              	      $tar "$name"
                     ;;
               7z ) echo "Filetype is 7z. Decompressing..."
                     $7z "$name"
	esac

done

exit 0
    

Comments

Snippets Manager replied on Thu, 2008/07/17 - 10:00pm

Good point. I fixed it, just for you.