1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#!/bin/sh
usage()
{
cat <<EOF
Usage:
$0 -o difference.zip -f from.zip -t to.zip
$0 -f from.zip -t to.zip
EOF
exit 1
}
output=
from=
to=
excludes=
while [ $# -gt 0 ]; do
o=$1
shift
case "$o" in
-o)
output=$1
shift
;;
-f)
from=$1
shift
;;
-t)
to=$1
shift
;;
-x)
excludes="$excludes $1"
shift
;;
*)
usage
;;
esac
done
[ -n "$from" ] || usage
[ -n "$to" ] || usage
found()
{
type=$1
source=$2
echo >&2 "$type: $source"
case "$type" in
new|changed|deleted)
echo "$source"
;;
excluded)
;;
deleted|*)
echo >&2 " * Sorry, can't handle deletion of $source."
;;
esac
}
tempdir=`mktemp -d -t zipdiff.XXXXXX`
newline="
"
fromlist="$(zipinfo -1 "$from" | grep -v /\$)"
tolist="$(zipinfo -1 "$to" | grep -v /\$)"
diffit()
{
echo "$fromlist" | while IFS= read -r line; do
case "$newline$tolist$newline" in
*$newline$line$newline*)
;;
*)
isexcluded=false
for P in $excludes; do
case "$line" in
$P)
found excluded "$line"
isexcluded=true
break
;;
esac
done
if ! $isexcluded; then
found deleted "$line"
fi
;;
esac
done
echo "$tolist" | while IFS= read -r line; do
case "$newline$fromlist$newline" in
*$newline$line$newline*)
# check if equal
isexcluded=false
for P in $excludes; do
case "$line" in
$P)
found excluded "$line"
isexcluded=true
break
;;
esac
done
if ! $isexcluded; then
unzip -p "$from" "$line" > "$tempdir/v1"
unzip -p "$to" "$line" > "$tempdir/v2"
if ! diff --brief "$tempdir/v1" "$tempdir/v2" >/dev/null 2>&1; then
found changed "$line"
fi
rm "$tempdir/v1"
rm "$tempdir/v2"
fi
;;
*)
# check if equal
isexcluded=false
for P in $excludes; do
case "$line" in
$P)
found excluded "$line"
isexcluded=true
break
;;
esac
done
if ! $isexcluded; then
found new "$line"
fi
;;
esac
done
}
result=`diffit`
case "$output" in
'')
;;
*)
rm -f "$output"
echo "$result" | while IFS= read -r line; do
echo >&2 "extracting $line..."
dline=./$line
mkdir -p "$tempdir/${dline%/*}"
unzip -p "$to" "$line" > "$tempdir/$line" # this may create an empty file - don't care, DP handles this as deletion
done
case "$output" in
/*)
;;
*)
output=`pwd`/$output
;;
esac
cd "$tempdir"
#zip -9r "$output" .
7za a -tzip -mx=9 "$output" .
cd ..
;;
esac
rm -rf "$tempdir"
|