node.js - 如何在没有额外库的情况下在 linux 和/或 windows 中解压缩 zip 文件?
问题描述
该命令应该在 linux 或 windows 环境中工作,并且不依赖于任何其他库。
解决方案
值得庆幸的是,在 Unix/Linux 世界中,我们几乎可以依靠unzip
存在。假设使用 Powershell v5 的“现代”Windows,有一个等效的Expand-Archive
命令。
const os = require("os");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
async function unzip(file, destination) {
const expandCommand = os.platform() === "win32" ?
`powershell -command "& {&'Expand-Archive' ${file} -DestinationPath ${destination}}"` :
`unzip ${file} -d ${destination}`;
const { stdout, stderr } = await exec(expandCommand);
return stderr ? Promise.reject(stderr) : stdout;
}
唯一棘手的部分是如何通过 exec 调用 powershell。
推荐阅读
- emacs - 如何在 Emacs 中突出显示我自己的语法?
- ios - 应用程序图标不填充 xcode 中的整个区域是否很常见?(包括图片)
- node.js - node-cron 代码放置的最佳实践是什么?
- facebook-graph-api - 使用图形 api 从 facebook 提取数据
- ios - 如何在 Swift 3 和 4 中发布 Google Sheets API V4 的值?
- user-interface - 如何摆脱已编译的 Haskell GUI 应用程序的黑色控制台窗口
- django - 在本地测试 Anymail webhook,无法接收信号
- javascript - 电子错误 | 节点.js
- java - 在构造函数中将对象作为参数传递
- json - Moshi 相当于 Gson 的“serializeNulls”