javascript - How to zip file before saving original on drive? -
in code p(file path) , can download it
function createcsv(){ return mytmp.gettempdir((tmppath) => { return new promise(function (resolve, reject) { let p = path.resolve(tmppath, "snake_case_users33.csv"); var ws = fs.createwritestream(p); csv .write([ {a: "a1", b: "b1"}, {a: "a2", b: "b2"} ], {headers: true}) .pipe(ws); resolve(p); }); }); }
but need zip .csv file, either before formatting date, or @ first format .csv file, after zip , save on drive , path.
at beginning need create csv :
let p = path.resolve(tmppath, "snake_case_users.csv"); var output = fs.createwritestream(p); csv.write([ {a: "a1", b: "b1"}, {a: "a2", b: "b2"} ], {headers: true}); //below let zippath = path.resolve(tmppath, "snake_case_users.zip"); , zip
maybe after use zippath zip csv somehow?
i suggest use archiver module.
https://github.com/archiverjs/node-archiver
it easy use , can wrap folder zip.
here example code make zip folder
function archive (cb) { var archive = require('archiver'); var timestamp = new date().gettime().tostring(); // self.logpath + '/archive' + timestamp + '.zip' path zip file. //you can use path like. var zippath = self.logpath + '/archive' + timestamp + '.zip' var output = fs.createwritestream(zippath); var archive = archiver('zip'); output.on('close', function() { console.log(archive.pointer() + ' total bytes'); console.log('archiver has been finalized , output file descriptor has closed.'); return cb("all ok or path zip"); }); archive.on('error', function(err) { return cb("error"); }); archive.pipe(output); //read directory var alllogs = fs.readdirsync('path dir or file'); //append files archive async.eachseries(alllogs, function(filename, callback){ var file = path.join(self.logpath, filename); archive.append(fs.createreadstream(file), { name: filename }); callback(); }, function(){ //finalize trigger on close event archive.finalize(); }); }
of course need change paths, else should fine.
hope helps
Comments
Post a Comment