【问题标题】:IndexOutOfBoundsException when reading from a ZipInputStream Java从 ZipInputStream Java 读取时出现 IndexOutOfBoundsException
【发布时间】:2012-06-14 18:42:27
【问题描述】:

我正在尝试从我在 stackoverflow 中遇到的上一个问题中实现此算法描述:

suppressing or not allowing the access time to be modified java

所以我实现为

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

但是,我在 else 语句中的线程“main”java.lang.IndexOutOfBoundsException 中遇到异常。有人知道为什么吗?另外,请问我的算法是否正确实现?

【问题讨论】:

    标签: java exception zip nullpointerexception indexoutofboundsexception


    【解决方案1】:

    你打电话给getNextEntry() 两次,而不是一次:

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

    改用这个:

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

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

    【讨论】:

    • 我应该使用什么来知道是否有要读取的条目?
    • 大小:0 名称:gs_info/ 大小:624 名称:gs_info/adobeillustrator.des 线程“main”中的异常 java.lang.IndexOutOfBoundsException 所以当它读取该文件并执行其任务时MD5,我明白了
    • else { 使用 ZipInputStream.read() sha1.update( bytes ) 读取条目字节; } 我不明白那部分...
    • 你是在告诉我你不理解你写的代码,这不是你问的问题的一部分吗?我能做什么?和这个问题有什么关系?
    • 您已经帮了很多忙,谢谢!我的问题是,如果它是一个文件,那么我需要将它作为 ZipInputStream 读取到 byte[] 缓冲区中,然后更新它的 md5。我不明白如何从 ZipEntry 创建 ZipInputStream
    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 2012-05-20
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多