![](/img/trans.png)
[英]Question about reading from a ZipInputStream/ZipEntry in java
[英]IndexOutOfBoundsException when reading from a ZipInputStream Java
我正在嘗試從stackoverflow中的上一個問題實現此算法描述:
所以我實施了
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中得到一個Exception。 有人知道為什么嗎? 另外,如果我的算法正確實現,你能告訴我嗎?
你要兩次調用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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.