首页 > 编程语言 >Node rar压缩/解压文件

Node rar压缩/解压文件

时间:2023-06-14 21:34:41浏览次数:44  
标签:Node 解压 rarPath err cmd param rar config


暂时未发现node有好用的rar解压/压缩库,所以就自己搜索了一下,简单写了一个,并做了个简单的封装。

rar文件的压缩/解压是通过命令行来完成的,所以就需要node 的child_process库,这个npm i xxx就可以了。

大概了解了下rar的语法,这个在WinRAR的安装文件目录下可以看到,我的是:C:/Program Files/WinRAR/WinRAR.chm,

rar命令和rar参数都可以看到,这里就不贴图了,

压缩文件命令:rar a rarPath srcPath -y,其中rarPath是要压缩到的文件地址,例如test.rar,srcPath是要压缩的文件或者文件夹,-y表示如果有提示确认,同意所有操作

解压文件命令:rar x rarPath destPath -y,其中rarPath是压缩文件的地址,例如test.rar,destPath是解压的路径,-y表示如果有提示确认,同意所有操作

const fs = require('fs');
const exec = require('child_process').exec;

/**
 * 执行cmd
 * @param cmd
 */
function execute(cmd) {
    exec(cmd, {encoding: 'binary'}, function (err, stdout, stderr) {
        if (err) {
            console.log(`err:${err}`);
        }
        if (stderr) {
            console.log(`stderr:${stderr}`);
        }
    });
}

/**
 * 判断文件或者路径是否存在
 * @param path
 * @param success 成功回调函数
 * @param error 错误回调函数
 */
function exist(path, success, error) {
    fs.exists(path, (exists) => {
        if (exists) {
            success && success();
        } else {
            console.log(`${path} not exist and create...`);
            error && error();
        }
    });
}

const rar = {
    /**
     * 压缩文件
     * @param config
     */
    compress: function (config) {
        let cmd = `rar a ${config.rarPath} ${config.srcPath} -y`;
        exist(config.srcPath, () => {
            execute(cmd);
        });
    },
    /**
     * 解压文件
     * @param config
     */
    decompress: function (config) {
        let cmd = `rar x ${config.rarPath} ${config.destPath} -y`;
        exist(config.rarPath, () => {
            exist(config.destPath, () => {
                execute(cmd);
            }, () => {
                fs.mkdir(config.destPath, (err) => {
                    if (err) {
                        console.log(err);
                    } else {
                        execute(cmd);
                    }
                });
            });
        });
    }
};

module.exports = rar;

测试代码:

Node rar压缩/解压文件_node

测试结果:

Node rar压缩/解压文件_rar压缩/解压_02

Node rar压缩/解压文件_解压文件_03

 

标签:Node,解压,rarPath,err,cmd,param,rar,config
From: https://blog.51cto.com/u_16159492/6481506

相关文章

  • Node Mysql操作封装
    由于最近要写个签到系统,频繁的操作mysql导致代码量暴涨,就想着优化下SQL的结构,减少工作量。'usestrict';varmysql=require('mysql');//数据库配置module.exports={/***数据库配置*/config:{host:'localhost',port:3306,......
  • 【JS错题总结】node中的微任务
    答案是n1n2p1p2 原因:node中的微任务包含两部分:1.process.nextTick()注册的回调(nextTicktaskqueue)2.promise.then()注册的回调(promise taskqueue) node在执行微任务时,会优先执行nextTicktaskqueue中的任务,执行完之后接着执行promisetaskqueue......
  • jbpm4配置library
    右键项目名称->BuildPath->ConfigureBuildPath->Libraries->AddExternalJARs...选择jbpm4.4文件夹下面的jbpm.jar然后添加jbpm4.4/lib文件夹下面的所有jar包JRESystemLibrary系统换成要和自己的保持一致,我是1.8的......
  • yarn 安装进行时,显现错误 node_modules\gifsicle: Command failed.
    1.错误显示[4/5]Buildingfreshpackages...[7/13]⠠jpegtran-bin[6/13]⠠gifsicle[8/13]⠠mozjpeg[4/13]⠠gifsicleerrorE:\IdeaStudyProjects\ucthings-ui-vue\node_modules\gifsicle:Commandfailed.Exitcode:1Command:nodelib/install.jsArguments:D......
  • npm install报错[email protected] postinstall: `node scripts/build.js`
    [email protected]: nodescripts/build.js解决方法:npmconfigsetsass_binary_site=https://npm.taobao.org/mirrors/node-sassnpminstall  ......
  • [nodejs] __dirname is not defined in ES module scope
    原因:CommonJS中提供的全局变量如require, exports, module.exports, __filename, __dirname等,在ESModules环境中均是不可用的,require, exports, module.exports在ESModules中基本对应着import,export,exportdefault。解决:import{dirname}from"node:path......
  • NodeJS研究笔记:利用Buffer类的二进制数据读取接口解析ELF文件格式
    javascript作为前端开发语言,自古来对二进制数据的读取解析方面的支持都很薄弱,一般来说,解析二进制数据时,往往是将数据转换成字符串,然后运用各种字符串操作技巧来实现二进制数据的读取。由于NodeJS作为后台服务器开发平台,数理逻辑的设计需求超越javascript作为前端语言时界面UI的设......
  • RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2 新增解压缩工具类ZipHelper
    在项目对文件进行解压缩是非常常用的功能,对文件进行压缩存储或传输可以节省流量与空间。压缩文件的格式与方法都比较多,比较常用的国际标准是zip格式。压缩与解压缩的方法也很多,在.NET2.0开始,在System.IO.Compression中微软已经给我们提供了解压缩的方法GZipStream。对于GZipSt......
  • nodejs 和 mysql 连接
      原文https://www.mysqltutorial.org/mysql-nodejs/connect/   letmysql=require('mysql')letconnection=mysql.createConnection({host:'119.91.31.144',user:'test1',password:'',//密码database:......
  • ubuntu解压ZIP文件名乱码问题
    背景一个windows压缩的zip文件,再ubuntu中解压后,文件名全是乱码,试过很多GBK、UTF-8的转换方法后找到该方法解决办法安装包sudoapt-getinstallunar使用假设需要解压的ZIP包是foo.zip列出所有文件lsarfoo.zip>如果列出的文件名已经正确解压所有文件unarfoo.z......