alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.09k stars 7.5k forks source link

内容相同时,每次生成的文件hash值不同 #3872

Closed dongbinlu closed 2 months ago

dongbinlu commented 2 months ago

异常代码

@Test
public void testhash()throws Exception{

    String fileName = TestFileUtil.getPath() + "hash" + ".xlsx";

    File file = new File(fileName);
    EasyExcel.write(file, DemoData.class)
            .sheet("模板")
            .doWrite(() -> {
                // 分页查询数据
                return data();
            });
    file.setLastModified(0L);
    file.setExecutable(false);
    file.setReadable(true);
    file.setWritable(true);

    File file1 = new File(fileName);
    byte[] bytes = calculateFileHash(file1);
    String hexString = HexUtils.toHexString(bytes);
    System.out.println(hexString);

}

public static byte[] calculateFileHash(InputStream inputStream) throws Exception {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    try (DigestInputStream dis = new DigestInputStream(inputStream, digest)) {

        byte[] buffer = new byte[1024 * 1024 * 500]; // 500 MB buffer
        int bytesRead;
        while ((bytesRead = dis.read(buffer)) != -1) {
            // Read buffer data and update digest
            digest.update(buffer, 0, bytesRead);
        }
        // Get the final hash value
        return digest.digest();
    }
}

异常提示

问题描述

文件内容相同,为什么每次生成的文件hash不一致

psxjoy commented 2 months ago

excel 本质是一个压缩包,里面存放了一些xml文件。文件的创建时间、所在环境都会影响hash值。这符合hash的定义。

psxjoy commented 2 months ago

不知道您是否已经解决该问题?我们先暂时关闭这个问题,如果还存在问题请重新Reopen这个issue。