nodejs解压zip/rar文件到本地,并获取到解压进度

方案:解压到本地的大小 / zip文件总大小(解压后的) ,得出解压进度

先得出解压后的文件大小,然后解压到本地

复制代码
const AdmZip = require("adm-zip");
const JSZip = require('jszip');

// 指定ZIP文件的路径
const admZip = new AdmZip("D:\\Users\\whr4220\\Downloads\\test (3).rar");
const zip = new JSZip();
// 解压目标目录
const extractPath = './downloads'

// 获取ZIP文件中所有文件的列表
const entries = admZip.getEntries();
// console.log(admZip, entries)


// 初始化已解压文件的总大小
let totalSize = 0;

// 遍历所有文件,计算总大小
entries.forEach(entry => {
  totalSize += entry.header.size;
});
console.log('解压后的大小', totalSize)

// 解压文件到指定目录
admZip.extractAllTo(extractPath, true);

console.log('解压完成');
复制代码
获取指定目录的大小,得出当前解压了多少
复制代码
const fs = require('fs');
const path = require('path');

//获取指定目录的大小
function getFolderSize(folderPath) {
  let totalSize = 0;

  fs.readdirSync(folderPath).forEach(file => {
    const filePath = path.join(folderPath, file);
    const stats = fs.statSync(filePath);

    if (stats.isFile()) {
      totalSize += stats.size;
    } else if (stats.isDirectory()) {
      totalSize += getFolderSize(filePath); // 递归调用
    }
  });

  return totalSize;
}

// 使用函数
const size = getFolderSize('./downloads');
console.log('大小', size);
复制代码

 

posted @   herry菌  阅读(346)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2018-06-27 ES6知识整理(8)--Promise对象
点击右上角即可分享
微信分享提示