infly-libs
Version:
工具组件库
58 lines (53 loc) • 1.62 kB
JavaScript
const { execFileSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const os = require("os");
/**
* 压缩文件
* @description
* 基于cross-zip库
* 关键代码[IO.Compression.CompressionLevel]::Optimal, $true
*/
function zipSync(inPath, outPath, includeFolder = false) {
const opts = {
cwd: path.dirname(inPath),
maxBuffer: Infinity
};
execFileSync(getZipCommand(), getZipArgs(inPath, outPath, includeFolder), opts);
}
function getZipCommand() {
if (process.platform === "win32") {
return "powershell.exe";
} else {
return "zip";
}
}
function quotePath(pathToTransform) {
return '"' + pathToTransform + '"';
}
function getZipArgs(inPath, outPath, includeFolder) {
const extraFolderParamStr = includeFolder ? `, [IO.Compression.CompressionLevel]::Optimal, $true` : "";
if (process.platform === "win32") {
return [
"-nologo",
"-noprofile",
"-command",
`& {
param([String]$myInPath, [String]$myOutPath);
Add-Type -A "System.IO.Compression.FileSystem";
[IO.Compression.ZipFile]::CreateFromDirectory($myInPath, $myOutPath${extraFolderParamStr});
exit !$?
}`,
"-myInPath",
quotePath(inPath),
"-myOutPath",
quotePath(outPath)
];
} else {
const fileName = path.basename(inPath);
return ["-r", "-y", outPath, fileName];
}
}
module.exports = {
zipSync
};