joh*_*01s 6 buffer file tar in-memory node.js
假设我有两个缓冲区:
const bufferFile1 = Buffer.from('Hello World!', 'utf-8')
const bufferFile2 = Buffer.from('Hello Again World!', 'utf-8')
Run Code Online (Sandbox Code Playgroud)
我如何创建 tarball 文件缓冲区/流/blob(不写入磁盘),其中上面的两个缓冲区应作为 tarball 中的两个文件存储。我希望(能够)通过管道传输 tarball 作为对请求的响应。
我研究过使用tar包。但这个解决方案需要路径而不是内存流/缓冲区。
这有可能实现吗?
PS:我对内存文件处理的经验很少。
整个项目是创建一个api端点
我真的没有地方临时存储文件(无服务器环境),这就是为什么我希望解决方案完全在内存中。
我能够让它与archiver和 axios 一起工作。
archiver
tar-stream
是和的包装zip-stream
。所以这可以通过类似的方式来实现tar-stream
,但我没有让它工作。
如果 archive.read() 给null
你应该确保
archive.on("finish",...
archive.on("finish",...
之前被调用archive.finalize()
这是我的完整代码片段:
const axios = require("axios");
const archiver = require("archiver");
const archive = archiver("tar", { gzip: false, store: false });
// data is a string representation of the text.
archive.append(data, { name: "main.txt" });
archive.on("finish", () => {
const blob = new Blob([archive.read()], { type: "application/x-tar" });
const formData = new FormData();
// FormData seems to only accept blob
formData.append("file", blob);
axios
.post(url, formData, {
// remove this if your file is not in a binary format
responseType: 'arraybuffer'
})
.then((response) => {
console.log(response.data); // the response and data
})
.catch((error) => {
console.error(error);
});
});
// doing this after registering the events or else it will the data might not be present
archive.finalize();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1115 次 |
最近记录: |