MD5 hash for zip files

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

I want to know when we unzip the data, edit the file, button it again and find the hash, will it differ from the original?

+4
source share
1 answer

You can create MD5 hashes for any arbitrary file, regardless of file type. The hash simply takes any stream of bytes and does not interpret its value at all. That way, you can use the examples you found for .txt files and apply them to .zip files.

, .zip, , MD5 .zip - , - -. .

, , MD5, . , , , , , zipped .

( ):

MD5 , MD5 . " ", , . ZipInputStream . :

    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(); }
+4

All Articles