1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.injectFileInZip = exports.injectBufferInZip = exports.injectStreamInZip = void 0;
|
4 | var debug_ = require("debug");
|
5 | var fs = require("fs");
|
6 | var yauzl = require("yauzl");
|
7 | var yazl = require("yazl");
|
8 | var debug = debug_("r2:utils#zip/zipInjector");
|
9 | var InjectType;
|
10 | (function (InjectType) {
|
11 | InjectType[InjectType["FILE"] = 0] = "FILE";
|
12 | InjectType[InjectType["BUFFER"] = 1] = "BUFFER";
|
13 | InjectType[InjectType["STREAM"] = 2] = "STREAM";
|
14 | })(InjectType || (InjectType = {}));
|
15 | function injectStreamInZip(destPathTMP, destPathFINAL, stream, zipEntryPath, zipError, doneCallback) {
|
16 | injectObjectInZip(destPathTMP, destPathFINAL, stream, InjectType.STREAM, zipEntryPath, zipError, doneCallback);
|
17 | }
|
18 | exports.injectStreamInZip = injectStreamInZip;
|
19 | function injectBufferInZip(destPathTMP, destPathFINAL, buffer, zipEntryPath, zipError, doneCallback) {
|
20 | injectObjectInZip(destPathTMP, destPathFINAL, buffer, InjectType.BUFFER, zipEntryPath, zipError, doneCallback);
|
21 | }
|
22 | exports.injectBufferInZip = injectBufferInZip;
|
23 | function injectFileInZip(destPathTMP, destPathFINAL, filePath, zipEntryPath, zipError, doneCallback) {
|
24 | injectObjectInZip(destPathTMP, destPathFINAL, filePath, InjectType.FILE, zipEntryPath, zipError, doneCallback);
|
25 | }
|
26 | exports.injectFileInZip = injectFileInZip;
|
27 | function injectObjectInZip(destPathTMP, destPathFINAL, contentsToInject, typeOfContentsToInject, zipEntryPath, zipError, doneCallback) {
|
28 | yauzl.open(destPathTMP, { lazyEntries: true, autoClose: false }, function (err, zip) {
|
29 | if (err || !zip) {
|
30 | debug("yauzl init ERROR");
|
31 | zipError(err);
|
32 | return;
|
33 | }
|
34 | var zipfile = new yazl.ZipFile();
|
35 | zip.on("error", function (erro) {
|
36 | debug("yauzl ERROR");
|
37 | zipError(erro);
|
38 | });
|
39 | zip.readEntry();
|
40 | zip.on("entry", function (entry) {
|
41 | if (entry.fileName[entry.fileName.length - 1] === "/") {
|
42 | }
|
43 | else if (entry.fileName === zipEntryPath) {
|
44 | }
|
45 | else {
|
46 | zip.openReadStream(entry, function (errz, stream) {
|
47 | if (err || !stream) {
|
48 | debug("yauzl openReadStream ERROR");
|
49 | debug(errz);
|
50 | zipError(errz);
|
51 | return;
|
52 | }
|
53 | var compress = entry.fileName !== "mimetype";
|
54 | zipfile.addReadStream(stream, entry.fileName, { compress: compress });
|
55 | });
|
56 | }
|
57 | zip.readEntry();
|
58 | });
|
59 | zip.on("end", function () {
|
60 | debug("yauzl END");
|
61 | process.nextTick(function () {
|
62 | zip.close();
|
63 | });
|
64 | if (typeOfContentsToInject === InjectType.FILE) {
|
65 | zipfile.addFile(contentsToInject, zipEntryPath);
|
66 | }
|
67 | else if (typeOfContentsToInject === InjectType.BUFFER) {
|
68 | zipfile.addBuffer(contentsToInject, zipEntryPath);
|
69 | }
|
70 | else if (typeOfContentsToInject === InjectType.STREAM) {
|
71 | zipfile.addReadStream(contentsToInject, zipEntryPath);
|
72 | }
|
73 | else {
|
74 | debug("yazl FAIL to inject! (unknown type)");
|
75 | }
|
76 | zipfile.end();
|
77 | var destStream2 = fs.createWriteStream(destPathFINAL);
|
78 | zipfile.outputStream.pipe(destStream2);
|
79 | destStream2.on("finish", function () {
|
80 | doneCallback();
|
81 | });
|
82 | destStream2.on("error", function (ere) {
|
83 | zipError(ere);
|
84 | });
|
85 | });
|
86 | zip.on("close", function () {
|
87 | debug("yauzl CLOSE");
|
88 | });
|
89 | });
|
90 | }
|
91 |
|
\ | No newline at end of file |