如何在将原始文件保存到驱动器之前压缩文件?
How to zip file before saving original on drive?
在这段代码中我得到了 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 之后?
我建议使用归档模块。
https://github.com/archiverjs/node-archiver
它非常易于使用,甚至可以将所有文件夹打包成 zip。
这是从文件夹制作 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();
});
}
当然你需要改变一些路径,但其他一切都应该没问题。
希望对您有所帮助
在这段代码中我得到了 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 之后?
我建议使用归档模块。
https://github.com/archiverjs/node-archiver
它非常易于使用,甚至可以将所有文件夹打包成 zip。
这是从文件夹制作 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();
});
}
当然你需要改变一些路径,但其他一切都应该没问题。
希望对您有所帮助