您在编辑的评论中提到文件非常大(先是10GB,然后编辑到100MB)。在mssql中存储这个文件是不可行的,因为rdbms系统不是用来存储大文件的。例如,您可能需要考虑将此对象存储到azure存储或aws s3。如果您需要更多信息,请阅读此
https://softwareengineering.stackexchange.com/questions/150669/is-it-a-bad-practice-to-store-large-files-10-mb-in-a-database
我怀疑nodejs对于这个下载/提取也不是一个好的技术,您可能需要考虑另一种可以从较高的资源消耗和多线程中获益的技术。但是,如果您希望在nodejs中执行此操作,可以归结为三个步骤
-
下载大文件
-
解压缩大文件
-
将大文件存储到某个位置(如azure或amazon存储)
为了在不耗尽内存的情况下下载文件,需要流式传输/管道传输,我不知道如何
fetch-everywhere
将处理此问题,因此我提供了一个使用
request
反而。我也选了
node-stream-zip
因为它有一个流式api,它不会将整个集合加载到内存中。
我把代码的“存储”部分留为空,因为我不可能知道您要将其存储在哪里,而且MSSQL不是一个选项。
const fs = require('fs');
const request = require('request');
const progress = require('request-progress');
const StreamZip = require('node-stream-zip');
// download large file...
const url = 'example.com/path/to/large/file';
const zipPath = 'generateATmpFileName.zip';
const jsonPath = 'generateATmpFileName.json';
progress(request(url))
.on('end', function () {
// file has been completely downloaded, lets unzip it
var zip = new StreamZip({
file: zipPath,
storeEntries: true
});
zip.on('ready', function() {
zip.stream('path-to-json-file-in-zip.json', function(error, zstream) {
zstream.pipe(fs.createWriteStream(jsonPath));
zstream.on('end', function() {
zip.close();
// file has been completely extracted, do what you want with it
});
});
});
})
.pipe(fs.createWriteStream(zipPath));