1, shell/cmd command line compression decompression (1) Zip compression decompression
zip compression : zip-rp{Password} < target file .zip> < source file >//Overwrite existing file by default
Zip extract : zip-op{password} < source file .zip>//Overwrite existing file by default
(2) RAR compression decompression
Description: Linux needs to download Rarlinux, then compress, make compile, ready to use.
rar compression : rar a-p{Password} < target file .rar> < source file >-y//Overwrite existing file by default
For example: RAR a-p123456 abc.rar ABC
rar extract : rar x-p{Password} < source file .rar>-y//Preserve source file path, overwrite existing file by default
For example: RAR x-p123456 abc.rar-y
2. How to execute shell/cmd command through Nodejs
Description: Via child_process module
var exec = require (' child_process '). exec; // Introducing the Child_process module function(cmdstr,next) { exec (cmdstr,function(err,stdout,stderr) { Next ({ err:err, stdout:stdout, stderr:stderr }) ;}
3. Encapsulation method
RAR Decompression:
/*method Name: rar decompression parameter: password Zipfilepath tgtfilepath example: 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" **/varFS = require ("FS");varexec = require (' child_process '). Exec;exports.unrar=function(Param,next) {Console.log ("Param:", param); varCmdstr = "rar x-p" +param.password+ "+param.zipfilepath+" "+param.tgtfilepath+"-y "; Console.log ("CMD:", CMDSTR); Fs.exists (Param.tgtfilepath,function(exists) {//determine if a path exists //Console.log (">> exists:", exists); if(exists) {exec (CMDSTR,function(Err,stdout,stderr) {//Execute command lineFs.readdir (Param.filespathinpro,next); }); } Else{fs.mkdir (Param.tgtfilepath,function(){//Create a directoryEXEC (CMDSTR,function(Err,stdout,stderr) {//Execute command lineFs.readdir (Param.filespathinpro,next); }); }); } });}
&NBSP;
RAR compression:
/*method Name: rar compression parameter: Passwordzipfilepathsrcfilepath For example: 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" **/varFS = require ("FS");varexec = require (' child_process '). Exec;exports.rar=function(param,next) {varCmdstr = ' rar a-ep-p ' +param.password+ ' +param.zipfilepath+ ' +param.srcfilepath+ '-y '; Console.log (">> cmdstr:", CMDSTR); Fs.exists (Param.srcfilepath,function(exists) {//determine if a path exists if(exists) {exec (cmdstr,next); } Else{Next ({code:400, msg:"Source File not found" }) } });}
< Span class= "hljs-built_in" >< Span class= "hljs-string" > &NBSP;
Nodejs Compression Decompression (encryption)