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

[复制链接]
发表于 2025-11-19 00:33:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
场景:当我处理惩罚electron分析压缩包实现进度条的过程中发现终极的文件字节数有缺失,且有些字节为0,利用tar库研究未果!
办理方案:引入别的库办理tar-fs/tar-stream/zlib
  1. // 如果你也需要在electron中使用可以参考:https://blog.csdn.net/jyl919221lc/article/details/142764766?spm=1001.2014.3001.5501
  2.                 try {
  3.             // 第一步:读取 tar.gz 文件,获取第一个目录名称
  4.             const extractor = tarStream.extract();
  5.             let extractedFolderName = null;
  6.             extractor.on('entry', (header, stream, next) => {
  7.                 if (!extractedFolderName && header.type === 'directory') {
  8.                     extractedFolderName = header.name.split('/')[0];
  9.                 }
  10.                 stream.resume(); // 跳过具体内容
  11.                 next();
  12.             });
  13.             extractor.on('error', (err) => {
  14.                 console.error('Error reading tar file to get folder name:', err);
  15.                 resolve({ code: 0, msg: '导入失败:无法读取文件获取文件夹名称' });
  16.             });
  17.             extractor.on('finish', () => {
  18.                 if (!extractedFolderName) {
  19.                     resolve({ code: 0, msg: '导入失败:无法确定解压后的文件夹名称' });
  20.                     return;
  21.                 }
  22.                 // 第二步:解压 tar.gz 文件到指定目录
  23.                 const extract = tarFS.extract(diagnosisDir);
  24.                 const readStream = fs.createReadStream(tarFilePath);
  25.                 const gunzip = zlib.createGunzip(); // 创建 gunzip 解压缩流
  26.                 let processedSize = 0;
  27.                 const totalSize = fs.statSync(tarFilePath).size;
  28.                 readStream.on('data', (chunk) => {
  29.                     processedSize += chunk.length;
  30.                     const progress = parseFloat((processedSize / totalSize).toFixed(2));
  31.                     // mainWindow.setProgressBar(progress); 展示出electron进度条
  32.                 });
  33.                 readStream.on('error', (err) => {
  34.                     console.error('Error reading the archive:', err);
  35.                     resolve({ code: 0, msg: '导入失败:读取文件失败' });
  36.                 });
  37.                 extract.on('error', (err) => {
  38.                     console.error('Error extracting the archive:', err);
  39.                     resolve({ code: 0, msg: '导入失败:解压文件失败' });
  40.                 });
  41.                 extract.on('finish', () => {
  42.                     console.log('Extraction completed');
  43.                     // mainWindow.setProgressBar(-1); // 重置进度条
  44.                     const fullPath = path.join(diagnosisDir, extractedFolderName);
  45.                     console.log(fullPath, 'fullPath');
  46.                     resolve({ code: 1, msg: '导入成功', path: fullPath });
  47.                 });
  48.                 // 管道:读取文件 -> 解压缩 -> 提取 tar 文件
  49.                 readStream.pipe(gunzip).pipe(extract);
  50.             });
  51.             // 读取 tar.gz 文件以获取第一个目录名称
  52.             const readStreamForFolderName = fs.createReadStream(tarFilePath);
  53.             const gunzipForFolderName = zlib.createGunzip(); // 创建 gunzip 解压缩流
  54.             readStreamForFolderName.pipe(gunzipForFolderName).pipe(extractor);
  55.         } catch (error) {
  56.             console.error('Error during extraction:', error);
  57.             resolve({ code: 0, msg: '导入失败' });
  58.         }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表