| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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);
- },
- /**
- * 修改 .eep 压缩文件中的某个文件内容
- * @param {string} archivePath EEP 文件路径
- * @param {string} entryPath 压缩包内文件路径
- * @param {Buffer} content 新文件内容
- */
- modifyEepFileSync: async (archivePath, entryPath, content, encoding = 'utf8', password = '') => {
- return await ipcRenderer.invoke('zip:update-entry-with-7z', {
- archivePath,
- entryPath,
- content,
- encoding,
- password,
- });
- },
- /**
- * 打开文件对话框
- * @param {Object} opts 对话框选项
- * @returns {Promise} 文件路径
- */
- openFileDialog: (opts) => ipcRenderer.invoke('dialog:openFiles', opts),
- });
- // 简单的键值存储 API
- contextBridge.exposeInMainWorld('electronStore', {
- get: (key) => ipcRenderer.invoke('store:get', key),
- set: (key, value) => ipcRenderer.invoke('store:set', key, value),
- delete: (key) => ipcRenderer.invoke('store:delete', key),
- });
|