#!/bin/sh
################################################################################
# Script name: zip_cleaner_v0.2.sh #
# Author: SoD- #
# Contact: SoD- @ EFNet #
# Releasedate: 2004-07-07 #
# #
# This script uses zip and unzip to remove garbage files and comments from zip #
# files. Just run it with the file to be cleaned as argument. It can also #
# extract nfo and diz. #
# #
# Installation: -Add any files you want denied in the $REMOVE_LIST variable. #
# -For usage with Dark0n3's zipscript-c: #
# +Add "char *zip_cleaner_arg;" to the variable declarations in #
# zipscript-c.c's main(). #
# +Add these lines above the line "d_log("Integrity ok\n");" : #
# if((zip_cleaner_arg = malloc(strlen(argv[2]) + strlen(argv[1]) + 26)) == NULL) {
# d_log("zip_cleaner: couldn't allocate memory, aborting\n");#
# break; #
# } #
# sprintf(zip_cleaner_arg, "zip_cleaner_v0.1beta.sh %s/%s\n", argv[2], argv[1]);
# if(system(zip_cleaner_arg) != 0) #
# d_log("zip_cleaner: failed to execute\n"); #
# else #
# d_log("zip_cleaner: executed ok\n"); #
# #
# Requirements: awk, chown, echo, grep, ls, unzip, zip, zipinfo (symlink #
# pointing to unzip). #
# #
# Limitations: -Can only handle file names without spaces. #
# #
# Changelog: v0.1beta -> v0.2: Made the match for unwanted files case #
# insensitive. Also added support for #
# extraction of .nfo and .diz files. #
# Changes in settings: Use lower case in $REMOVE_LIST and set #
# EXTRACT_NFO_DIZ and EXTRACT_TO_LOWER. #
# NOTE: I normally wouldn't dream of using site names in a script but #
# since those listed in $REMOVE_LIST have been found in loads of #
# zip files i guess they want the publicity. #
################################################################################
# Add any file names you want removed from zip files here. In lower case!
REMOVE_LIST="nf2k2.nfo twh.nfo ph.nfo bar.nfo ti.nfo usux.nfo paranoia.nfo
echobase.nfo nia.nfo trope.exe stb.nfo ghetto.txt tnl.nfo tnl.exe
pre.nfo hello.kitty rb.nfo pbox.nfo kane.ans kane.jpg kane.nfo
asylum.txt ghetto.nfo tqs.nfo topcn.nfo spy.nfo ts.nfo playboys.nfo
dl.nfo ptp.nfo wizard.nfo bkn.nfo wto.nfo me.nfo tdi.nfo dm.nfo
kane-xmas.nfo tct.nfo atom.nfo tp.nfo vdrlake.nfo"
# Do you want the script to extract any remaining .nfo and .diz files? YES/NO.
# (.nfo and .diz files in subdirectories isn't processed.)
EXTRACT_NFO_DIZ=YES
# If EXTRACT_NFO_DIZ=YES, do you want the extracted .nfo and .diz to be lower
# case or left as they are? Set to "YES" for lower case and "NO" to leave as is.
EXTRACT_TO_LOWER=YES
################################################################################
# No more settings needed below, just some editing if the script won't work.
################################################################################
if [ ! $1 ] || [ ! -f $1 ] || [ ! -w $1 ]; then
exit 1
fi
OWNER=`ls -l $1 | awk '{print $3}'`
GROUP=`ls -l $1 | awk '{print $4}'`
FILE_LIST=`unzip -l $1 | tail -n+4 | awk '{print $4}' | grep -iv "\.rar$\| \
\.r[[:digit:]][[:digit:]]$\|\.zip$\|\.ace\|\.c[[:digit:]][[:digit:]]$\|\.diz$"`
# This outer loop goes thru each file in the zip (except those excluded above).
for FILE in $FILE_LIST; do
# This inner loop removes files listed in $REMOVE_LIST from the zip.
for REMOVE_FILE in $REMOVE_LIST; do
if [ "`echo $FILE | tr '[:upper:]' '[:lower:]'`" == "$REMOVE_FILE" ]; then
#echo "Removing $REMOVE_FILE from `basename $1`"
zip -dq "$1" "$FILE"
fi
done
done
#echo "Removing any multi-line comment from `basename $1`"
zip -qz "$1" < .
chown $OWNER:$GROUP "$1"
if [ $EXTRACT_NFO_DIZ == YES ]; then
#echo "Extracting .nfo and .diz files from `basename $1`"
EXTRACTED_FILES=`unzip -Cn "$1" "*.nfo" "*.diz" -x */* | \
grep "\<inflating: \<" | awk '{print $2}'`
for FILE in $EXTRACTED_FILES; do
chown $OWNER:$GROUP "$FILE"
if [ $EXTRACT_TO_LOWER == YES ]; then
mv "$FILE" "`echo $FILE | tr '[:upper:]' '[:lower:]'`"
fi
done
fi
exit