Node.js - 压缩/解压缩文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15530435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:56:48  来源:igfitidea点击:

Node.js - Zip/Unzip a folder

node.jszlib

提问by htaidirt

I'm diving into Zlib of node.js. I was able to compress and uncompress files using the provided examples (http://nodejs.org/api/zlib.html#zlib_examples) but I didn't be able to find more about doing the same for folders?

我正在深入研究 node.js 的 Zlib。我能够使用提供的示例 ( http://nodejs.org/api/zlib.html#zlib_examples)压缩和解压缩文件,但我找不到更多关于对文件夹执行相同操作的信息?

One possibility (but that I consider as tinkering) is to use node-zipmodule and adding all the files of the folder one by one. But I'll face a problem when uncompressing (I will lose the folders in this case).

一种可能性(但我认为是修补)是使用node-zip模块并一个一个地添加文件夹的所有文件。但是在解压缩时我会遇到问题(在这种情况下我会丢失文件夹)。

Any idea how to compress (and then uncompress) a whole folder (respecting the sub-solders hierarchy) using Node.js?

知道如何使用 Node.js 压缩(然后解压缩)整个文件夹(尊重子焊料层次结构)?

Thanks.

谢谢。

回答by htaidirt

I've finally got it, with the help of @generalhenry (see comments on the question) and

在@generalhenry 的帮助下,我终于得到了它(请参阅对问题的评论)和

as mentioned in the comments, we need to compress the folder in two steps:

如评论中所述,我们需要分两步压缩文件夹:

  1. Convert the folder into a .tarfile

  2. Compress the .tarfile

  1. 将文件夹转换为.tar文件

  2. 压缩.tar文件

In order to perform the first step, I needed two node.js modules:

为了执行第一步,我需要两个 node.js 模块:

npm install tar
npm install fstream

The first one allows us to create .tarfiles. You can have access to the source code here https://github.com/isaacs/node-tar

第一个允许我们创建.tar文件。您可以在此处访问源代码https://github.com/isaacs/node-tar

The second node module will help us to read a folder and write a file. Concerning the basic fsnode.js module, I don't know if it is possible to read a directory (I'm not talking about getting all the files in an array, using fs.readdir, but handling all the files and their organization in folders).

第二个节点模块将帮助我们读取文件夹和写入文件。关于基本的fsnode.js 模块,我不知道是否可以读取目录(我不是在谈论获取数组中的所有文件,使用fs.readdir,而是处理所有文件及其在文件夹中的组织)。

Then, when I convert the folder to .tarfile, I can compress it using Gzip()of Zlib.

然后,当我将文件夹转换为.tar文件时,我可以使用Gzip()of压缩它Zlib

Here is the final code:

这是最终的代码:

var fstream = require('fstream'),
    tar = require('tar'),
    zlib = require('zlib');

fstream.Reader({ 'path': 'path/to/my/dir/', 'type': 'Directory' }) /* Read the source directory */
.pipe(tar.Pack()) /* Convert the directory to a .tar file */
.pipe(zlib.Gzip()) /* Compress the .tar file */
.pipe(fstream.Writer({ 'path': 'compressed_folder.tar.gz' })); /* Give the output file name */

This helped me to compress an entire folder using node.js

这帮助我使用 node.js 压缩整个文件夹

2 more things:

还有 2 件事:

  • As you can see, there is a lack of documentation on tarmodule. I hope this will be improved soon since the two examples that was provided talk about how to extract content from the .tarfile.

  • I used the fstreammodule to help me handle the source directory. Can this be bypassed using fs? I don't know (please, comment if you have an idea).

  • 如您所见,缺少关于tar模块的文档。我希望这会很快得到改进,因为提供的两个示例讨论了如何从.tar文件中提取内容。

  • 我使用该fstream模块来帮助我处理源目录。这可以绕过使用fs吗?我不知道(如果您有想法,请发表评论)。

回答by B T

You can use the tar-streammodule to create a tar archive. Its much more flexible and simpler than node-tar in that:

您可以使用tar-stream模块来创建 tar 存档。它比 node-tar 更灵活、更简单,因为:

  • You can add files to the archive (not just directories, which is a limitation of node-tar)
  • It works with normal node filesystem streams (node-tar strangely requires the use of the fstream module)
  • Its pretty fully documented (node-tar isn't well documented)
  • You can create an archive without hitting the filesystem
  • 您可以将文件添加到存档中(不仅仅是目录,这是 node-tar 的限制)
  • 它适用于普通节点文件系统流(奇怪的是,node-tar 需要使用 fstream 模块)
  • 它的文档非常完整(node-tar 没有很好的文档记录)
  • 您可以在不访问文件系统的情况下创建存档

Create the tar archive, then compress it using zlib, then write it wherever you want (network, filesystem, etc).

创建 tar 存档,然后使用 zlib 对其进行压缩,然后将其写入任何您想要的位置(网络、文件系统等)。

回答by Andry

Providing an updated answer as package tarhas been updated since 2013.

提供更新的答案,因为包 tar自 2013 年以来已更新。

To achieve the same result, the code is much simpler and straightforward:

为了达到相同的结果,代码要简单得多:

const tar = require("tar"); // version ^6.0.1
const fs = require("fs");

tar.c(
  {
    gzip: true // this will perform the compression too
  },
  ["path/to/my/dir/"]
).pipe(fs.createWriteStream('path/to/my/dir.tgz'));

No need to explicitly use zlib.

无需显式使用zlib.