java - 如何编写正确的 zipfile 校验和值,并将其作为 zipfile 内容的一部分

标签 java algorithm checksum

目前,我的应用程序将对磁盘上的文件列表执行压缩过程,并允许用户作为备份目的的电子邮件附件发送。

为了具有数据损坏检测能力,我使用以下方法生成校验和

public static long getChecksum(File file) {
    FileInputStream stream = null;
    CheckedInputStream cis = null;
    try {
        // Compute Adler-32 checksum
        stream = new FileInputStream(file);
        cis = new CheckedInputStream(stream, new Adler32());
        byte[] tempBuf = new byte[128];
        while (cis.read(tempBuf) >= 0) {
        }
        long checksum = cis.getChecksum().getValue();
        return checksum;
    } catch (IOException ex) {
        Log.e(TAG, "", ex);
    } finally {
        org.yccheok.gui.Utils.close(cis);
        org.yccheok.gui.Utils.close(stream);
    }
    return 0;
}

我倾向于将校验和信息作为文件名嵌入

myapp_12-dec-15_3659593860.zip

对我来说,文件名不是很友好,

我希望我能有这样的文件名

myapp_12-dec-15.zip

如果我想实现这一点,我需要将校验和写入一个文本文件,并将其作为 zip 文件内容的一部分。

然而,这会产生一个悖论。

  1. 如果我将校验和文件写入 zip 文件,之前计算的校验和将不再有效。
  2. 如果我想在将校验和文件写入 zip 文件后才计算校验和,那么校验和文件的内容应该是什么?

最佳答案

由于您制定了自己的校验和计算和验证算法,我认为您可以做出以下假设:zip 文件包含数据和元数据。数据是用户从他的磁盘中选择的文件。元数据是包含数据校验和的文件,也可以在 zip 中获得。

那么校验和不是根据完整的 zip 文件计算的,而是仅根据用户的数据计算的,然后将其添加到 zip 文件不会更改校验和。

关于java - 如何编写正确的 zipfile 校验和值,并将其作为 zipfile 内容的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469689/

相关文章:

algorithm - 无逻辑/按位运算的校验和

java - Spring根据当前范围注入(inject)不同的bean

java - Android 开发切换 TextView 可见性

algorithm - 通过边缘循环分割 3D 模型

C++ - 构造 ICMP 数据包时出现访问冲突

performance - 为什么这个 Go 代码的速度与 Python 的速度相当(而且快不了多少)?

java - 无法在启用身份验证的情况下使用 DriverManager.getConnection(url,properties) 连接到 Derby

java - 我想在文本字段中输入 1 到 15 之间的数字

algorithm - paranthesized 字符串并行处理的有效性

python - Grover 算法的大简化版是什么?