Skip to content

Instantly share code, notes, and snippets.

@guilt
Last active December 13, 2023 14:01
Show Gist options
  • Select an option

  • Save guilt/af13bfc22602075be32539eef43e21f2 to your computer and use it in GitHub Desktop.

Select an option

Save guilt/af13bfc22602075be32539eef43e21f2 to your computer and use it in GitHub Desktop.
Java Archive Verifier
#!/bin/sh
# Usage: jar-verify FILE-OR-DIR-TO-VERIFY
#
# Histogram: ./jar-verify test.jar | cut -d: -f 2 | uniq -c | sort -rn
which which 2>&1 >/dev/null || { echo "Ensure Which is Installed."; exit 1; }
which file 2>&1 >/dev/null || { echo "Ensure File Type Detector - file is Installed."; exit 1; }
which unzip 2>&1 >/dev/null || { echo "Ensure Unzip is Installed."; exit 1; }
[ -z "$TMPDIR " ] && { TMPDIR=/tmp; echo "Temporary Directory is now: $TMPDIR"; }
function checkClassType() {
local CO="$1"
local QUAL="$2"
local OUTPUT=`file "$CO" 2>/dev/null | cut -d: -f 2-`
echo "$QUAL: $OUTPUT"
}
function checkDirType() {
local DO="$1"
local QUAL="$2"
[ -d "$DO" ] && {
ls "$DO" | while read file; do
checkFile "$DO/$file" "$QUAL/$file"
done
}
}
function extractArchiveToDir() {
local AO="$1"
local DD="$2"
unzip -d "$DD" -o -q "$AO"
}
function checkArchiveType() {
local AO="$1"
local QUAL="$2"
local TMP="rand-$RANDOM-`date +%Y-%m-%d`"
mkdir -p "$TMPDIR/$TMP" && {
extractArchiveToDir "$AO" "$TMPDIR/$TMP" && checkDirType "$TMPDIR/$TMP" "$QUAL"
rm -fr "$TMPDIR/$TMP/"
}
}
function checkFile() {
local FO=$1
local QUAL=$2
case $1 in
*.class)
checkClassType "$FO" "$QUAL"
;;
*.war|*.ear|*.jar|*.zip)
checkArchiveType "$FO" "$QUAL"
;;
*)
checkDirType "$FO" "$QUAL"
;;
esac
}
[ -z "$1" ] && { echo "Syntax is: $0 FILE-OR-DIR-TO-VERIFY" ; exit 1 ; }
while [ ! -z "$1" ]; do
checkFile "$1" "$1"
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment