Nodejs Compression Decompression (encryption)

Source: Internet
Author: User
Tags rar

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.