Last active
January 25, 2025 21:30
-
-
Save Bigjango13/6eb737070ab92f3f9502cb6af159a48f to your computer and use it in GitHub Desktop.
Tool for diffing zip files (and related files, like jars)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zipfile | |
from sys import argv, exit | |
# Open | |
if len(argv) < 3: | |
print("Usage: zipdiff.py old.zip new.zip <dont smoosh folders") | |
exit(0) | |
za, zb = zipfile.ZipFile(argv[1]), zipfile.ZipFile(argv[2]) | |
sa, sb = set(za.infolist()), set(zb.infolist()) | |
da = {i.filename:i for i in sa} | |
db = {i.filename:i for i in sb} | |
smoosh_folders = True | |
if len(argv) > 3: | |
smoosh_folders = False | |
# Get stuff | |
def remove_underlings(l): | |
l = list(set(l)) | |
l.sort() | |
if not smoosh_folders: | |
return l | |
dirs = {} | |
ret = [] | |
for i in l: | |
if (dir := next((x for x in list(dirs.keys()) if i.startswith(x)), False)): | |
dirs[dir] += 1 | |
elif i.endswith("/"): | |
dirs[i] = 0 | |
else: | |
ret.append(i) | |
# Add back the dirs | |
ret.extend([p + " (" + str(k) + " files)" for (p,k) in dirs.items()]) | |
return ret | |
added = remove_underlings([i.filename for i in sb if not da.get(i.filename)]) | |
removed = remove_underlings([i.filename for i in sa if not db.get(i.filename)]) | |
modified = remove_underlings([i.filename for i in sb if (j:=da.get(i.filename)) and i.CRC != j.CRC]) | |
def plist(p,l): | |
p += ": " | |
print(p, end='') | |
print(*l, sep="\n" + p) | |
plist("Added", added) | |
plist("Removed", removed) | |
plist("Modified", modified) | |
# Print summary | |
print("\nSummary:") | |
print("- Files in A: " + str(len(sa))) | |
print("- Files in B: " + str(len(sb))) | |
print("- Added: " + str(len(added))) | |
print("- Removed: " + str(len(removed))) | |
print("- Modified: " + str(len(modified))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment