ZipUtils.js 6.52 KB
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _fsExtra = _interopRequireDefault(require("fs-extra"));

var _archiver = _interopRequireDefault(require("archiver"));

var _unzipStream = _interopRequireDefault(require("unzip-stream"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var ZipUtils =
/*#__PURE__*/
function () {
  function ZipUtils() {
    _classCallCheck(this, ZipUtils);
  }

  _createClass(ZipUtils, null, [{
    key: "zip",

    /**
     * @description ZipUtils.zip('/xx/floder',floder,'/output/','myzip.zip')
     * @param {String} sourcePath 
     * @param {String} fromFloderName
     * @param {String} destinationFolder 
     * @param {String} zipFileName 
     * @returns {String}
     */
    value: function () {
      var _zip = _asyncToGenerator(
      /*#__PURE__*/
      regeneratorRuntime.mark(function _callee(sourcePath, fromFloderName, destinationFolder, zipFileName) {
        var zipFilePath, archive, outputSteam;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                zipFilePath = destinationFolder + zipFileName;

                _fsExtra.default.ensureDirSync(destinationFolder);

                archive = (0, _archiver.default)("zip");
                outputSteam = _fsExtra.default.createWriteStream(zipFilePath);
                _context.next = 6;
                return archive.pipe(outputSteam);

              case 6:
                _context.next = 8;
                return archive.directory(sourcePath, fromFloderName);

              case 8:
                _context.next = 10;
                return archive.finalize();

              case 10:
                return _context.abrupt("return", zipFilePath);

              case 11:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      return function zip(_x, _x2, _x3, _x4) {
        return _zip.apply(this, arguments);
      };
    }()
    /**
     * @description ZipUtils.unzip("/tmp/myzip.zip",/tmp/output)
     * @param {String} zipFilePath 
     * @param {String} extractToPath 
     */

  }, {
    key: "unzip",
    value: function () {
      var _unzip2 = _asyncToGenerator(
      /*#__PURE__*/
      regeneratorRuntime.mark(function _callee4(zipFilePath, extractToPath) {
        return regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.next = 2;
                return new Promise(function (subResolve, subReject) {
                  _fsExtra.default.createReadStream(zipFilePath).pipe(_unzipStream.default.Extract({
                    path: extractToPath
                  })).on("close",
                  /*#__PURE__*/
                  function () {
                    var _ref = _asyncToGenerator(
                    /*#__PURE__*/
                    regeneratorRuntime.mark(function _callee2(entry) {
                      return regeneratorRuntime.wrap(function _callee2$(_context2) {
                        while (1) {
                          switch (_context2.prev = _context2.next) {
                            case 0:
                              console.log('extract file successfully');
                              subResolve(entry);

                            case 2:
                            case "end":
                              return _context2.stop();
                          }
                        }
                      }, _callee2);
                    }));

                    return function (_x7) {
                      return _ref.apply(this, arguments);
                    };
                  }()).on("error",
                  /*#__PURE__*/
                  function () {
                    var _ref2 = _asyncToGenerator(
                    /*#__PURE__*/
                    regeneratorRuntime.mark(function _callee3(error) {
                      return regeneratorRuntime.wrap(function _callee3$(_context3) {
                        while (1) {
                          switch (_context3.prev = _context3.next) {
                            case 0:
                              console.log('extract file failed');
                              subReject(error);

                            case 2:
                            case "end":
                              return _context3.stop();
                          }
                        }
                      }, _callee3);
                    }));

                    return function (_x8) {
                      return _ref2.apply(this, arguments);
                    };
                  }());
                });

              case 2:
                return _context4.abrupt("return", extractToPath);

              case 3:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4);
      }));

      return function unzip(_x5, _x6) {
        return _unzip2.apply(this, arguments);
      };
    }()
  }]);

  return ZipUtils;
}();

var _default = ZipUtils;
exports.default = _default;