ZipUtils.js
6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"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;