Search This Blog

Python: generates crc32 and adler32 checksum for big files


import zlib
import sys
import urllib2

def __zlib_csum(url, func):
if isinstance(url, basestring if sys.version_info[0] < 3 else str):
url = urllib2.Request(url)
f = urllib2.urlopen(url)
csum = None
try:
chunk = f.read(1024)
if len(chunk)>0:
csum = func(chunk)
while True:
chunk = f.read(1024)
if len(chunk)>0:
csum = func(chunk, csum)
else:
break
finally:
f.close()
if csum is not None:
csum = csum & 0xffffffff
return csum


def crc32(url):
return __zlib_csum(url, zlib.crc32)

def adler32(url):
return __zlib_csum(url, zlib.adler32)

if __name__ == '__main__':
print(hex(crc32('file:/tmp/111.zip')))
print(hex(adler32('file:/tmp/111.zip')))

No comments:

Post a Comment