1 npm install --save-dev jszip 引入jszip依赖
2
const fs = require('fs');
var stat = fs.stat;
const path = require('path');
const JSZip = require('jszip');
const zip = new JSZip();
3:
//copy指定文件到指定路径下
jsCopyFileToPath(formPath, toPath) {
let tempfilename = this.getFileName(formPath);
fs.readFile(formPath, function (err, data) {
if (err) {
throw err;
}
fs.writeFile(toPath + tempfilename, data, function (error) {
if (error) {
throw error;
}
});
});
},
4:根据文件名称删除文件
deletepicByfileName(picfilePath) {
fs.unlink(picfilePath, function (err) {
if (err) {
throw err;
}
});
},
5:读取压缩文件
startZIP(currPath, zipName) {
var tempPath = path.join(currPath, 'app');
if (fs.existsSync(tempPath)) {
readDir(zip, tempPath);
}
if (isFail) {
return zip.generateAsync({
type: 'nodebuffer', //nodejs用
compression: 'DEFLATE', //压缩算法
compressionOptions: {
level: 9, //压缩级别
},
})
.then(function (content) {
fs.writeFileSync(zipName, content, 'utf-8'); //将打包的内容写入 当前目录下的 result.zip中
});
}
},
//读取目录及文件
var readDir = function (obj, nowPath) {
let tempmes = "日志文件不存在或格式不正确"
let files = fs.readdirSync(nowPath); //读取目录中的所有文件及文件夹(同步操作)
try {
files.forEach(function (fileName, index) {
console.log(fileName, index); //打印当前读取的文件名
let fillPath = nowPath + '\\' + fileName;
let file = fs.statSync(fillPath);
if (parseInt(file.size / 1024 / 1024) > 20) {//单个文件大于20M
tempmes = "日志文件格式不正确,单个日志文件超过20M"
throw Error();
}
if (CheckChinese(fileName)) {
if (file.isDirectory()) {
let dirlist = zip.folder(fileName); //压缩对象中生成该目录
readDir(dirlist, fillPath); //重新检索目录文件
} else {
isFail = true;
if (fileName.indexOf('log') === -1) {
obj.file(fileName, fs.readFileSync(fillPath));
}
}
}
});
} catch (e) {
openConfControlWindow(tempmes);
closelogwindow()
isFail = false;
}
}
6:复制文件夹
//读取目录及文件
var readDir = function (obj, nowPath) {
let tempmes = "日志文件不存在或格式不正确"
let files = fs.readdirSync(nowPath); //读取目录中的所有文件及文件夹(同步操作)
try {
files.forEach(function (fileName, index) {
console.log(fileName, index); //打印当前读取的文件名
let fillPath = nowPath + '\\' + fileName;
let file = fs.statSync(fillPath);
if (parseInt(file.size / 1024 / 1024) > 20) {//单个文件大于20M
tempmes = "日志文件格式不正确,单个日志文件超过20M"
throw Error();
}
if (CheckChinese(fileName)) {
if (file.isDirectory()) {
let dirlist = zip.folder(fileName); //压缩对象中生成该目录
readDir(dirlist, fillPath); //重新检索目录文件
} else {
isFail = true;
if (fileName.indexOf('log') === -1) {
obj.file(fileName, fs.readFileSync(fillPath));
}
}
}
});
} catch (e) {
openConfControlWindow(tempmes);
closelogwindow()
isFail = false;
}
}
var copy = function (src, dst) {
fs.readdir(src, function (err, paths) {
console.log(paths)
if (err) {
throw err;
}
paths.forEach(function (path) {
var _src = src + '/' + path;
var _dst = dst + '/' + path;
var readable;
var writable;
stat(_src, function (err, st) {
if (err) {
throw err;
}
if (st.isFile()) {
readable = fs.createReadStream(_src);
writable = fs.createWriteStream(_dst);
readable.pipe(writable);
} else if (st.isDirectory()) {
exists(_src, _dst, copy);
}
});
});
});
}
//判断是文件还是文件夹,并且创建文件夹
var exists = function (src, dst, callback) {
fs.exists(dst, function (exists) {
if (exists) {
callback(src, dst);
} else {
fs.mkdir(dst, function () {//创建目录
callback(src, dst)
})
}
})
}
调用: exists(frompath, topath, copy)