const hash256Sync = createFileHash256Sync(filePath)
const hash256Syncafter = createFileHash256Sync(filePath2)
if (hash256Sync == hash256Syncafter) {
fs.unlink(filePath2,(err,data)=>{
if (err) {
console.log(err);
} else{
console.log('删除文件成功');
}
})
}
export function createFileHash256(file) {
//从文件创建一个可读流
return new Promise((resolve, reject) => {
const stream = fs.createReadStream(file);
const fsHash = crypto.createHash('sha256');
stream.on('data', function (d) {
fsHash.update(d);
});
stream.on('end', function () {
const md5 = fsHash.digest('hex');
resolve(md5);
});
})
}
export function createFileHash256Sync(file) {
const crypto = require('crypto');
const fs = require('fs');
//读取一个Buffer
const buffer = fs.readFileSync(file);
const fsHash = crypto.createHash('sha256');
fsHash.update(buffer);
const md5 = fsHash.digest('hex');
return md5
}