简体   繁体   English

从ZipInputStream Java读取时的IndexOutOfBoundsException

[英]IndexOutOfBoundsException when reading from a ZipInputStream Java

I'm trying to implement this algorithm description from a previous question I had here in stackoverflow: 我正在尝试从stackoverflow中的上一个问题实现此算法描述:

suppressing or not allowing the access time to be modified java 抑制或不允许修改访问时间java

so I implemented as 所以我实施了

byte[] digest = new byte[this.BUFFER];
        MessageDigest md5;

        try {
            md5 = MessageDigest.getInstance("MD5");

            while(entry.getNextEntry() != null){

                ZipEntry current = entry.getNextEntry();

                if(current.isDirectory()){
                    digest = this.encodeUTF8(current.getName());
                    md5.update(digest);
                }
                else{
                        entry.read(digest, 0, this.BUFFER);
                        md5.update(digest);
                }
            }
            digest = md5.digest();
            entry.close();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

However, I'm getting a Exception in thread "main" java.lang.IndexOutOfBoundsException in the else statement. 但是,我在else语句中的线程“main”java.lang.IndexOutOfBoundsException中得到一个Exception。 Does someone know why? 有人知道为什么吗? Also, could you please tell me if my algorithm was correctly implemented? 另外,如果我的算法正确实现,你能告诉我吗?

You're calling getNextEntry() twice, instead of once: 你要两次调用getNextEntry() ,而不是一次:

while (entry.getNextEntry() != null) { // goes to the next entry
    ZipEntry current = entry.getNextEntry(); // goes to the next entry

Use this instead: 请改用:

ZipEntry current;
while ((current = entry.getNextEntry()) != null) {
    // use current   
}

or 要么

for (ZipEntry current = entry.getNextEntry(); current != null; current = entry.getNextEntry()) {
    // use current
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM