当我尝试快速计算一个zip文件的MD5并同时将其解压缩时,对于某些zip文件,它不起作用:尽管文件未损坏,但MD5的总和不正确。

这是我的代码片段:

MessageDigest lMd = MessageDigest.getInstance("MD5");

FileOutputStream lFos = null;
try (DigestInputStream lDis = new DigestInputStream(lListener.getInputStream(), lMd);
                            ZipInputStream lZip = new ZipInputStream(lDis)) {

    // Read the response content
    //get the zipped file list entry
    ZipEntry lZipEntry = lZip.getNextEntry();

    while (lZipEntry != null) {
        String lFileName = lZipEntry.getName();

        File lNewFile = new File(UPDATE_FOLDER + File.separator + lFileName);

        if (lZipEntry.isDirectory()) {
            lNewFile.mkdirs();
        } else {
            //create all non exists folders
            new File(lNewFile.getParent()).mkdirs();

            lFos = new FileOutputStream(lNewFile);

            int lRead;
            while ((lRead = lZip.read(lBuffer)) > -1) {
                lFos.write(lBuffer, 0, lRead);
            }

            lFos.close();
        }

        lZipEntry = lZip.getNextEntry();
    }

    lZip.closeEntry();
} finally {
    if (lFos != null) {
        lFos.close();
}
                        byte[] lDigest = lMd.digest();

                    StringBuffer lHexString = new StringBuffer();
                    for (int lI = 0; lI < lDigest.length; lI++) {
                        if ((0xFF & lDigest[lI]) < 0x10) {
                            lHexString.append("0"
                                    + Integer.toHexString((0xFF & lDigest[lI])));
                        } else {
                            lHexString.append(Integer.toHexString(0xFF & lDigest[lI]));
                        }
                    }
                    String lDigestStr = lHexString.toString();


你能帮助我吗?

谢谢!
前!

最佳答案

您需要将这两个操作分开。 ZipInputStream不能完全消耗基础流。原因是ZIP文件格式,基本上是这样的:

<file header><file data>
<file header><file data>
<file header><file data>
<...>
<last file header><last file data>
<directory header>
<directory header>
<directory header>
<...>
<last directory header>


<file header><directory header>具有不同的签名。这是调用ZipInputStreamgetNextEntry()的作用:

if (get32(tmpbuf, 0) != LOCSIG) {
    return null;
}


它验证刚读取的缓冲区以字符“ PK \ 003 \ 004”开头,这将是正确的文件头。但是,一旦目录头开始,由于目录头是“ PK \ 001 \ 002”,它将开始返回null

此后,ZipInputStream将从null返回getNextEntry(),之后您将停止使用它。解决您特定问题的唯一方法是先验证文件,然后再将其解压缩。如评论中所提到的,您还是应该那样做,因为如果验证失败,则绝对不要使用该文件!

10-07 14:12