Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Un TAR和Un GZip文件存储为JavaScript缓冲区_Javascript_Node.js_Gzip_Tar_Multer - Fatal编程技术网

Un TAR和Un GZip文件存储为JavaScript缓冲区

Un TAR和Un GZip文件存储为JavaScript缓冲区,javascript,node.js,gzip,tar,multer,Javascript,Node.js,Gzip,Tar,Multer,我正在Node.js/Express.js上开发一个服务器脚本,用于接收上载的带有多个文件的.tar.gz归档文件。该脚本必须解压和解压缩存档中的CSV文件,解析它们并将一些存储在数据库中。不需要在服务器上存储文件,只需处理它们即可。为了上传文件,我使用Multer,没有指定文件的存储位置,因此文件上传仅在req.files中可用,如Buffer 我的问题是,如何才能untar和ungzip缓冲区来获取文件的内容? 如果我这样做: const { unzipSync } = require('z

我正在Node.js/Express.js上开发一个服务器脚本,用于接收上载的带有多个文件的.tar.gz归档文件。该脚本必须解压和解压缩存档中的CSV文件,解析它们并将一些存储在数据库中。不需要在服务器上存储文件,只需处理它们即可。为了上传文件,我使用Multer,没有指定文件的存储位置,因此文件上传仅在
req.files
中可用,如
Buffer

我的问题是,如何才能untar和ungzip缓冲区来获取文件的内容? 如果我这样做:

const { unzipSync } = require('zlib');

const zipped = req.files[0];
const result = await unzipSync(zipped.buffer);
const str = result.toString('utf-8');

我得到的不是文件的内容,而是所有信息,包括文件名、一些元数据等,都是字符串,这很难解析。有更好的方法吗?

我设法使用和库来解压缓冲区

const tar = require('tar-stream');
const streamifier = require('streamifier');
const { unzipSync } = require('zlib');

const untar = ({ buffer }) => new Promise((resolve, reject) => {
  // Buffer is representation of .tar.gz file uploaded to Express.js server
  // using Multer middleware with MemoryStorage
  const textData = [];
  const extract = tar.extract();
  // Extract method accepts each tarred file as entry, separating header and stream of contents:
  extract.on('entry', (header, stream, next) => {
    const chunks = [];
    stream.on('data', (chunk) => {
      chunks.push(chunk);
    });
    stream.on('error', (err) => {
      reject(err);
    });
    stream.on('end', () => {
      // We concatenate chunks of the stream into string and push it to array, which holds contents of each file in .tar.gz:
      const text = Buffer.concat(chunks).toString('utf8');
      textData.push(text);
      next();
    });
    stream.resume();
  });
  extract.on('finish', () => {
    // We return array of tarred files's contents:
    resolve(textData);
  });
  // We unzip buffer and convert it to Readable Stream and then pass to tar-stream's extract method:
  streamifier.createReadStream(unzipSync(buffer)).pipe(extract);
});

使用这种方法,我设法避免在文件系统中存储任何临时文件,并以独占方式处理内存中所有文件的内容。

为什么不使用实际的
tar
,然后从磁盘加载生成的数据?(使用
exec
spawn
)是的,甚至更容易为节点使用tar模块,例如。我只是在想,如果我可以避免保存上传到磁盘和从缓冲区本身解压。如果你想解压tgz,你需要解压和解压。现在你只是在解拉链,是的。但是如何在JavaScript中解压缓冲区呢?我发现了许多模块,但没有这样的功能。它们主要处理文件系统中的文件或读取流。您可以直接链接到一个库,该库可以满足您的需要,但您找不到具体的细节,所以:您可能想要。这样,开源社区中的每个人都会受益。