6

I would like to read a .txt file, append data to the end and finally send it to a zipstream. Right now what I'm doing is writting a new file and then using the new file for zipstream, but I would like to do it on the fly, without creating an unnecessary new file.

My question is how to create a read stream, modify it and send to another readstream (maybe with a writestream in the middle).

Is this possible?

The original idea was this one, but I'm lost somewhere in the middle:

var zipstream = require('zipstream');
var Stream = require('stream');

var zipOut = fs.createWriteStream('file.zip');
var zip = zipstream.createZip({ level : 1 });
zip.pipe(zipOut);

var rs = fs.createReadStream('file.txt');
var newRs = new Stream(); // << Here should be an in/out stream??
newRs.pipe = function(dest) {
  dest.write(rs.read());
  dest.write("New text at the end");
};
zip.addEntry(newRs, {name : 'file.txt'}, function() {
  zip.finalize();
});
Miquel
  • 8,339
  • 11
  • 59
  • 82

1 Answers1

-1

You can inplement a transform (subclass of stream.Transform). Then in its _flush method you have the ability to output any content you want when the input stream has reached the end. This can be piped between a readable stream and a writable one. Refer to node's stream module documentation for inplementation details.

  • 3
    How about put some sample code and share the link of the module document? That would make this a good answer :) – Til Jan 19 '19 at 13:38