| 5 |
| while True: |
| 6 | firstBytes = first.read(4096); |
| 7 | secondBytes = second.read(4096); |
| 8 | if firstBytes != secondBytes: |
| 9 | return False |
| 10 |
| 11 | if firstBytes == b"": |
| 12 | break |
| 13 |
| 14 | return True |
| 15 |
| 16 | def compare(first, second): |
| 17 | FILES_TO_IGNORE = ["META-INF/MANIFEST.MF", "META-INF/CERT.RSA", "META-INF/CERT.SF"] |
| 18 |
| 19 | firstZip = ZipFile(first, 'r') |
| 20 | secondZip = ZipFile(second, 'r') |
| 21 |
| 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())) |
| 24 |
| 25 | if len(firstList) != len(secondList): |
| 26 | print("APKs has different amount of files (%d != %d)" % (len(firstList), len(secondList))) |
| 27 | return False |
| 28 |
| 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') |
| 36 |
| 37 | if compareFiles(firstFile, secondFile) != True: |
| 38 | print("APK file %s does not match" % firstInfo.filename) |
| 39 | return False |
| 40 |
| 41 | secondList.remove(secondInfo) |
| 42 | break |
| 43 |
| 44 | if found == False: |
| 45 | print("file %s not found in second APK" % firstInfo.filename) |
| 46 | return False |
| 47 |
| 48 | if len(secondList) != 0: |
| 49 | for secondInfo in secondList: |
| 50 | print("file %s not found in first APK" % secondInfo.filename) |
| 51 | return False |
| 52 |
| 53 | return True |
| 54 |
| 55 | if __name__ == '__main__': |
| 56 | if len(sys.argv) != 3: |
| 57 | print("Usage: apkdiff <pathToFirstApk> <pathToSecondApk>") |
| 58 | sys.exit(1) |
| 59 |
| 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!") |
| 64 |