on work/style help sign in
Multigram/apkdiff.py
63 lines 1.8 kB history raw
1import sys
2from zipfile import ZipFile
4def compareFiles(first, second):
5 while True:
6 firstBytes = first.read(4096)
7 secondBytes = second.read(4096)
8 if firstBytes != secondBytes:
9 return False
11 if firstBytes == b"" and secondBytes == b"":
12 break
14 return True
16def compare(first, second):
17 FILES_TO_IGNORE = ["META-INF/MANIFEST.MF", "META-INF/CERT.RSA", "META-INF/CERT.SF"]
19 firstZip = ZipFile(first, 'r')
20 secondZip = ZipFile(second, 'r')
22 firstList = list(filter(lambda firstInfo: firstInfo.filename not in FILES_TO_IGNORE, firstZip.infolist()))
23 secondList = list(filter(lambda secondInfo: secondInfo.filename not in FILES_TO_IGNORE, secondZip.infolist()))
25 if len(firstList) != len(secondList):
26 print("APKs has different amount of files (%d != %d)" % (len(firstList), len(secondList)))
27 return False
29 for firstInfo in firstList:
30 found = False
31 for secondInfo in secondList:
32 if firstInfo.filename == secondInfo.filename:
33 found = True
34 firstFile = firstZip.open(firstInfo, 'r')
35 secondFile = secondZip.open(secondInfo, 'r')
37 if compareFiles(firstFile, secondFile) != True:
38 print("APK file %s does not match" % firstInfo.filename)
39 return False
41 secondList.remove(secondInfo)
42 break
44 if found == False:
45 print("file %s not found in second APK" % firstInfo.filename)
46 return False
48 if len(secondList) != 0:
49 for secondInfo in secondList:
50 print("file %s not found in first APK" % secondInfo.filename)
51 return False
53 return True
55if __name__ == '__main__':
56 if len(sys.argv) != 3:
57 print("Usage: apkdiff <pathToFirstApk> <pathToSecondApk>")
58 sys.exit(1)
60 if sys.argv[1] == sys.argv[2] or compare(sys.argv[1], sys.argv[2]) == True:
61 print("APKs are the same!")
62 else:
63 print("APKs are different!")