標籤:預設 tde com console span zip 命令列 p12 var
1、shell/cmd命令列壓縮解壓縮(1)zip壓縮解壓縮
zip壓縮:zip -rP{密碼} <目標檔案.zip> <源檔案> //預設覆蓋現有檔案
zip解壓縮:zip -oP{密碼} <源檔案.zip> //預設覆蓋現有檔案
(2)rar壓縮解壓縮
說明: linux需要下載rarlinux,然後壓縮,make編譯後,即可使用。
rar壓縮:rar a -p{密碼} <目標檔案.rar> <源檔案> -y //預設覆蓋現有檔案
例如:rar a -p123456 abc.rar abc
rar解壓縮:rar x -p{密碼 } <源檔案.rar> -y //保留源檔案路徑,預設覆蓋現有檔案
例如:rar x -p123456 abc.rar -y
2、如何通過nodejs執行shell/cmd命令
說明:通過child_process模組
var exec = require(‘child_process‘).exec; //引入child_process模組exports.execCmd = function(cmdStr,next){ exec(cmdStr,function(err,stdout,stderr){ next({ err:err, stdout:stdout, stderr:stderr }); });} 3、封裝成方法
rar解壓縮:
/*方法名:rar解壓縮參數: password zipFilePath tgtFilePath例如: var password ="20170313", zipFilePath ="D:/test/18_20170313.rar", srcFilePath = "D:/test/18_20170313"; cmdStr = "rar x -P20170313 D:\test\18_20170313.rar D:\test\18_20170313 -y" * */var fs = require("fs");var exec = require(‘child_process‘).exec;exports.unrar = function(param,next){ console.log("param:",param); var cmdStr = "rar x -P"+param.password+" "+param.zipFilePath+" "+param.tgtFilePath+" -y"; console.log("cmd:",cmdStr); fs.exists(param.tgtFilePath, function(exists) { //判斷路徑是否存在 //console.log(">> exists:",exists); if(exists) { exec(cmdStr,function(err,stdout,stderr){ //執行命令列 fs.readdir(param.filesPathInPro,next); }); } else { fs.mkdir(param.tgtFilePath,function(){ //建立目錄 exec(cmdStr,function(err,stdout,stderr){ //執行命令列 fs.readdir(param.filesPathInPro,next); }); }); } });}
rar壓縮:
/*方法名:rar壓縮參數:passwordzipFilePathsrcFilePath例如:var password ="20170313",zipFilePath ="D:/test/18_20170313.rar",srcFilePath = "D:/test/18_20170313";cmdStr ="rar a -ep -P20170313 D:\test\18_20170313.rar D:\test\18_20170313" * */var fs = require("fs");var exec = require(‘child_process‘).exec;exports.rar = function(param,next){ var cmdStr = ‘rar a -ep -P‘+param.password+‘ ‘+param.zipFilePath+‘ ‘+param.srcFilePath+‘ -y‘; console.log(">> cmdStr:",cmdStr); fs.exists(param.srcFilePath, function(exists) { //判斷路徑是否存在 if(exists) { exec(cmdStr,next); } else { next({ code:400, msg:"源檔案找不到" }) } });}
nodejs壓縮解壓縮(加密)