文件管理异步修复

This commit is contained in:
小朱
2025-07-10 22:27:49 +08:00
parent 382cc5eb0d
commit e8b6b696c5
2 changed files with 28 additions and 11 deletions

View File

@@ -515,8 +515,8 @@ const FileManagerPage: React.FC = () => {
if (success) {
addNotification({
type: 'success',
title: '压缩成功',
message: `成功创建压缩文件 "${archiveName}"`
title: '压缩任务已下发',
message: `异步操作,详细进度可查看任务栏 "${archiveName}"`
})
}
setCompressDialog({ visible: false, files: [] })

View File

@@ -126,22 +126,39 @@ export class CompressionWorker {
const stream = createReadStream(archivePath)
.pipe(unzipper.Parse())
stream.on('entry', (entry) => {
stream.on('entry', async (entry) => {
totalFiles++
const fileName = entry.path
const type = entry.type
const filePath = path.join(targetPath, fileName)
if (type === 'File') {
entry.pipe(createWriteStream(filePath))
entry.on('close', () => {
extractedFiles++
const progress = Math.floor((extractedFiles / totalFiles) * 90) + 5
taskManager.updateTask(taskId, {
message: `正在解压... (${extractedFiles}/${totalFiles})`,
progress: Math.min(95, progress)
try {
// 确保文件所在的目录存在
const fileDir = path.dirname(filePath)
await fs.mkdir(fileDir, { recursive: true })
entry.pipe(createWriteStream(filePath))
entry.on('close', () => {
extractedFiles++
const progress = Math.floor((extractedFiles / totalFiles) * 90) + 5
taskManager.updateTask(taskId, {
message: `正在解压... (${extractedFiles}/${totalFiles})`,
progress: Math.min(95, progress)
})
})
})
} catch (error) {
console.error(`创建目录失败: ${path.dirname(filePath)}`, error)
entry.autodrain()
}
} else if (type === 'Directory') {
// 处理目录条目
try {
await fs.mkdir(filePath, { recursive: true })
} catch (error) {
console.error(`创建目录失败: ${filePath}`, error)
}
entry.autodrain()
} else {
entry.autodrain()
}