我是 NodeJS 的新手。我知道我们可以使用 pipe() 方法将数据流式传输到客户端。
这是代码片段
router.get('/archive/*', function (req, res) {
var decodedURI = decodeURI(req.url);
var dirarr = decodedURI.split('/');
var dirpath = path.join(dir, dirarr.slice(2).join("/"));
console.log("dirpath: " + dirpath);
var archive = archiver('zip', {
zlib: {level: 9} // Sets the compression level.
});
archive.directory(dirpath, 'new-subdir');
archive.on('error', function (err) {
throw err;
});
archive.pipe(res)
archive.on('finish', function () {
console.log("finished zipping");
});
archive.finalize();
});
当我使用获取请求时,下载的压缩文件但 没有任何扩展名。我知道它是因为我正在将写入流传输到响应中。反正有没有.zip扩展名的管道?或者如何在不在 HDD 中构建 zip 文件的情况下发送 zip 文件?