[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
NodeJS开发中,操作zip文件有两大类方法:使用三方模块,如node-zip,或调用外部程序。
调用外部程序是使用execfile等方法启动子程序,但这种方式,在不同系统间通用性很差,所以并不推荐。
而使用三方模块的话,通常模块都不支持操作带有密码的zip文件。
经过许久查找,终于从国外资料站查到一个支持密码的zip模块,以下便分享给大家:
这个模块叫作:unzipper。
直接用源码演示其使用方法,注意图中密码:
源码:
const unzipper = require('unzipper');
(async () => {
try {
const directory = await unzipper.Open.file('demo.zip');
const extracted = await directory.files[0].buffer('123456');
console.log(extracted.toString());
} catch(e) {
console.log(e);
}
})();
用于测试的是demo.TXT文件,内容写入“hello”,然后将文件压缩成:demo.zip,压缩前设置密码为:123456。
然后在Node环境中运行上面的源码:
与期待一致,打开了带密码的压缩文件,并显示出了文件中的内容。
而如果密码不正确会如何呢?
在源码中修改密码字符串为错误的密码,并再次执行,会显示:“BAD_PASSWORD”:
测试完毕,证实此模块可处理带密码的ZIP文件。
补充知识:
NodeJS源码,如同JavaScript一样,也是公开透明的。如果使用NodeJS做的项目要交付给客户,或NodeJS开发的产品要发布,直接交付或发布等于开源了产品。那要如何保护源码呢?方案是对JS源码进行混淆加密。
例如本文的NodeJS代码,可使用JShaman进行混淆加密:
加密后的代码虽然还是明文代码,但成为了不可读、不可理解、不可逆向、不可修改的密文代码:
而且完全不影响正常使用: