Skip to content

Commit

Permalink
压缩和解压缩
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo committed Oct 25, 2024
1 parent d519e48 commit 71bcaf0
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
68 changes: 67 additions & 1 deletion pbottleRPA.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ const childProcess = require('child_process');
/**
* 当前脚本的路径,结尾无/ 如 'D:/pbottleRPAdemo'
*/
const jsPath = path.resolve('./');
const jsPath = path.resolve('./');
const CppUrl = `http://127.0.0.1:49888/`
let basePath = ''; //基座路径

console.log("基座服务地址:(NodeJS)",CppUrl);
exports.jsPath = jsPath
exports.basePath = basePath
exports.__dirname = jsPath
exports.目录路径 = jsPath

Expand All @@ -40,6 +42,22 @@ exports.设置默认操作延时 = setDefaultDelay



/**
* 获取并设置基座平台的根目录路径 | V2025.0 以上版本启用
* @returns {string}
*/
function getBasePath() {
if (basePath) {
return basePath;
}
let url = `${CppUrl}?action=basePath`
let res = request('GET', url);
basePath = res.getBody('utf8')
return basePath;
}
exports.getBasePath = getBasePath


/**
* 发出系统警告声音
* @returns
Expand Down Expand Up @@ -814,6 +832,54 @@ var aiObject= (minimumScore=0.5, x=0, y=0, width=-1, height=-1)=>{
exports.aiObject = aiObject
exports.物体识别 = aiObject


/**
* 压缩文件夹内容成一个zip文件包 v2025.0 以后版本生效
* @param {string} directory 文件夹路径,输入绝对路径
* @param {string} zipFilePath zip文件包
*/
function zipDir(directory,zipFilePath="") {
let basePath = getBasePath()
if (!zipFilePath) {
zipFilePath = path.join(directory,'RPA生成的压缩包.zip')
}
try {
zipFilePath = path.join(zipFilePath)
directory = path.join(directory)
let exe = path.join(`${basePath}/bin/7za`)
childProcess.execSync(`"${exe}" a "${zipFilePath}" "${directory}"`, { stdio: ['ignore', 'ignore', 'pipe'], encoding: 'utf8' })
} catch (error) {
if (!error.stderr.includes('Headers Error')) { //warning
console.error(`压缩失败`, error);
}
}
}
exports.zipDir = zipDir
exports.压缩 = zipDir


/**
* 解压缩zip文件内容到指定文件夹内 v2025.0 以后版本生效
* @param {string} zipFilePath zip文件包
* @param {string} directory 文件夹路径,输入绝对路径 默认解压到zip文件当前目录
*/
function unZip(zipFilePath,directory="") {
let basePath = getBasePath()
if (!directory) {
directory = path.dirname(zipFilePath)
}
try {
filePath = path.join(zipFilePath)
directory = path.join(directory)
let exe = path.join(`${basePath}/bin/7za`)
childProcess.execSync(`"${exe}" x "${filePath}" -o"${directory}" -aoa`, { stdio: ['ignore', 'ignore', 'pipe'], encoding: 'utf8' })
} catch (error) {
console.error(`解压缩失败`, error);
}
}
exports.unZip = unZip
exports.解压缩 = unZip

/**
* 获取buffer存储内容
* 此buffer可以跨脚本存取,RPA重启时才重置,存取多线程下安全
Expand Down
28 changes: 28 additions & 0 deletions 压缩和解压缩示例.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 小瓶RPA演示demo,具体api请查看*流程开发文档*
* 官网:https://rpa.pbottle.com/
* 流程开发文档:https://gitee.com/pbottle/pbottle-rpa/wikis/pages
*/
const pbottleRPA = require('./pbottleRPA')
const fs = require("fs");


pbottleRPA.log('压缩文件测试')
pbottleRPA.zipDir(pbottleRPA.__dirname+'/input',pbottleRPA.__dirname+'/目标压缩包.zip')
pbottleRPA.wait(2)


console.log('监测压缩结果');
if (!fs.existsSync(pbottleRPA.__dirname+'/目标压缩包.zip')) {
pbottleRPA.exit('未检测到,退出!~')
}


console.log('解压文件测试')
pbottleRPA.unZip(pbottleRPA.__dirname+'/目标压缩包.zip',pbottleRPA.__dirname+'/解压目录/')


console.log('压缩测试完成:正在打开目录');
pbottleRPA.openDir(pbottleRPA.__dirname)


0 comments on commit 71bcaf0

Please sign in to comment.