MD5 hash for zip files

Priya picture Priya · Jul 7, 2015 · Viewed 15.1k times · Source

Is it possible to generate MD5 hash for .zip files in java? All the examples I found were for .txt files.

I want to know when we unzip the data, edit a file, again zip it and find the hash, will it be different from the original one?

Answer

mastov picture mastov · Jul 7, 2015

You can create MD5 hashes for any arbitrary file, independently of the file type. The hash just takes any byte stream and doesn't interpret its meaning at all. So you can use the examples you have found for .txt files and apply them to .zip files.

And yes, editing a file inside the .zip will most likely change the MD5 of the .zip file - even though that's not guaranteed, due to hash collisions. But that's just a general property of hashes and has nothing to do with the zipping.

Note, however, that rezipping files may change the MD5 hash, even if the content has not changed. That's because even though the unzipped files are the same as before, the zipped file may vary depending on the used compression algorithm and its parameters.

EDIT (based on your comment):

If you want to avoid those changing MD5 hashes on rezipping, you have to run the MD5 on the unzipped files. You can do that on-the-fly without actually writing the files to disk, just by using streams. ZipInputStream helps you. A simple code example:

    InputStream theFile = new FileInputStream("example.zip");
    ZipInputStream stream = new ZipInputStream(theFile);
    try
    {
        ZipEntry entry;
        while((entry = stream.getNextEntry()) != null)
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
            DigestInputStream dis = new DigestInputStream(stream, md);
            byte[] buffer = new byte[1024];
            int read = dis.read(buffer);
            while (read > -1) {
                read = dis.read(buffer);
            }
            System.out.println(entry.getName() + ": "
                    + Arrays.toString(dis.getMessageDigest().digest()));
        }
    } finally { stream.close(); }