Creating an MD5 Hash of A ZipFile
<p>I want to create an MD5 hash of a <code>ZipFile</code>, not of one of the files inside it. However, <code>ZipFile</code> objects aren't easily convertible to streams.</p>

<pre><code>from hashlib import md5
from zipfile import ZipFile

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

return hasher.hexdigest()
</code></pre>

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

<p>Is there a straightforward way to turn a <code>ZipFile</code> into a stream?</p>

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

<p>This function should return the MD5 hash of any file, provided it's path (requires <code>pycrypto</code> module):</p>

<pre><code>from Crypto.Hash import MD5


def get_MD5(file_path):

    chunk_size = 8192

    h = MD5.new()

    with open(file_path, 'rb') as f:
        while True:
            chunk = f.read(chunk_size)
            if len(chunk):
                h.update(chunk)
            else:
                break

    return h.hexdigest()

print get_MD5('pics.zip') # example
</code></pre>

<p><strong>output:</strong></p>

<pre><code>6a690fa3e5b34e30be0e7f4216544365
</code></pre>

<p><a href="https://pypi.python.org/pypi/pycrypto" rel="nofollow">Info on pycrypto</a></p>

python