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 分钟前
【Qt】绘图
开发语言·qt
咖啡续命又一天21 分钟前
python 自动化采集 ChromeDriver 安装
开发语言·python·自动化
只_只30 分钟前
npm install sqlite3时报错解决
前端·npm·node.js
FuckPatience35 分钟前
Vue ASP.Net Core WebApi 前后端传参
前端·javascript·vue.js
全马必破三1 小时前
Buffer:Node.js 里处理二进制数据的 “小工具”
前端·node.js
huohaiyu1 小时前
synchronized (Java)
java·开发语言·安全·synchronized
一枚前端小能手1 小时前
🔥 SSR服务端渲染实战技巧 - 从零到一构建高性能全栈应用
前端·javascript
Komorebi_99991 小时前
Vue3 provide/inject 详细组件关系说明
前端·javascript·vue.js
_OP_CHEN1 小时前
C++基础:(九)string类的使用与模拟实现
开发语言·c++·stl·string·string类·c++容器·stl模拟实现
蓝天智能1 小时前
QT MVC中View的特点及使用注意事项
开发语言·qt·mvc