Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MD5 hash for zip files

Tags:

java

zip

md5-file

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?

like image 670
Priya Avatar asked Jul 07 '15 11:07

Priya


People also ask

What is a zip MD5 file?

An MD5 file is a checksum file used for the verification of a file's integrity. It is similar to a fingerprint for the associated file and is uniquely generated using an algorithm that uses the number of bits in the file. It is used to verify the file with software such as md5sum.

Do zip files have hash?

Because the ZIP file contains the file name. So if you change the name of the zipped files this changes the data in the ZIP, and therefore the hash of the ZIP file. To compare ZIPs, it's best to uncompress them, unless you use something that mounts them as a device.


1 Answers

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(); }
like image 142
mastov Avatar answered Sep 19 '22 10:09

mastov