目前,我的应用程序将对磁盘上的文件列表执行压缩过程,并允许用户作为备份目的的电子邮件附件发送。
为了具有数据损坏检测能力,我使用以下方法生成校验和
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 文件内容的一部分。
然而,这会产生一个悖论。
- 如果我将校验和文件写入 zip 文件,之前计算的校验和将不再有效。
- 如果我想在将校验和文件写入 zip 文件后才计算校验和,那么校验和文件的内容应该是什么?
最佳答案
由于您制定了自己的校验和计算和验证算法,我认为您可以做出以下假设:zip 文件包含数据和元数据。数据是用户从他的磁盘中选择的文件。元数据是包含数据校验和的文件,也可以在 zip 中获得。
那么校验和不是根据完整的 zip 文件计算的,而是仅根据用户的数据计算的,然后将其添加到 zip 文件不会更改校验和。
关于java - 如何编写正确的 zipfile 校验和值,并将其作为 zipfile 内容的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469689/