开发者

Calculate md5 hash of a zip file in Java program

I have a zip file, and in my Java code i want to calculate the md5 hash of the zip file. Is there any java libary i can u开发者_如何学Pythonse for this purpose ?. Some example would be really appreciated.

Thank You


I got that working a few weeks ago with this Article here:

http://www.javalobby.org/java/forums/t84420.html

Just to have it a stackoveflow:

public static void main(String[] args) throws NoSuchAlgorithmException, FileNotFoundException {
MessageDigest digest = MessageDigest.getInstance("MD5");
File f = new File("c:\\myfile.txt");
InputStream is = new FileInputStream(f);                
byte[] buffer = new byte[8192];
int read = 0;
try {
    while( (read = is.read(buffer)) > 0) {
        digest.update(buffer, 0, read);
    }       
    byte[] md5sum = digest.digest();
    BigInteger bigInt = new BigInteger(1, md5sum);
    String output = bigInt.toString(16);
    System.out.println("MD5: " + output);
}
catch(IOException e) {
    throw new RuntimeException("Unable to process file for MD5", e);
}
finally {
    try {
        is.close();
    }
    catch(IOException e) {
        throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
    }
}       
}


There is also an option to do that with Apache Codec like that

final String sha256 = DigestUtils.sha256Hex(new FileInputStream(file))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜