Get the crc32 of the passed file. The crc32 of a file can be used for error checking; two files with the same crc32 are considered equivalent. Note that the entire file must be read to produce the crc32. Args: filename: string, path to a file block_size: Integer, process the files
(filename, block_size=_DEFAULT_BLOCK_SIZE)
| 775 | |
| 776 | |
| 777 | def file_crc32(filename, block_size=_DEFAULT_BLOCK_SIZE): |
| 778 | """Get the crc32 of the passed file. |
| 779 | |
| 780 | The crc32 of a file can be used for error checking; two files with the same |
| 781 | crc32 are considered equivalent. Note that the entire file must be read |
| 782 | to produce the crc32. |
| 783 | |
| 784 | Args: |
| 785 | filename: string, path to a file |
| 786 | block_size: Integer, process the files by reading blocks of `block_size` |
| 787 | bytes. Use -1 to read the file as once. |
| 788 | |
| 789 | Returns: |
| 790 | hexadecimal as string, the crc32 of the passed file. |
| 791 | """ |
| 792 | crc = 0 |
| 793 | with FileIO(filename, mode="rb") as f: |
| 794 | chunk = f.read(n=block_size) |
| 795 | while chunk: |
| 796 | crc = binascii.crc32(chunk, crc) |
| 797 | chunk = f.read(n=block_size) |
| 798 | return hex(crc & 0xFFFFFFFF) |