其实要解决的问题,很简单,获取远程文件,然后解压到本地读取。
在vscode中通过node.js来实现是比较方便的,相比之前的zip.js,我觉得我还是比较喜欢node.js实现方式。
test.js如下内容:
//文件下载var fs = require("fs");var path = require("path");var request = require("request");//创建文件夹目录var dirPath = path.join(__dirname, "file");if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath); console.log("文件夹创建成功");} else { console.log("文件夹已存在");}//循环多线程下载for (let i = 0; i < 1; i++) { let fileName = "apache-tomcat-8.5.38.zip" ; let url = "http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v8.5.38/bin/" + fileName; let stream = fs.createWriteStream(path.join(dirPath, fileName)); request(url).pipe(stream).on("close", function (err) { console.log("文件[" + fileName + "]下载完毕"); });}//整数转字符串,不足的位数用0补齐function intToString(num, len) { let str = num.toString(); while (str.length < len) { str = "0" + str; } return str;}
如何解压到本地某个文件夹?
unzip.js内容如下:
var fs = require("fs");var unzip = require("unzip");fs.createReadStream('C://Users//test//Desktop//file//apache-tomcat-8.5.38.zip').pipe(unzip.Extract({ path: 'C://Users//test//Desktop//file' }));
如何读取本地某个文件夹的文件内容呢?
readFile.js内容如下:
const fs = require("fs");let readFile = path => { return new Promise((resolve,reject) => { fs.readFile(path,"utf-8",(err,data) => { if(err){ reject(err); } else{ resolve(data); } }); });};readFile("C://Users//test//Desktop//file//apache-tomcat-8.5.38/BUILDING.txt").then(data => { console.log(data);}).catch(err => { console.log("err");});