Skip to content

Instantly share code, notes, and snippets.

@brucewoodward
Last active March 25, 2017 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brucewoodward/a5a549d302696f40b46651898007eb0b to your computer and use it in GitHub Desktop.
Save brucewoodward/a5a549d302696f40b46651898007eb0b to your computer and use it in GitHub Desktop.
Unpack zip files from usenet.
#!/usr/local/bin/bash
zipOutput=/tmp/$$.tmp
die()
{
echo $* 1>&2
exit 1
}
run_unpack()
{
local file="$1" parFile= prefix=
# unzip the zip file, saving the output of the unzip command.
(unzip $file | tee "$zipOutput") || die "failed to unzip file $file"
# using the output of the unzip command, find par2 file with the shortest file name.
parFile=`cat "$zipOutput" | awk '/extracting/ && $2 ~ /par2$/ {print $0, length($NF)}' | sort -n -k 3 | head -1 | awk '{print $(NF-1)}'`
# bail out if there isn't a par2 file.
if [[ -z "$parFile" ]]
then
die "didn't find a par file"
fi
echo parFile $parFile
# Run the par2 recovery.
par2 r "$parFile" || die "failed to run par2"
# Build the end file from the rar files.
unrar e `awk '$2 ~ /rar$/ {print $2}' "$zipOutput"` || die "unrar failed"
# Get the first piece of text from the par2 file.
prefix=`echo "$parFile" | awk -F. '{print $1}'`
# Use the prefix to remove files not needed any more.
rm $file "$prefix".*.par2 "$prefix".*.sfv "$prefix".*.nfo "$prefix".*.r??
}
## MAIN
# if there are command line args, assume that the args are files to be unpacked.
if [[ ! -z "$1" ]]
then
for f in "$@"
do
run_unpack "$f"
done
else
# OR use select, ls and perl to offer a list of possible selections.
PS3="Select zip file to unpack: "
select file in `ls | perl -ne '/^(1\d|\d).*\.zip$/ && print'`
do
run_unpack "$file"
break
done
fi
rm -f "$zipOutput"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment