nodejs appending data to the end of a readstream
Asked Answered
A

1

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();
});
Allembracing answered 18/7, 2014 at 8:44 Comment(0)
B
-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.

Bloodworth answered 19/1, 2019 at 13:18 Comment(1)
How about put some sample code and share the link of the module document? That would make this a good answer :)Lesterlesya

© 2022 - 2024 — McMap. All rights reserved.