Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an MD5 Hash of A ZipFile

Tags:

python

I want to create an MD5 hash of a ZipFile, not of one of the files inside it. However, ZipFile objects aren't easily convertible to streams.

from hashlib import md5
from zipfile import ZipFile

zipped = ZipFile(r'/Foo/Bar/Filename.zip')
hasher = md5()
hasher.update(zipped)

return hasher.hexdigest()

The above code generates the error :TypeError: must be convertible to a buffer, not ZipFile.

Is there a straightforward way to turn a ZipFile into a stream?

There's no security issues here, I just need a quick an easy way to determine if I've seen a file before. hash(zipped) works fine, but I'd like something a little more robust if possible.

like image 346
Batman Avatar asked Nov 29 '25 03:11

Batman


1 Answers

Just open the ZipFile as a regular file. Following code works on my machine.

from hashlib import md5
m = md5()
with open("/Foo/Bar/Filename.zip", "rb") as f:
    data = f.read() #read file in chunk and call update on each chunk if file is large.
    m.update(data)
    print m.hexdigest()
like image 120
Pant Avatar answered Dec 01 '25 17:12

Pant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!