在Node.js中,如何更改(覆盖)二进制文件中的字节而不添加字节

时间:2018-12-17 17:30:25

标签: javascript node.js binary fs

在Node.js中,如何更改(覆盖)二进制文件中的字节(以一定的偏移量)而不在其间添加字节并更改其长度?

在C语言中,我只想对fopen()使用“ r +”,fseek()将文件更改为要更改的偏移量,然后使用fwrite()覆盖字节。 Node.js中的等效项看起来如何?

2 个答案:

答案 0 :(得分:2)

好的,我发现它实际上很简单^^

fs.open(filename, "r+", (err, fd) => {
    if(!err) {
        fs.write(
            fd, new Uint8Array([byte]), 0, 1, offset,
            (err, bw, buf) => {
                if(!err) {
                    // succesfully wrote byte to offset
                }
            }
        );
    }
});

答案 1 :(得分:0)

我不得不做一些您最近描述的事情。我必须在不更改大小的情况下更新可执行文件中的URL。关键是在流上使用Transform。这个想法是,Transform将读入和写出所需的确切数据,并且仅修改您指定的字节。

这是一个Transform类,可以在流中进行查找和替换。构造函数采用参数说明字节的开始和结束序列是应替换的块的含义。还有一个padValue参数用于保持相同的大小。

import { Transform } from 'stream'

export default class FindAndReplaceTransform extends Transform {
  constructor(startBuffer, endBuffer, replacementValueBuffer, padValue, options) {
    super(options);
    this.startBuffer = startBuffer;
    this.endBuffer = endBuffer;
    this.replacementValueBuffer = replacementValueBuffer;
    this.padValue = padValue;
  }

  _findInBuffer(sourceBuffer, searchBuffer) {
    let searchFound = -1;
    let lengthOfPartialMatch = 0;
    for (let i = 0; i < sourceBuffer.length; i++) {
      for (let j = 0; j < searchBuffer.length; j++) {
        if (i + j >= sourceBuffer.length) {
          if (j > 0) {
            lengthOfPartialMatch = j;
          }
          break;
        }
        if (sourceBuffer[i + j] !== searchBuffer[j]) {
          break;
        }
        if (j === searchBuffer.length - 1) {
          searchFound = i;
        }
      }
      if (searchFound >= 0 || lengthOfPartialMatch > 0) {
        break;
      }
    }
    return { searchFound, lengthOfPartialMatch };
  }

  _doReplacement(length) {
    let replacementValueBuffer = this.replacementValueBuffer;
    if (this.padValue !== undefined) {
      replacementValueBuffer = Buffer.concat([replacementValueBuffer, Buffer.alloc(length - replacementValueBuffer.length, this.padValue)], length);
    }
    this.push(replacementValueBuffer);
  }

  //override
  _transform(data, encoding, done) {
    if(this.lengthOfPartialStartMatch){
      data = Buffer.concat([this.startBuffer.slice(0, this.lengthOfPartialStartMatch), data], this.lengthOfPartialStartMatch + data.length);
      delete this.lengthOfPartialStartMatch;
    }
    if(this.lengthOfPartialEndMatch){
      data = Buffer.concat([this.endBuffer.slice(0, this.lengthOfPartialEndMatch), data], this.lengthOfPartialEndMatch + data.length);
      this.replacementBuffer = this.replacementBuffer.slice(0, this.replacementBuffer.length - this.lengthOfPartialEndMatch);
      delete this.lengthOfPartialEndMatch;
    }

    let startAlreadyFound = !!this.replacementBuffer
    let { searchFound: startIndex, lengthOfPartialMatch: lengthOfPartialStartMatch } = this._findInBuffer(data, this.startBuffer);
    let tail = data.slice(startIndex >= 0 && !startAlreadyFound ? startIndex : 0);
    let { searchFound: endIndex, lengthOfPartialMatch: lengthOfPartialEndMatch } = this._findInBuffer(tail, this.endBuffer);


    if (!startAlreadyFound && startIndex >= 0) {
      this.push(data.slice(0, startIndex))
      this.replacementBuffer = Buffer.alloc(0);
      startAlreadyFound = true;
    }
    if (startAlreadyFound) {
      if (endIndex >= 0) {
        let replacementLength = this.replacementBuffer.length + endIndex + this.endBuffer.length;
        this._doReplacement(replacementLength);
        delete this.replacementBuffer;
        if (endIndex + this.endBuffer.length < tail.length) {
          let remainder = tail.slice(endIndex + this.endBuffer.length)
          this._transform(remainder, encoding, done);
          return;
        }
      } else {
        this.lengthOfPartialEndMatch = lengthOfPartialEndMatch;
        this.replacementBuffer = Buffer.concat([this.replacementBuffer, tail], this.replacementBuffer.length + tail.length);
      }
    } else {
      this.lengthOfPartialStartMatch = lengthOfPartialStartMatch;
      this.push(data.slice(0, data.length - lengthOfPartialStartMatch))
    }
    done();
  }

  //override
  _flush(done) {
    if (this.replacementBuffer) {
      this.push(this.replacementBuffer)
    }
    if(this.lengthOfPartialStartMatch){
      this.push(this.startBuffer.slice(0, this.lengthOfPartialStartMatch));
    }
    delete this.replacementBuffer;
    delete this.lengthOfPartialStartMatch;
    delete this.lengthOfPartialEndMatch;
    done()
  }
}

要使用上述转换,您可以执行以下操作:

let stream = fs.createReadStream(inputFile);
let padding = 0x00;
let startSequence = Buffer.from('${', 'utf16le');
let endSequence = Buffer.from('}', 'utf16le');
let transform = new FindAndReplaceTransform(startSequence, endSequence, Buffer.from(replacementValue, 'utf16le'), paddingValue);
stream = stream.pipe(transform);
stream.pipe(fs.createWriteStream(outputFile));

很显然,如果您要做的只是更改某个偏移量的字节,那么Transform类将大大简化。我提供上面的代码是因为我有它,如果您想做一些更复杂的事情,可以将其作为参考。

您要确保实现的主要方法是_transform方法。根据您的实现,您可能还需要实现_flush方法。上面代码中的其他类方法是我对替换代码的实现,并且不需要Transform来工作。