node.js文件压缩包解析,反馈解析进度,解析后的文件字节正常

场景:当我处理electron解析压缩包实现进度条的过程中发现最终的文件字节数有缺失,且有些字节为0,使用tar库研究未果!

解决方案:引入其余库解决tar-fs/tar-stream/zlib

javascript 复制代码
// 如果你也需要在electron中使用可以参考:https://blog.csdn.net/jyl919221lc/article/details/142764766?spm=1001.2014.3001.5501
		try {
            // 第一步:读取 tar.gz 文件,获取第一个目录名称
            const extractor = tarStream.extract();
            let extractedFolderName = null;
            extractor.on('entry', (header, stream, next) => {
                if (!extractedFolderName && header.type === 'directory') {
                    extractedFolderName = header.name.split('/')[0];
                }
                stream.resume(); // 跳过具体内容
                next();
            });
            extractor.on('error', (err) => {
                console.error('Error reading tar file to get folder name:', err);
                resolve({ code: 0, msg: '导入失败:无法读取文件获取文件夹名称' });
            });
            extractor.on('finish', () => {
                if (!extractedFolderName) {
                    resolve({ code: 0, msg: '导入失败:无法确定解压后的文件夹名称' });
                    return;
                }
                // 第二步:解压 tar.gz 文件到指定目录
                const extract = tarFS.extract(diagnosisDir);
                const readStream = fs.createReadStream(tarFilePath);
                const gunzip = zlib.createGunzip(); // 创建 gunzip 解压缩流
                let processedSize = 0;
                const totalSize = fs.statSync(tarFilePath).size;
                readStream.on('data', (chunk) => {
                    processedSize += chunk.length;
                    const progress = parseFloat((processedSize / totalSize).toFixed(2));
                    // mainWindow.setProgressBar(progress); 展示出electron进度条
                });
                readStream.on('error', (err) => {
                    console.error('Error reading the archive:', err);
                    resolve({ code: 0, msg: '导入失败:读取文件失败' });
                });
                extract.on('error', (err) => {
                    console.error('Error extracting the archive:', err);
                    resolve({ code: 0, msg: '导入失败:解压文件失败' });
                });
                extract.on('finish', () => {
                    console.log('Extraction completed');
                    // mainWindow.setProgressBar(-1); // 重置进度条
                    const fullPath = path.join(diagnosisDir, extractedFolderName);
                    console.log(fullPath, 'fullPath');
                    resolve({ code: 1, msg: '导入成功', path: fullPath });
                });
                // 管道:读取文件 -> 解压缩 -> 提取 tar 文件
                readStream.pipe(gunzip).pipe(extract);
            });
            // 读取 tar.gz 文件以获取第一个目录名称
            const readStreamForFolderName = fs.createReadStream(tarFilePath);
            const gunzipForFolderName = zlib.createGunzip(); // 创建 gunzip 解压缩流
            readStreamForFolderName.pipe(gunzipForFolderName).pipe(extractor);
        } catch (error) {
            console.error('Error during extraction:', error);
            resolve({ code: 0, msg: '导入失败' });
        }
相关推荐
W是笔名2 分钟前
python___容器类型的数据___序列
开发语言·python
☆cwlulu3 分钟前
try-throw-catch异常捕获流程
开发语言·c++
竹林8186 分钟前
用 Pinata + IPFS 存 NFT 元数据踩了三天坑,我总结了这份完整的前端实现方案
javascript
林希_Rachel_傻希希8 分钟前
web性能优化之延迟加载图片和<inframe>
前端·javascript·面试
漂亮的摩托9 分钟前
深感一无所长,准备试着从零开始写个富文本编辑器
开发语言·php
要开心吖ZSH15 分钟前
Java事务与MySQL事务的关系及MVCC通俗解析
java·开发语言·mysql·mvcc
寻道码路33 分钟前
LangChain4j Java AI 应用开发实战(二十六):多模型集成策略 —— OpenAI、DeepSeek、阿里百炼混合使用
java·开发语言·人工智能·ai
面朝大海,春不暖,花不开38 分钟前
BPF与eBPF简介:核心概念与观测工具概览
开发语言·php·ebpf·bpf·性能观测
ch.ju39 分钟前
Java Programming Chapter 4——Static code block
java·开发语言
弹简特42 分钟前
【Java项目-企悦抽】04-项目演示+项目源码+AI赋能整理接口文档
java·开发语言