浅析Node中zip压缩和zip解压缩(附代码)

文章2022-12-10115 人已阅来源:网络

之前的文章《你值得了解的certbot开启亚马逊EC2主机https(代码分享)》中,给大家了解了certbot开启亚马逊EC2主机https。下面本篇文章给大家了解Node中zip压缩和zip解压缩,我们一起看看怎么做。

浅析Node中zip压缩和zip解压缩(附代码)

NodeJS文件(夹)压缩/解压方案(zip/unzip)-Linuxzip相关命令

Use adm-zip

adm-zip支持archiveunarchive一个或多个文件或者整个文件夹的功能,使用非常的简单方便。

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);

更多 apihttps://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文件,而且它只是存在于内存中,需要写文件操作后才会真正存到磁盘。

更多 APIhttps://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" })
);

更多APIhttps://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.zipabc2.zipabc3.zip,使用通配符

unzip abc\?.zip

注释:?表示一个字符,如果用*表示任意多个字符。 不解压,只查看内容

unzip -v test.zip

验证zip文件完成性,查看网上下的包包是不是已经下载完了

unzip -t test.zip

我用-v选项发现music.zip压缩文件里面有很多目录和子目录,并且子目录中其实都是歌曲mp3文件,我想把这些文件都下载到第一级目录,而不是一层一层建目录:

unzip -j music.zip

以上就是浅析Node中zip压缩和zip解压缩(附代码)的详细内容!