apkdiff.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import sys
  2. from zipfile import ZipFile
  3. def compareFiles(first, second):
  4. while True:
  5. firstBytes = first.read(4096);
  6. secondBytes = second.read(4096);
  7. if firstBytes != secondBytes:
  8. return False
  9. if firstBytes == b"":
  10. break
  11. return True
  12. def compare(first, second):
  13. FILES_TO_IGNORE = ["META-INF/MANIFEST.MF", "META-INF/CERT.RSA", "META-INF/CERT.SF"]
  14. firstZip = ZipFile(first, 'r')
  15. secondZip = ZipFile(second, 'r')
  16. firstList = list(filter(lambda firstInfo: firstInfo.filename not in FILES_TO_IGNORE, firstZip.infolist()))
  17. secondList = list(filter(lambda secondInfo: secondInfo.filename not in FILES_TO_IGNORE, secondZip.infolist()))
  18. if len(firstList) != len(secondList):
  19. print("APKs has different amount of files (%d != %d)" % (len(firstList), len(secondList)))
  20. return False
  21. for firstInfo in firstList:
  22. found = False
  23. for secondInfo in secondList:
  24. if firstInfo.filename == secondInfo.filename:
  25. found = True
  26. firstFile = firstZip.open(firstInfo, 'r')
  27. secondFile = secondZip.open(secondInfo, 'r')
  28. if compareFiles(firstFile, secondFile) != True:
  29. print("APK file %s does not match" % firstInfo.filename)
  30. return False
  31. secondList.remove(secondInfo)
  32. break
  33. if found == False:
  34. print("file %s not found in second APK" % firstInfo.filename)
  35. return False
  36. if len(secondList) != 0:
  37. for secondInfo in secondList:
  38. print("file %s not found in first APK" % secondInfo.filename)
  39. return False
  40. return True
  41. if __name__ == '__main__':
  42. if len(sys.argv) != 3:
  43. print("Usage: apkdiff <pathToFirstApk> <pathToSecondApk>")
  44. sys.exit(1)
  45. if sys.argv[1] == sys.argv[2] or compare(sys.argv[1], sys.argv[2]) == True:
  46. print("APKs are the same!")
  47. else:
  48. print("APKs are different!")