node 下载 解压 重命名

Node.js文件处理与压缩

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>注意力</title>

<style>
html,
body {
width: 100%;
height: 100%;
}
</style>
</head>

<body>

</body>
<script src="../src/js/lib/jquery.min.js"></script>

<script src="../src/js/global.js"></script>

<script>
var http = require('http');
var fs = require('fs');
var request = require('request');
var unzip = require('unzip');

/*---- 下载解压包 ----*/
var download = function (uri, filename, callback) {
request.head(uri, function (err, res, body) {
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};

download('http://ddd1.pc6.com/soft/jfsky.com-msicuu.rar', 'upload/src/a.rar', function () {
console.log('done');
});

/*---- 解压 ---*/
var extract = unzip.Extract({
path: 'upload/b/a'
});
extract.on('error', function () {
console.log("解压失败");
});
extract.on('finish', function () {
console.log("解压成功");
/* 判断文件夹是否存在 */
fs.exists("upload/b/a", function (exists) {
if (exists) {
console.log("文件夹存在");
/* 重命名文件夹 */
fs.rename('upload/b/a', 'upload/b/c', function (err) {
if (err) {
console.log("重命名失败");
} else {
console.log("重命名成功");
/* 删除文件夹 */
fs.rmdir('upload/b/a', function (err) {
if (err) {
console.log("删除失败");
} else {
console.log("删除成功");
}
});
}
});
} else {
console.log("文件夹不存在");
}
});
});
fs.createReadStream('upload/src/a.zip').pipe(extract);


/* 读取文件 获取应用中的版本号 */
fs.readFile("package.json", function (err, res) {
if (err) throw err;
/* console.log(JSON.parse(res.toString())); */
});

console.log("执行结束");
</script>

</html>

 

转载于:https://www.cnblogs.com/limit1/p/6023849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值