Node.js使用jszip实现文件夹操作

5 篇文章 0 订阅
3 篇文章 0 订阅
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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值