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: '导入失败' });
        }
相关推荐
lizz3114 小时前
C++模板编程:从入门到精通
java·开发语言·c++
shoubepatien15 小时前
JAVA -- 05
java·开发语言
寰天柚子15 小时前
Java并发编程中的线程安全问题与解决方案全解析
java·开发语言·python
沐知全栈开发15 小时前
Bootstrap 下拉菜单:设计与实现指南
开发语言
晚烛15 小时前
实战前瞻:构建高可靠、强协同的 Flutter + OpenHarmony 智慧教育平台
javascript·flutter·html
Evand J16 小时前
【MATLAB例程】多锚点RSSI定位和基站选择方法,基于GDOP、基站距离等因素。以Wi-Fi定位为例,附下载链接
开发语言·matlab·定位·gdop·rssi
superman超哥16 小时前
仓颉语言中锁的实现机制深度剖析与并发实践
c语言·开发语言·c++·python·仓颉
JAVA+C语言16 小时前
String Constant Pool
java·开发语言
保护我方头发丶16 小时前
ESP-wifi-蓝牙
前端·javascript·数据库
郝学胜-神的一滴16 小时前
OpenGL的glDrawElements函数详解
开发语言·c++·程序人生·游戏·图形渲染