Convert ZIP to TAR using Linux shell

Problem

You have ZIP archive and want to convert it to TAR automatically conserving the original directory structure.

Solution

Use this script (comments inline).

#!/bin/bash
# Usage: ziptotar.sh <ZIP name>
tmpdir=`mktemp -d`
#Copy the zip to the temporary directory
cp "$1" $tmpdir/
#Unzip
(cd $tmpdir && unzip -q "$1")
#Remove the original zipfile because we don't want that to be tar'd
rm "$tmpdir/$1"
#Tar the files
outfilename=$(echo "$1" | rev | cut -d. -f2- | rev).tar
(cd $tmpdir && tar cf "$outfilename" *)
mv "$tmpdir/$outfilename" .
#Remove the temporary directory
rm -rf $tmpdir
#Print what we did
echo "Converted $1 to $outfilename"