本篇文章給大家了解Node中zip壓縮和zip解壓縮,我們一起看看怎么做。

NodeJS文件(夾)壓縮/解壓方案(zip/unzip)-Linux上zip相關命令
Use adm-zip
adm-zip支持archive和unarchive一個或多個文件或者整個文件夾的功能,使用非常的簡單方便。
var adm_zip = require("adm-zip");
//creating archives
var zip = new adm_zip();
zip.addLocalFolder("./dist");
zip.writeZip("./dist.zip");
//extracting archives
var unzip = new adm_zip("dist.zip");
unzip.extractAllTo("./dist", /*overwrite*/ true);更多 api 查看:https://github.com/cthackers/adm-zip
Use JSZip
這個庫在使用的時候需要把文件一個個增加到zip對象中,而且需要把內容也手動添加,再使用寫文件操作把內存中的zip對象轉成物理存儲。所以如果是對于一整個文件夾來說,就很麻煩,需要遍歷文件夾
var JSZip = require("jszip");
var fs = require("fs");
var zip = new JSZip();
var file_content = fs.readFileSync("archive/a.txt");
zip.file("a.txt", file_content);
var data = fs.readFileSync("archive/img/pic.jpeg");
zip.file("img/pic.jpeg", data, { base64: true });
var zipfolder = zip.generate({ type: "nodebuffer" });
fs.writeFile("jszip.zip", zipfolder, function (err) {
if (err) throw err;
});JSZip里面也有個folder方法,但它只是用來切換zip對象內部的虛擬路徑,比如zip.folder("img").file('a.txt')就是在zip中添加一個img子目錄,在下面創建a.txt,效果等同于zip.file("img/a.txt")。這里還需要注意的是,文件的內容都需要手動添加,如果僅僅是zip.file("a.txt");只是在zip對象中創建了內容為空的txt文件,而且它只是存在于內存中,需要寫文件操作后才會真正存到磁盤。
更多 API查看:https://github.com/Stuk/jszip
Use archiver and unzip
archiver很強大,支持zip格式tar格式,只需要提供路徑就可以壓縮已存在的文件夾。
壓縮:
// require modules
var fs = require("fs");
var archiver = require("archiver");
// create a file to stream archive data to.
var output = fs.createWriteStream(__dirname + "/example.zip");
var archive = archiver("zip", {
zlib: { level: 9 }, // Sets the compression level.
});
// pipe archive data to the file
archive.pipe(output);
// append a file from stream
var file1 = __dirname + "/file1.txt";
archive.append(fs.createReadStream(file1), { name: "file1.txt" });
// append a file from string
archive.append("string cheese!", { name: "file2.txt" });
// append a file from buffer
var buffer3 = Buffer.from("buff it!");
archive.append(buffer3, { name: "file3.txt" });
// append a file
archive.file("file1.txt", { name: "file4.txt" });
// append files from a sub-directory and naming it `new-subdir` within the archive
archive.directory("subdir/", "new-subdir");
// append files from a sub-directory, putting its contents at the root of archive
archive.directory("subdir/", false);
// append files from a glob pattern
archive.glob("subdir/*.txt");
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();解壓
var fs = require("fs");
var unzip = require("unzip");
fs.createReadStream("archiver-unzip.zip").pipe(
unzip.Extract({ path: "unarchive" })
);更多API查看:https://github.com/archiverjs/node-archiver
Linux上zip/unzip命令
apt-get install zip unzip
解壓縮zip文件,語 法:
unzip [-cflptuvz][-agcjlmnoqsvx][-P <密碼>][.zip文件][文件][-d <目錄>][-x <文件>] 或 unzip [-Z]
補充說明:unzip為.zip壓縮文件的解壓縮程序。
參數:
-c將解壓縮的結果顯示到屏幕上,并對字符做適當的轉換。
-f更新現有的文件。
-l顯示壓縮文件內所包含的文件。
-p與-c參數類似,會將解壓縮的結果顯示到屏幕上,但不會執行任何的轉換。
-t檢查壓縮文件是否正確。
-u與-f參數類似,但是除了更新現有的文件外,也會將壓縮文件中的其他文件解壓縮到目錄中。
-v執行是時顯示詳細的信息。
-z僅顯示壓縮文件的備注文字。
-a對文本文件進行必要的字符轉換。
-b不要對文本文件進行字符轉換。
-C壓縮文件中的文件名稱區分大小寫。
-j不處理壓縮文件中原有的目錄路徑。
-L將壓縮文件中的全部文件名改為小寫。
-M將輸出結果送到more程序處理。
-n解壓縮時不要覆蓋原有的文件。
-o不必先詢問用戶,unzip執行后覆蓋原有文件。
-P<密碼> 使用zip的密碼選項。
-q執行時不顯示任何信息。
-s將文件名中的空白字符轉換為底線字符。
-V保留VMS的文件版本信息。
-X解壓縮時同時回存文件原來的UID/GID。
[.zip 文件]指定.zip壓縮文件。
[文件]指定要處理.zip壓縮文件中的哪些文件。
-d<目錄>指定文件解壓縮后所要存儲的目錄。
-x<文件>指定不要處理.zip壓縮文件中的哪些文件。
-Z unzip-Z等于執行zipinfo指令。
基本用法是:
zip [參數][打包后的文件名] [打包的目錄路徑]
參數:
-a將文件轉成 ASCII 模式
-F嘗試修復損壞的壓縮文件
-h顯示幫助界面
-m將文件壓縮之后,刪除源文件
-n特定字符串 不壓縮具有特定字尾字符串的文件
-o將壓縮文件內的所有文件的最新變動時間設為壓縮時候的時間
-q安靜模式,在壓縮的時候不顯示指令的執行過程
-r將指定的目錄下的所有子目錄以及文件一起處理
-S包含系統文件和隱含文件(S 是大寫)
范 例:
zip命令可以用來將文件壓縮成為常用的zip格式。unzip命令則用來解壓縮zip文件。 壓縮文件abc.txt和一個目錄dir1,名字test.zip:
zip -r test.zip abc.txt dir1 #解壓縮: ```sh unzip test.zip
目錄下有abc1.zip,abc2.zip和abc3.zip,使用通配符
unzip abc\?.zip
注釋:?表示一個字符,如果用*表示任意多個字符。 不解壓,只查看內容
unzip -v test.zip
驗證zip文件完成性,查看網上下的包包是不是已經下載完了
unzip -t test.zip
我用-v選項發現music.zip壓縮文件里面有很多目錄和子目錄,并且子目錄中其實都是歌曲mp3文件,我想把這些文件都下載到第一級目錄,而不是一層一層建目錄:
unzip -j music.zip






