Node.js - 压缩/解压缩文件夹 [英] Node.js - Zip/Unzip a folder

查看:135
本文介绍了Node.js - 压缩/解压缩文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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-zip 模块并一个一个添加文件夹中的所有文件.但是解压时会遇到问题(这种情况下文件夹会丢失).

One possibility (but that I consider as tinkering) is to use node-zip module 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.js 压缩(然后解压缩)整个文件夹(尊重子焊料层次结构)吗?

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

谢谢.

推荐答案

我终于明白了,在@generalhenry 的帮助下(见问题评论)和

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

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

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

  1. 将文件夹转成.tar文件

压缩.tar文件

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

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

npm install tar
npm install fstream

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

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

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

The second node module will help us to read a folder and write a file. Concerning the basic fs node.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).

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

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

这是最终代码:

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 */

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

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

还有两件事:

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

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

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

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

这篇关于Node.js - 压缩/解压缩文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆