Cuối cùng tôi đã nhận được nó, với sự giúp đỡ của @generalhenry [xem nhận xét về câu hỏi] và
Như đã đề cập trong các bình luận, chúng ta cần nén thư mục theo hai bước:
Chuyển đổi thư mục thành tệp
.tar
Nén tệp
.tar
Để thực hiện bước đầu tiên, tôi cần hai mô -đun Node.js:
npm install tar
npm install fstream
Cái đầu tiên cho phép chúng tôi tạo các tệp .tar
. Bạn có thể có quyền truy cập vào mã nguồn tại đây //github.com/isaacs/node-tar
Mô -đun nút thứ hai sẽ giúp chúng tôi đọc một thư mục và viết một tệp. Liên quan đến mô -đun fs
Node.js cơ bản, tôi không biết liệu có thể đọc thư mục không [tôi không nói về việc lấy tất cả các tệp trong một mảng, sử dụng fs.readdir
, nhưng xử lý tất cả các tệp và tổ chức của chúng trong các thư mục ].
Sau đó, khi tôi chuyển đổi thư mục thành tệp .tar
, tôi có thể nén nó bằng
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 */
1 của 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 */
2.Đây là mã cuối cùng:
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 */
Điều này đã giúp tôi nén toàn bộ một thư mục bằng Node.js
2 điều nữa:
Như bạn có thể thấy, thiếu tài liệu trên mô -đun
3. Tôi hy vọng điều này sẽ được cải thiện sớm vì hai ví dụ được cung cấp nói về cách trích xuất nội dung từ tệpvar 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 */
.tar
.Tôi đã sử dụng mô -đun
5 để giúp tôi xử lý thư mục nguồn. Điều này có thể được bỏ qua bằng cách sử dụngvar 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 */
fs
? Tôi không biết [xin vui lòng, bình luận nếu bạn có ý tưởng].
Code ví dụ Node.js nén, giải nén file – module Zlib [Xem thêm: Hướng dẫn Node.js] Để thực hiện nén/giải nén file [zip/unzip] trong node.js ta sử dụng module Module sử dụng
7var 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 */
zlib là module có sẵn khi cài node.js, để include zlib vào project ta dùng method
var zlib = require['zlib'];
Nén file [zip]
Dưới đây là code ví dụ nén file
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 */
8 thành file 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 */
9var zlib = require['zlib']; var fs = require['fs']; var gzip = zlib.createGzip[]; var r = fs.createReadStream['hello.txt']; var w = fs.createWriteStream['hello.txt.gzip']; r.pipe[gzip].pipe[w]; console.log['done'];
Đầu vào của method
var zlib = require['zlib'];0 và
var zlib = require['zlib'];1 sẽ là url của file được dùng để nén và url của file kết quả sau khi được nén
Giải nén file [unzip]
Đây là code ví dụ giải nén file
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 */
9 thành file var zlib = require['zlib'];3.
var zlib = require['zlib']; var fs = require['fs']; var unzip = zlib.createUnzip[]; var read = fs.createReadStream['hello.txt.gz']; var write = fs.createWriteStream['unzip.txt']; read.pipe[unzip].pipe[write]; console.log["unZipped Successfully"];
Một số method khác của zlib
constants | Returns an object containing Zlib constants |
createDeflate[] | Creates a Deflate object |
createDeflateRaw[] | Creates a DeflateRaw object |
createGunzip[] | Creates a Gunzip object |
createGzip[] | Creates a Gzip object |
createInflate[] | Creates a Inflate object |
createInflateRaw[] | Creates a InflateRaw object |
createUnzip[] | Creates a Unzip object |
deflate[] | Compress a string or buffer, using Deflate |
deflateSync[] | Compress a string or buffer, syncronously, using Deflate |
deflateRaw[] | Compress a string or buffer, using DeflateRaw |
deflateRawSync[] | Compress a string or buffer, syncronously, using DeflateRaw |
gunzip[] | Compress a string or buffer, using Gunzip |
gunzipSync[] | Compress a string or buffer, syncronously, using Gunzip |
gzip[] | Compress a string or buffer, using Gzip |
gzipSync[] | Compress a string or buffer, syncronously, using Gzip |
inflate[] | Decompress a string or buffer, using Inflate |
inflateSync[] | Decompress a string or buffer, syncronously, using Inflate |
inflateRaw[] | Decompress a string or buffer, using InflateRaw |
inflateRawSync[] | Decompress a string or buffer, syncronously, using InflateRaw |
unzip[] | Decompress a string or buffer, using Unzip |
unzipSync[] | Decompress a string or buffer, syncronously, using Unzip |
Okay, Done!
Download code ví dụ trên tại đây.
——————–
References: