如何在不包含父目录的情况下创建 tar 存档

How to create a tar archive without including parent directory

我想要 tar 位于另一个文件夹中的文件和文件夹。

例如:

mainFolder/subFolder
mainFolder/file

我只想 tar subFolder & file 使用 node-tar.

tar.c({
  gzip: true,
  file: 'uploads.tar.gz'
}, ['temp']).then(() => {
  console.log({
    status: 0,
    message: 'Tarball has been created'
  });
});

这会创建 tar.gz 文件,该文件在解压时也有临时文件夹。 请帮助我。

您可以在路径数组中添加多个文件和文件夹(参见 https://github.com/npm/node-tar#high-level-api

tar.c(
      {
        gzip: true,
        file: 'uploads.tar.gz'
        C: 'mainFolder'
      },
      ['subFolder', 'file']
    ).then(() => {
      console.log( {status: 0, message: 'Tarball has been created'});
});