Node zip压缩和解压缩

邱秋 • 2019年04月11日 • 阅读:5932 • nodejs

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

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

更多 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

Linuxzip/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
#解压缩:
```bash
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

我,秦始皇,打钱!