1

I am able to append data to a file like this

row (this I will get )

 fs.appendFile('./file.txt', JSON.stringify(row) + '\n', (err) => {
      if (err) throw err;
    });

But how can I "append" the data while zipping it at the same time ? I am unsure if this is possible, If yes any pointers will be extremely helpful. Can I achieve it through piping? if yes how?

zip.appendData('./file.zip',JSON.stringify(row) + '\n', (err) => {
          if (err) throw err;
        });

Something like above ?

Naman Gupta
  • 155
  • 6
  • Not sure it's possible, see this [comment](https://stackoverflow.com/questions/48638885/compress-into-zip-file-with-node-js-core-zlib-module#comment84276787_48638885). Are you restricted to `.zip` or can you use `.gz` as well? – Avraham Sep 24 '19 at 16:10

2 Answers2

2

I don't know if it's possible to append to a .zip archive without rewriting it.

If a .gz file is considered, you can use the built-in zlib module to append directly to the .gz file.

const zlib = require('zlib');

zlib.gzip(JSON.stringify(row), (err, data) => {
  if (err) throw err;
  fs.appendFile('file.txt.gz', data, err => {
    if (err) throw err;
  })
})
Avraham
  • 928
  • 4
  • 12
0

I'm not sure this gonna work, but maybe helps you.

You need zlib package on your project

npm install zlib

Code:

const fs = require('fs');
const zlib = require('zlib');

function WriteAndZip(filename) {
    return new Promise(async function (resolve, reject) {
        fs.appendFile(`./your_path/${filename}`, JSON.stringify(row) + '\n', (err) => {
            if (err) throw err;
        });
        const fileContents = fs.createReadStream(`./your_path/${filename}`);
        const writeStream = fs.createWriteStream(`./your_path/${filename}.gz`);
        const zip = zlib.createGzip();
        fileContents.pipe(zip).pipe(writeStream).on('finish', (err) => {
            if (err) return reject(err);
            else resolve();
        });
    });
}
imfsilva
  • 139
  • 8