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: '导入失败' });
        }
相关推荐
明天更新1 分钟前
Java处理压缩文件的两种方式!!!!
java·开发语言·7-zip
老胖闲聊7 分钟前
C# 注册表操作类
开发语言·c#
勘察加熊人9 分钟前
Python+Streamlit实现登录页
开发语言·python
理智的煎蛋14 分钟前
keepalived+lvs
java·开发语言·集成测试·可用性测试
Bl_a_ck1 小时前
【React】Craco 简介
开发语言·前端·react.js·typescript·前端框架
编程有点难1 小时前
Python训练打卡Day23
开发语言·python
程序员爱钓鱼1 小时前
跳转语句:break、continue、goto -《Go语言实战指南》
开发语言·后端·golang·go1.19
hardStudy_h1 小时前
C程序的存储空间分配
c语言·开发语言
橙子199110161 小时前
Kotlin 中的 Unit 类型的作用以及 Java 中 Void 的区别
java·开发语言·kotlin
yours_Gabriel1 小时前
【登录认证】JWT令牌
java·开发语言·redis