当前位置: 首页 > news >正文

如何在Node.js中执行解压缩文件操作

一、解压文件

1.安装依赖:

安装adm-zip依赖包:npm install adm-zip --save

安装iconv-lite依赖包:npm install iconv-lite --save

解压前的file文件夹结构:

update-1.0.2.zip压缩包内容:

2.在depresssFile.js文件,解压zip文件代码,方法一:(解压文件中文件名包含中文推荐使用)

代码中的:

entry.entryName = iconv.decode(entry.rawEntryName, 'utf8');

可以替换成(二选一)

entry.entryName = iconv.decode(entry.rawEntryName, 'gbk');

文件的路径可以写绝对路径也可以写相对路径,绝对路径不容易错,相对路径是depresssFile.js文件到update-1.0.2.zip解压文件的位置

// 引入依赖
const AdmZip = require('adm-zip');
const iconv = require('iconv-lite');// 待解压zip文件所在的路径
var file = 'D:/node/LocalLibrary/file/update-1.0.2.zip';
// 解压后存放的文件夹
var target = 'D:/node/LocalLibrary/file';// 方法1:解压zip文件
function decompressFile1(file, target) {const zip = new AdmZip(file);var zipEntries = zip.getEntries();for (var i = 0; i < zipEntries.length; i++) {var entry = zipEntries[i];entry.entryName = iconv.decode(entry.rawEntryName, 'utf8');}zip.extractAllTo(target, true);
}// 执行函数
decompressFile1(file, target);// 导出(在其他js文件引用decompressFile函数需要添加以下代码)
module.exports = decompressFile1;

解压zip文件代码,方法二:

const AdmZip = require('adm-zip');// 待解压zip文件所在的路径
var file = 'D:/node/LocalLibrary/file/update-1.0.2.zip';
// 解压后存放的文件夹
var target = 'D:/node/LocalLibrary/file';// 方法2:解压zip文件
function decompressFile2(file, target) {const zip = new AdmZip(file);zip.extractAllTo(target, true);
}// 调用
decompressFile2(file, target);// 导出
module.exports = decompressFile2;

解压后的file文件夹结构:

二、压缩文件

1.压缩文件代码:(压缩文件的文件路径对应自己要压缩的文件夹路径即可,存放压缩文件的文件路径同理)

// 压缩文件成zip格式
const AdmZip = require('adm-zip');// filePath: 要压缩的文件路径
var filePath = 'D:/node/LocalLibrary/file/update-1.0.2/route/route.js';
// outputPath: 压缩后的文件路径
var outputPath = 'D:/node/LocalLibrary/file/route.zip';// 压缩文件
function compressFile(filePath, outputPath) {const zip = new AdmZip();zip.addLocalFile(filePath);zip.writeZip(outputPath);
}// 调用函数
compressFile(filePath, outputPath);// 导出函数
module.exports = compressFile;

压缩完成的目录结构: 

http://www.lryc.cn/news/470316.html

相关文章:

  • 梦熊 CSP-S模拟赛 T3 youyou 的序列 II
  • 记录下docker部署gitlab-ce-17.5版本及客户端git拉取方式配置
  • opencv-platform实现人脸识别
  • leetcode 有重复字符串的排列组合
  • 【大数据学习 | kafka】kafka的组件架构
  • Python基于TensorFlow实现简单循环神经网络回归模型(SimpleRNN回归算法)项目实战
  • torch.isclose
  • Python记录-字典
  • python读取学术论文PDF文件内容
  • 5550 取数(max)
  • Windows常用网络命令
  • 地磁传感器(学习笔记上)
  • 使用 NumPy 和 Matplotlib 进行高级数据可视化:实践指南
  • mysql 启动报错 ‘/var/run/mysqld/mysqld.sock‘
  • JAVA基础:常用类 (习题笔记)
  • element 按钮变形 el-button样式异常
  • Windows/Linux(服务器)查看显卡的名称
  • 算法基础 - 时间复杂度和空间复杂度(万字长文详解)
  • 【K8S系列】Kubernetes 中 Service IP 地址和端口不匹配问题及解决方案【已解决】
  • 10. 异常处理器
  • python查询并安装项目所依赖的所有包
  • istio多主集群架构验证方法
  • Java全栈经典面试题剖析8】JavaSE高级 -- 线程同步、 线程通信、死锁、线程池
  • linux 驱动, struct file , struct node, private_data
  • ubuntu 硬盘扩容
  • cm211-1刷机教程镜像包
  • Android 15自定义设置导航栏与状态栏,EdgeToEdge适配
  • 设计模式概览
  • 力扣每日一题打卡 684. 冗余连接
  • 什么是微服务中的反应性扩展?