2011-03-14 94 views
2

我有一個zip文件,在我的Java代碼中,我想計算zip文件的md5哈希值。有沒有可用於此目的的Java庫?一些例子將非常感激。在Java程序中計算zip文件的md5哈希值

謝謝

+0

能否請您看看頁面的右下角進入「相關」部分?謝謝。 – bezmax

+0

我看了一下相關的問題,無法找到與Java中的文件哈希有關的問題。有一些用於Java中的Strings和Perl中的文件等,但沒有用於Java中的File。 – Chris

+0

對不起,我發現一個:http://stackoverflow.com/questions/304268/using-java-to-get-a-files-md5-checksum – Chris

回答

4

我得到了工作在幾個星期前本條這裏:

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

正好有它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); 
    } 
}  
} 
+0

謝謝克里斯,這工作! :) – Joey

+0

有一個更簡單的方法,請看看http://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java – Randyaa