如何检查两个文件是否具有相同的内容

How to check if two files have the same content?

本文关键字:是否 两个 何检查 检查 文件      更新时间:2023-09-26

我正在使用mocha/supertest/should.js测试REST服务

GET /files/<hash>将文件作为流返回。

如何在should.js中断言文件内容相同?

it('should return file as stream', function (done) {
    var writeStream = fs.createWriteStream('test/fixtures/tmp.json');
    
    var req = api.get('/files/676dfg1430af3595');
    req.on('end', function(){
       var tmpBuf = fs.readFileSync('test/fixtures/tmp.json');
       var testBuf = fs.readFileSync('test/fixtures/test.json');
    
       // How to assert with should.js file contents are the same (tmpBuf == testBuf )
       // ...
    
       done();
    });
});

令人惊讶的是,没有人建议使用Buffer.equals。这似乎是最快、最简单的方法,自v0.11以来就一直存在。

因此,您的代码将变成tmpBuf.equals(testBuf)

您有3个解决方案:

第一个

比较结果字符串

tmpBuf.toString() === testBuf.toString();

第二

使用循环逐字节读取缓冲区

var index = 0,
    length = tmpBuf.length,
    match = true;
while (index < length) {
    if (tmpBuf[index] === testBuf[index]) {
        index++;
    } else {
        match = false;
        break;
    }
}
match; // true -> contents are the same, false -> otherwise

第三

使用第三方模块,如buffertools和buffertools.compare(buffer,buffer|string)方法。

should.js中,您可以使用.eql来比较Buffer的实例:

> var buf1 = new Buffer('abc');
undefined
> var buf2 = new Buffer('abc');
undefined
> var buf3 = new Buffer('dsfg');
undefined
> buf1.should.be.eql(buf1)
...
> buf1.should.be.eql(buf2)
...
> buf1.should.be.eql(buf3)
AssertionError: expected <Buffer 61 62 63> to equal <Buffer 64 73 66 67>
    ...
> 

用于比较大文件,例如断言文件上载时的图像。缓冲区或字符串与should.eql的比较需要很长时间。我建议使用crypto模块断言缓冲区哈希:

const buf1Hash = crypto.createHash('sha256').update(buf1).digest();
const buf2Hash = crypto.createHash('sha256').update(buf2).digest();
buf1Hash.should.eql(buf2Hash);

一种更简单的方法是断言缓冲区长度,如下所示:

buf1.length.should.eql(buf2.length)

不使用shouldjs作为断言模块,您当然可以使用不同的工具

使用file-comparenode-temp的解决方案:

it('should return test2.json as a stream', function (done) {
    var writeStream = temp.createWriteStream();
    temp.track();
    var req = api.get('/files/7386afde8992');
    req.on('end', function() {
        comparator.compare(writeStream.path, TEST2_JSON_FILE, function(result, err) {
            if (err) {
                return done(err);
            }
            result.should.true;
            done();
        });
    });
    req.pipe(writeStream);
});

我认为应该在JavaScript中使用非阻塞调用来获得更好的性能,至少可以防止阻塞其他操作:

阻塞是指Node.js进程中附加JavaScript的执行必须等待非JavaScript操作完成。发生这种情况是因为在发生阻塞操作时,事件循环无法继续运行JavaScript。

在Node.js中,JavaScript由于占用CPU而不是等待非JavaScript操作(如I/O)而表现出较差的性能,通常不被称为阻塞。Node.js标准库中使用libuv的同步方法是最常用的阻塞操作。本机模块也可能具有阻塞方法。

因此,我将使用以下代码来更改Sync调用。此外,我将使用Max建议的方法equals来比较两个文件:

const fs = require('fs')
fs.readFile('file1', (err, data1) => {
    if (err) throw err;
    fs.readFile('file2', (err, data2) => {
        if (err) throw err;
        if (data1.equals(data2)) {
            console.log('EQUAL')
        } else {
            console.log('NON EQUAL')
        }
    });
});

尽管对于一个小脚本和一个单独的脚本,结果几乎是相同的