nodejs rar/zip加密压缩、解压缩
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压缩
参数:
password
zipFilePath
srcFilePath
例如:
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 rar/zip加密压缩、解压缩的更多相关文章
- 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩
原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// ...
- mac系统中怎么打开rar/zip等压缩文件?
平常使用mac的同学们,可能经常要接受下别人发过来的rar文件,可惜的时mac os x系统默认是不能打开rar文件(不知道以后苹果会支持rar不?),那么我们该如何去解圧rar文件,接下来我将介绍. ...
- Android zip文件压缩解压缩
DirTraversal.java <P style="TEXT-ALIGN: left; PADDING-BOTTOM: 0px; WIDOWS: 2; TEXT-TRANSFORM ...
- zip unzip 压缩解压缩命令
直接上例子: mkdir test1 touch test1/1.txt touch test1/2.txt zip -r test1.zip test1 #-r 参数是包含文件夹下的文件 un ...
- Python实现加密压缩成RAR或ZIP文件
博主在前两篇博文分别介绍了加密RAR文件的解压https://www.cnblogs.com/kangbazi666/p/13646308.html和加密ZIP文件的解压https://www.cnb ...
- linux 系统下 zip 的加密压缩与解压缩命令
1.加密压缩 [small@sun shine]# zip -rP king java.zip java adding: java/ (stored 0%) adding: java/default/ ...
- Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)
1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...
- Linux的压缩/解压缩文件处理 zip & unzip
Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服 ...
- C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 C# 文件压缩加解密
C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http:// ...
随机推荐
- centos7 安装nginx与配置
第一步安装 使用Yum安装是推荐的方式,整体的流程非常的简单,也不容易出错,如果不需要什么特殊配置,建议使用Yum尽进行安装. 第一种安装方式,通过添加epel源 yum install epel-r ...
- Android 上的一些profiler tools
cpu这边先配了一个unity自带的profiler https://docs.unity3d.com/560/Documentation/Manual/ProfilerWindow.html 连an ...
- mvn test 中文乱码
有两种解决办法: 1.设置encoding:<argLine>-Dfile.encoding=UTF-8</argLine>,解决读取文件中的中文乱码问题 2.升级maven- ...
- Yasm 1.3.0 Release Notes
Yasm 1.3.0 Release Notes http://yasm.tortall.net/releases/Release1.3.0.html Target Audience Welcome ...
- SEO优化100条
1.准备个好域名.①.尽量在5位数内,当然也不一定,反正要让用户好记.(看个人):②.尽量用顶级的域名,搜索排名感觉好一点.③.做中文站最好用拼音注册,不要问为什么.看百度(baidu.com)就是很 ...
- 实现微信浏览器内打开App Store链接
http://www.ildsea.com/1781.html 微信浏览器是不支持打开App Store 页面的,不知道微信为什么这么做.比如你页面写 <a href=”http://itune ...
- linux 修改时间
实例:设置时间伟2008年8月8号12:00# date -s "2008-08-08 12:00:00"修改完后,记得执行clock -w,把系统时间写入CMOS date -s ...
- STM32F103RB, KEIL编译出错:cannot open preprocessing output output file ".\神舟i号\main.d" no such file or
STM32F103RB, KEIL编译出错:cannot open preprocessing output output file ".\神舟i号\main.d" no su ...
- Android创建JSON格式数据
Android创建JSON格式数据 作为上一篇博客的补充,简单那解说了一下Android创建JSON格式数据的小Demo. 1. 创建JSON格式数据 对于Android创建JSON格式数据.因为An ...
- RelativeLayout经常使用属性介绍及实例解说
RelativeLayout是一种相对布局.控件的位置是依照相对位置来计算的.后一个控件在什么位置依赖于前一个控件的基本位置,是布局最经常使用,也是最灵活的一种布局. 下边来看一下他的经常使用属性 这 ...