在这段代码中我得到p(文件路径)并可以下载
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);
});
});
}
但我需要在格式化日期之前压缩.csv文件,或者首先格式化.csv文件,之后将其压缩并保存在驱动器上并获取路径。
一开始我需要创建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"); and zip
也许我们可以使用zipPath以某种方式压缩这个csv?
答案 0 :(得分:0)
我建议使用archiver模块。
https://github.com/archiverjs/node-archiver
它非常易于使用,甚至可以将所有文件夹包装成拉链。
以下是从文件夹
制作zip的示例代码function archive (cb) {
var archive = require('archiver');
var timestamp = new Date().getTime().toString();
// this self.logPath + '/archive' + timestamp + '.zip' is the path to zip file.
//You can use any path you 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 and the output file descriptor has closed.');
return cb("All ok or path to zip");
});
archive.on('error', function(err) {
return cb("Error");
});
archive.pipe(output);
//read directory
var allLogs = fs.readdirSync('path to dir or to file');
//append files to archive
async.eachSeries(allLogs, function(fileName, callback){
var file = path.join(self.logPath, fileName);
archive.append(fs.createReadStream(file), { name: fileName });
callback();
}, function(){
//finalize it will trigger on close event
archive.finalize();
});
}
当然你需要改变一些路径,但其他一切都应该没问题。
希望这有帮助