[英]MD5 being returned is the same even after digesting the files - Java
我編寫了以下Java
方法來讀取ZipInputStream
文件的所有條目,並僅基於FILE CONTENT處理其MD5
。 在我的班級Tczip
我有:
public String digest( ZipInputStream entry ) throws IOException{
byte[] digest = null;
MessageDigest md5 = null;
String mdEnc = "";
ZipEntry current;
try {
md5 = MessageDigest.getInstance( "MD5" );
if( entry != null ) {
while(( current = entry.getNextEntry() ) != null ) {
if( current.isDirectory() ) {
digest = this.encodeUTF8( current.getName() );
md5.update( digest );
}
else{
int size = ( int )current.getSize();
if(size > 0){
digest = new byte[ size ];
entry.read( digest, 0, size );
md5.update( digest );
}
}
}
digest = md5.digest();
mdEnc = new BigInteger( 1, md5.digest() ).toString( 16 );
entry.close();
}
}
catch ( NoSuchAlgorithmException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalArgumentException ex){
System.out.println("There is an illegal encoding.");
//
// The fix for Korean/Chinese/Japanese encodings goes here
//
Charset encoding = Charset.forName("utf-8");
ZipInputStream zipinputstream =
new ZipInputStream(new FileInputStream( this.filename ), encoding);
digest = new byte[ 1024 ];
current = zipinputstream.getNextEntry();
while (current != null) { //for each entry to be extracted
String entryName = current.getName();
System.out.println("Processing: " + entryName);
int n;
FileOutputStream fileoutputstream =
new FileOutputStream( this.filename );
while (( n = zipinputstream.read( digest, 0, 1024 )) > -1) {
fileoutputstream.write(digest, 0, n);
}
fileoutputstream.close();
zipinputstream.closeEntry();
current = zipinputstream.getNextEntry();
}//while
zipinputstream.close();
}
return mdEnc;
}
public byte[] encodeUTF8( String name ) {
final Charset UTF8_CHARSET = Charset.forName( "UTF-8" );
return name.getBytes( UTF8_CHARSET );
}
然后,程序將遍歷根目錄(aka C:\\workspace\\path\\to\\source\\code
),遍歷所有目錄,查找要處理的.zip
文件。 這些文件進入File[] files
:
public void showFiles( File[] files ){
for( File file : files ){
if( file.isDirectory() ) {
showFiles( file.listFiles( this.filter ) );
}
else {
try {
String path = file.getCanonicalPath();
String relative = path.replace("tc10.0.0.2012080100_A", "tc10.0.0.2012080600_C" );
File b = new File(relative);
if( b.exists() ) {
System.out.println( "Processing :" + file.getName() );
this.zip_a = new Tczip( path );
this.zip_b = new Tczip( relative );
String md5_a = this.zip_a.digest();
String md5_b = this.zip_b.digest();
System.out.println("MD5 A: " + md5_a);
System.out.println("MD5 B: " + md5_b);
if( md5_a.equals( md5_b )){
System.out.println( "They Match" );
}
else {
System.out.println( "They don't Match" );
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
因此,我想處理所有這些zip文件的MD5
,並比較它們是否匹配: 預計兩個相等(含內容)的ZIP文件具有相同的MD5 。 如果文件內容不同,則MD5
將不同。 但是,當我執行程序時,我有:
Processing :web.zip
MD5 A: d41d8cd98f00b204e9800998ecf8427e
MD5 B: d41d8cd98f00b204e9800998ecf8427e
They Match
Processing :weldmgmt_icons.zip
MD5 A: d41d8cd98f00b204e9800998ecf8427e
MD5 B: d41d8cd98f00b204e9800998ecf8427e
They Match
Processing :weldmgmt_install.zip
MD5 A: d41d8cd98f00b204e9800998ecf8427e
MD5 B: d41d8cd98f00b204e9800998ecf8427e
They Match
Processing :weldmgmt_template.zip
MD5 A: d41d8cd98f00b204e9800998ecf8427e
MD5 B: d41d8cd98f00b204e9800998ecf8427e
They Match
為什么它們相同的MD5
? 我希望兩個文件具有相同的MD5
,但不是全部。 有什么建議么? 我究竟做錯了什么?
我相信以下代碼行:
while(( current = entry.getNextEntry() ) != null ) {
if( current.isDirectory() ) {
digest = this.encodeUTF8( current.getName() );
md5.update( digest );
}
else{
int size = ( int )current.getSize();
if(size > 0){
digest = new byte[ size ];
entry.read( digest, 0, size );
md5.update( digest );
}
}
}
是此實現失敗的地方。 因此,查看API,對entry.getNextEntry()的調用將返回下一個要處理的文件。 但是,如果該值不是directory
,則將其丟棄。 因此,有理由認為散列是相同的,因為您每次entry.read
在entry.read
行中處理相同的文件。
更新
要解決此問題,您應該能夠按照entry = entry.getNextEntry();
或者,為了currentEntry = entry.getNextEntry();
對他人的痛苦,請執行以下操作: currentEntry = entry.getNextEntry();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.