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: '导入失败' });
        }
相关推荐
c***V32324 分钟前
Vue优化
前端·javascript·vue.js
努力往上爬de蜗牛1 小时前
react native真机调试
javascript·react native·react.js
y***86693 小时前
TypeScript在Electron应用中的使用
javascript·typescript·electron
zy happy5 小时前
若依 vue3 报错:找不到模块“@/api/xxxx/xxxxx”或其相应的类型声明。。Vue 3 can not find mod
前端·javascript·vue.js
tobebetter95275 小时前
How to manage python versions on windows
开发语言·windows·python
meichaoWen5 小时前
【Vue3】vue3的全面学习(一)
前端·javascript·学习
9***P3346 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
CoderYanger6 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz6 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
多多*6 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven