Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
即使在消化文件之后,返回的MD5也是相同的-Java_Java - Fatal编程技术网

即使在消化文件之后,返回的MD5也是相同的-Java

即使在消化文件之后,返回的MD5也是相同的-Java,java,Java,我编写了以下Java方法来读取ZipInputStream文件的所有条目,并仅基于文件内容处理其MD5。在我的班级里,我有: public String digest( ZipInputStream entry ) throws IOException{ byte[] digest = null; MessageDigest md5 = null; String mdEnc = ""; ZipEn

我编写了以下
Java
方法来读取
ZipInputStream
文件的所有条目,并仅基于文件内容处理其
MD5
。在我的班级里,我有:

 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
文件。这些文件进入
文件[]文件

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 );
                        }
                    }
                }
这是实现失败的地方。那么看看 调用entry.getNextEntry()将返回下一个要处理的文件。但是,如果该值不是
目录
,则将丢弃该值。因此,哈希值相同是有道理的,因为您只是在
条目中处理相同的文件。每次读取

更新

要解决这个问题,您应该能够按照
entry=entry.getNextEntry()的思路来做一些事情

或者,为了减轻别人的痛苦,可以执行以下操作:
currentEntry=entry.getNextEntry()

你确定zip文件实际上不同吗?是的,它们有不同的时间戳,文件内容谢谢,你有什么建议我如何解决它吗?现在还不清楚我在其他方面要做什么来解决问题…:-(@philippe您必须将entry.getnextery返回的值赋给一个变量。目前为止,您只是在丢弃它。但我在
while
循环中执行此操作,没有?
ZipEntry current;
然后我在
while((current=entry.getnextery())!=null)
不能完全做到这一点吗?@philippe确实做到了,但您从未使用过当前的
current