验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
File.js
原始数据 复制代码
var fs = require('fs')
var URL = require('url').URL
var archiver = require('archiver')
/**
* 对文件的操作,包括复制,压缩,删除
*/
class File {
/**
* 复制单个文件
* @param {String} sourcePath 原路径
* @param {String} fileName 文件名
* @param {String} destPath 目标路径
* @returns {Promise}
*/
copy(sourcePath, fileName, destPath) {
return new Promise((resolve, reject) => {
const sourceFile = new URL(sourcePath + '/' + fileName)
console.log(`正在copy文件 ${fileName} 从 ${sourcePath} 到 ${destPath} ......`)
let destFile = destPath + '' + fileName
destFile = destFile.indexOf('file:') === -1 ? destFile : new URL(destFile)
var readStream = fs.createReadStream(sourceFile)
var writeStream = fs.createWriteStream(destFile)
readStream.pipe(writeStream)
writeStream.on('close', () => {
resolve(fileName + ' copy successfully!')
})
writeStream.on('error', (error) => {
reject(error)
})
readStream.on('error', (error) => {
reject(error)
})
})
}
/**
* 压缩文件夹
* @param {String} sourceDir 待压缩的文件夹地址
* @param {*} outputFile 输出文件的地址
* @returns {Promise}
*/
deflate(sourceDir, outputFile) {
return new Promise((resolve, reject) => {
console.log(`正在压缩${sourceDir}到${outputFile}......`)
var output = fs.createWriteStream(outputFile)
var archive = archiver('zip')
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', function () {
console.log(archive.pointer() + ' total bytes')
console.log('archiver has been finalized and the output file descriptor has closed.')
resolve(outputFile)
})
// This event is fired when the data source is drained no matter what was the data source.
// It is not part of this library but rather from the NodeJS Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
output.on('end', function () {
console.log('Data has been drained')
})
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function (err) {
if (err.code === 'ENOENT') {
// log warning
console.log(err)
} else {
// throw error
reject(err)
}
})
// good practice to catch this error explicitly
archive.on('error', function (err) {
reject(err)
})
// pipe archive data to the file
archive.pipe(output)
archive.directory(sourceDir, false)
archive.finalize()
})
}
/**
* 删除单个文件
* @param {String} targetFile 待删除文件的地址
* @returns {Promise}
*/
del(targetFile) {
return new Promise((resolve, reject) => {
console.log(`正在删除文件${targetFile}...`)
fs.unlink(targetFile, (err) => {
if (err) {
reject(err)
} else {
resolve(`${targetFile} 删除成功!`)
}
})
})
}
/**
* 同步删除整个文件夹
* @param {String} targetDir 目标文件夹的地址
*/
deleteDirSync(path) {
var files = []
var self = this
console.log(`正在删除文件夹${path}...`)
if (fs.existsSync(path)) {
files = fs.readdirSync(path)
files.forEach(function (file, index) {
var curPath = path + '/' + file
if (fs.statSync(curPath).isDirectory()) { // recurse
self.deleteDirSync(curPath)
} else { // delete file
fs.unlinkSync(curPath)
}
})
fs.rmdirSync(path)
}
}
}
module.exports = File

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助