Hướng dẫn nodejs zlib zip folder - thư mục zip nodejs zlib
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:
Để thực hiện bước đầu tiên, tôi cần hai mô -đun Node.js:
Cái đầu tiên cho phép chúng tôi tạo các tệp 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 Sau đó, khi tôi chuyển đổi thư mục thành tệp 1 của 2.Đây là mã cuối cùng:
Đ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:
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 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 8 thành file 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 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
Okay, Done! Download code ví dụ trên tại đây. ——————– References: |