0

我编写了以下Java方法来读取文件的所有条目并基于FILE CONTENT ONLYZipInputStream处理它。在我的课堂上,我有:MD5Tczip

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

所以我想处理MD5所有这些 zip 文件,并比较它们是否匹配:两个 EQUAL(IN CONTENT) 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,但不是全部。有什么建议么?我究竟做错了什么?

4

1 回答 1

0

我相信以下代码行:

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 = entry.getNextEntry(); Or 的方式做一些事情,以减轻对其他人的痛苦,请执行以下操作:currentEntry = entry.getNextEntry();

于 2012-08-22T12:45:15.280 回答