| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- const { contextBridge, ipcRenderer } = require('electron');
- const path = require('path');
- const fs = require('fs');
- // 公开给渲染进程的 API
- contextBridge.exposeInMainWorld('fileAPI', {
- /**
- * 用主进程的 7z 解压(支持密码)
- * @param {string} filePath EEP 文件路径
- * @param {string} password 密码
- * @returns {Promise} 解压结果
- */
- extractZip: async (filePath, password = '') => {
- return await ipcRenderer.invoke('extract-zip', { filePath, password });
- },
- /**
- * 读取解压后的文件内容
- * @param {string} tempDir 临时解压目录路径
- * @param {string} entryName 文件条目名称
- * @returns {Buffer} 文件内容
- */
- readZipFileSync: (tempDir, entryName) => {
- const filePath = path.join(tempDir, entryName);
- return fs.readFileSync(filePath);
- },
- /**
- * 读取解压后的目录内容
- * @param {string} tempDir 临时解压目录路径
- * @param {string} entryName 目录条目名称
- * @returns {string[]} 目录下的文件和子目录列表
- */
- readZipDirSync: (tempDir, entryName) => {
- const dirPath = path.join(tempDir, entryName);
- return fs.readdirSync(dirPath);
- },
- /**
- * 删除临时解压目录
- * @param {string} tempDir 临时解压目录路径
- */
- deleteTempDir: (tempDir) => {
- fs.rmSync(tempDir, { recursive: true, force: true });
- },
- /**
- * 监听主进程发送的打开 EEP 文件事件
- * @param {Function} callback 回调函数,接收文件路径作为参数
- * @returns {Function} 用于移除监听器的函数
- */
- onOpenEep: (callback) => {
- const handler = (event, filePath) => callback(filePath);
- ipcRenderer.on('open-eep', handler);
- return () => ipcRenderer.removeListener('open-eep', handler);
- },
- /**
- * 打开文件对话框
- * @param {Object} opts 对话框选项
- * @returns {Promise} 文件路径
- */
- openFileDialog: (opts) => ipcRenderer.invoke('dialog:openFiles', opts),
- });
|