analogger 1.28.3 → 1.29.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,959 +0,0 @@
1
- /**
2
- * DO NOT EDIT THIS FILE DIRECTLY.
3
- * This file is generated following the conversion of
4
- * @see [./node_modules/adm-zip/adm-zip.js]{@link ./node_modules/adm-zip/adm-zip.js}
5
- *
6
- **/
7
- import Utils from "./util.mjs";
8
- import pth from "path";
9
- import ZipEntry from "./zipEntry.mjs";
10
- import ZipFile from "./zipFile.mjs";
11
-
12
-
13
-
14
-
15
-
16
- const get_Bool = (...val) => Utils.findLast(val, (c) => typeof c === "boolean");
17
- const get_Str = (...val) => Utils.findLast(val, (c) => typeof c === "string");
18
- const get_Fun = (...val) => Utils.findLast(val, (c) => typeof c === "function");
19
-
20
- const defaultOptions = {
21
- // option "noSort" : if true it disables files sorting
22
- noSort: false,
23
- // read entries during load (initial loading may be slower)
24
- readEntries: false,
25
- // default method is none
26
- method: Utils.Constants.NONE,
27
- // file system
28
- fs: null
29
- };
30
-
31
- export default function (/**String*/ input, /** object */ options) {
32
- let inBuffer = null;
33
-
34
- // create object based default options, allowing them to be overwritten
35
- const opts = Object.assign(Object.create(null), defaultOptions);
36
-
37
- // test input variable
38
- if (input && "object" === typeof input) {
39
- // if value is not buffer we accept it to be object with options
40
- if (!(input instanceof Uint8Array)) {
41
- Object.assign(opts, input);
42
- input = opts.input ? opts.input : undefined;
43
- if (opts.input) delete opts.input;
44
- }
45
-
46
- // if input is buffer
47
- if (Buffer.isBuffer(input)) {
48
- inBuffer = input;
49
- opts.method = Utils.Constants.BUFFER;
50
- input = undefined;
51
- }
52
- }
53
-
54
- // assign options
55
- Object.assign(opts, options);
56
-
57
- // instanciate utils filesystem
58
- const filetools = new Utils(opts);
59
-
60
- if (typeof opts.decoder !== "object" || typeof opts.decoder.encode !== "function" || typeof opts.decoder.decode !== "function") {
61
- opts.decoder = Utils.decoder;
62
- }
63
-
64
- // if input is file name we retrieve its content
65
- if (input && "string" === typeof input) {
66
- // load zip file
67
- if (filetools.fs.existsSync(input)) {
68
- opts.method = Utils.Constants.FILE;
69
- opts.filename = input;
70
- inBuffer = filetools.fs.readFileSync(input);
71
- } else {
72
- throw Utils.Errors.INVALID_FILENAME();
73
- }
74
- }
75
-
76
- // create variable
77
- const _zip = new ZipFile(inBuffer, opts);
78
-
79
- const { canonical, sanitize, zipnamefix } = Utils;
80
-
81
- function getEntry(/**Object*/ entry) {
82
- if (entry && _zip) {
83
- var item;
84
- // If entry was given as a file name
85
- if (typeof entry === "string") item = _zip.getEntry(pth.posix.normalize(entry));
86
- // if entry was given as a ZipEntry object
87
- if (typeof entry === "object" && typeof entry.entryName !== "undefined" && typeof entry.header !== "undefined") item = _zip.getEntry(entry.entryName);
88
-
89
- if (item) {
90
- return item;
91
- }
92
- }
93
- return null;
94
- }
95
-
96
- function fixPath(zipPath) {
97
- const { join, normalize, sep } = pth.posix;
98
- // convert windows file separators and normalize
99
- return join(".", normalize(sep + zipPath.split("\\").join(sep) + sep));
100
- }
101
-
102
- function filenameFilter(filterfn) {
103
- if (filterfn instanceof RegExp) {
104
- // if filter is RegExp wrap it
105
- return (function (rx) {
106
- return function (filename) {
107
- return rx.test(filename);
108
- };
109
- })(filterfn);
110
- } else if ("function" !== typeof filterfn) {
111
- // if filter is not function we will replace it
112
- return () => true;
113
- }
114
- return filterfn;
115
- }
116
-
117
- // keep last character on folders
118
- const relativePath = (local, entry) => {
119
- let lastChar = entry.slice(-1);
120
- lastChar = lastChar === filetools.sep ? filetools.sep : "";
121
- return pth.relative(local, entry) + lastChar;
122
- };
123
-
124
- return {
125
- /**
126
- * Extracts the given entry from the archive and returns the content as a Buffer object
127
- * @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
128
- * @param {Buffer|string} [pass] - password
129
- * @return Buffer or Null in case of error
130
- */
131
- readFile: function (entry, pass) {
132
- var item = getEntry(entry);
133
- return (item && item.getData(pass)) || null;
134
- },
135
-
136
- /**
137
- * Returns how many child elements has on entry (directories) on files it is always 0
138
- * @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
139
- * @returns {integer}
140
- */
141
- childCount: function (entry) {
142
- const item = getEntry(entry);
143
- if (item) {
144
- return _zip.getChildCount(item);
145
- }
146
- },
147
-
148
- /**
149
- * Asynchronous readFile
150
- * @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
151
- * @param {callback} callback
152
- *
153
- * @return Buffer or Null in case of error
154
- */
155
- readFileAsync: function (entry, callback) {
156
- var item = getEntry(entry);
157
- if (item) {
158
- item.getDataAsync(callback);
159
- } else {
160
- callback(null, "getEntry failed for:" + entry);
161
- }
162
- },
163
-
164
- /**
165
- * Extracts the given entry from the archive and returns the content as plain text in the given encoding
166
- * @param {ZipEntry|string} entry - ZipEntry object or String with the full path of the entry
167
- * @param {string} encoding - Optional. If no encoding is specified utf8 is used
168
- *
169
- * @return String
170
- */
171
- readAsText: function (entry, encoding) {
172
- var item = getEntry(entry);
173
- if (item) {
174
- var data = item.getData();
175
- if (data && data.length) {
176
- return data.toString(encoding || "utf8");
177
- }
178
- }
179
- return "";
180
- },
181
-
182
- /**
183
- * Asynchronous readAsText
184
- * @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
185
- * @param {callback} callback
186
- * @param {string} [encoding] - Optional. If no encoding is specified utf8 is used
187
- *
188
- * @return String
189
- */
190
- readAsTextAsync: function (entry, callback, encoding) {
191
- var item = getEntry(entry);
192
- if (item) {
193
- item.getDataAsync(function (data, err) {
194
- if (err) {
195
- callback(data, err);
196
- return;
197
- }
198
-
199
- if (data && data.length) {
200
- callback(data.toString(encoding || "utf8"));
201
- } else {
202
- callback("");
203
- }
204
- });
205
- } else {
206
- callback("");
207
- }
208
- },
209
-
210
- /**
211
- * Remove the entry from the file or the entry and all it's nested directories and files if the given entry is a directory
212
- *
213
- * @param {ZipEntry|string} entry
214
- * @returns {void}
215
- */
216
- deleteFile: function (entry, withsubfolders = true) {
217
- // @TODO: test deleteFile
218
- var item = getEntry(entry);
219
- if (item) {
220
- _zip.deleteFile(item.entryName, withsubfolders);
221
- }
222
- },
223
-
224
- /**
225
- * Remove the entry from the file or directory without affecting any nested entries
226
- *
227
- * @param {ZipEntry|string} entry
228
- * @returns {void}
229
- */
230
- deleteEntry: function (entry) {
231
- // @TODO: test deleteEntry
232
- var item = getEntry(entry);
233
- if (item) {
234
- _zip.deleteEntry(item.entryName);
235
- }
236
- },
237
-
238
- /**
239
- * Adds a comment to the zip. The zip must be rewritten after adding the comment.
240
- *
241
- * @param {string} comment
242
- */
243
- addZipComment: function (comment) {
244
- // @TODO: test addZipComment
245
- _zip.comment = comment;
246
- },
247
-
248
- /**
249
- * Returns the zip comment
250
- *
251
- * @return String
252
- */
253
- getZipComment: function () {
254
- return _zip.comment || "";
255
- },
256
-
257
- /**
258
- * Adds a comment to a specified zipEntry. The zip must be rewritten after adding the comment
259
- * The comment cannot exceed 65535 characters in length
260
- *
261
- * @param {ZipEntry} entry
262
- * @param {string} comment
263
- */
264
- addZipEntryComment: function (entry, comment) {
265
- var item = getEntry(entry);
266
- if (item) {
267
- item.comment = comment;
268
- }
269
- },
270
-
271
- /**
272
- * Returns the comment of the specified entry
273
- *
274
- * @param {ZipEntry} entry
275
- * @return String
276
- */
277
- getZipEntryComment: function (entry) {
278
- var item = getEntry(entry);
279
- if (item) {
280
- return item.comment || "";
281
- }
282
- return "";
283
- },
284
-
285
- /**
286
- * Updates the content of an existing entry inside the archive. The zip must be rewritten after updating the content
287
- *
288
- * @param {ZipEntry} entry
289
- * @param {Buffer} content
290
- */
291
- updateFile: function (entry, content) {
292
- var item = getEntry(entry);
293
- if (item) {
294
- item.setData(content);
295
- }
296
- },
297
-
298
- /**
299
- * Adds a file from the disk to the archive
300
- *
301
- * @param {string} localPath File to add to zip
302
- * @param {string} [zipPath] Optional path inside the zip
303
- * @param {string} [zipName] Optional name for the file
304
- * @param {string} [comment] Optional file comment
305
- */
306
- addLocalFile: function (localPath, zipPath, zipName, comment) {
307
- if (filetools.fs.existsSync(localPath)) {
308
- // fix ZipPath
309
- zipPath = zipPath ? fixPath(zipPath) : "";
310
-
311
- // p - local file name
312
- const p = pth.win32.basename(pth.win32.normalize(localPath));
313
-
314
- // add file name into zippath
315
- zipPath += zipName ? zipName : p;
316
-
317
- // read file attributes
318
- const _attr = filetools.fs.statSync(localPath);
319
-
320
- // get file content
321
- const data = _attr.isFile() ? filetools.fs.readFileSync(localPath) : Buffer.alloc(0);
322
-
323
- // if folder
324
- if (_attr.isDirectory()) zipPath += filetools.sep;
325
-
326
- // add file into zip file
327
- this.addFile(zipPath, data, comment, _attr);
328
- } else {
329
- throw Utils.Errors.FILE_NOT_FOUND(localPath);
330
- }
331
- },
332
-
333
- /**
334
- * Callback for showing if everything was done.
335
- *
336
- * @callback doneCallback
337
- * @param {Error} err - Error object
338
- * @param {boolean} done - was request fully completed
339
- */
340
-
341
- /**
342
- * Adds a file from the disk to the archive
343
- *
344
- * @param {(object|string)} options - options object, if it is string it us used as localPath.
345
- * @param {string} options.localPath - Local path to the file.
346
- * @param {string} [options.comment] - Optional file comment.
347
- * @param {string} [options.zipPath] - Optional path inside the zip
348
- * @param {string} [options.zipName] - Optional name for the file
349
- * @param {doneCallback} callback - The callback that handles the response.
350
- */
351
- addLocalFileAsync: function (options, callback) {
352
- options = typeof options === "object" ? options : { localPath: options };
353
- const localPath = pth.resolve(options.localPath);
354
- const { comment } = options;
355
- let { zipPath, zipName } = options;
356
- const self = this;
357
-
358
- filetools.fs.stat(localPath, function (err, stats) {
359
- if (err) return callback(err, false);
360
- // fix ZipPath
361
- zipPath = zipPath ? fixPath(zipPath) : "";
362
- // p - local file name
363
- const p = pth.win32.basename(pth.win32.normalize(localPath));
364
- // add file name into zippath
365
- zipPath += zipName ? zipName : p;
366
-
367
- if (stats.isFile()) {
368
- filetools.fs.readFile(localPath, function (err, data) {
369
- if (err) return callback(err, false);
370
- self.addFile(zipPath, data, comment, stats);
371
- return setImmediate(callback, undefined, true);
372
- });
373
- } else if (stats.isDirectory()) {
374
- zipPath += filetools.sep;
375
- self.addFile(zipPath, Buffer.alloc(0), comment, stats);
376
- return setImmediate(callback, undefined, true);
377
- }
378
- });
379
- },
380
-
381
- /**
382
- * Adds a local directory and all its nested files and directories to the archive
383
- *
384
- * @param {string} localPath - local path to the folder
385
- * @param {string} [zipPath] - optional path inside zip
386
- * @param {(RegExp|function)} [filter] - optional RegExp or Function if files match will be included.
387
- */
388
- addLocalFolder: function (localPath, zipPath, filter) {
389
- // Prepare filter
390
- filter = filenameFilter(filter);
391
-
392
- // fix ZipPath
393
- zipPath = zipPath ? fixPath(zipPath) : "";
394
-
395
- // normalize the path first
396
- localPath = pth.normalize(localPath);
397
-
398
- if (filetools.fs.existsSync(localPath)) {
399
- const items = filetools.findFiles(localPath);
400
- const self = this;
401
-
402
- if (items.length) {
403
- for (const filepath of items) {
404
- const p = pth.join(zipPath, relativePath(localPath, filepath));
405
- if (filter(p)) {
406
- self.addLocalFile(filepath, pth.dirname(p));
407
- }
408
- }
409
- }
410
- } else {
411
- throw Utils.Errors.FILE_NOT_FOUND(localPath);
412
- }
413
- },
414
-
415
- /**
416
- * Asynchronous addLocalFolder
417
- * @param {string} localPath
418
- * @param {callback} callback
419
- * @param {string} [zipPath] optional path inside zip
420
- * @param {RegExp|function} [filter] optional RegExp or Function if files match will
421
- * be included.
422
- */
423
- addLocalFolderAsync: function (localPath, callback, zipPath, filter) {
424
- // Prepare filter
425
- filter = filenameFilter(filter);
426
-
427
- // fix ZipPath
428
- zipPath = zipPath ? fixPath(zipPath) : "";
429
-
430
- // normalize the path first
431
- localPath = pth.normalize(localPath);
432
-
433
- var self = this;
434
- filetools.fs.open(localPath, "r", function (err) {
435
- if (err && err.code === "ENOENT") {
436
- callback(undefined, Utils.Errors.FILE_NOT_FOUND(localPath));
437
- } else if (err) {
438
- callback(undefined, err);
439
- } else {
440
- var items = filetools.findFiles(localPath);
441
- var i = -1;
442
-
443
- var next = function () {
444
- i += 1;
445
- if (i < items.length) {
446
- var filepath = items[i];
447
- var p = relativePath(localPath, filepath).split("\\").join("/"); //windows fix
448
- p = p
449
- .normalize("NFD")
450
- .replace(/[\u0300-\u036f]/g, "")
451
- .replace(/[^\x20-\x7E]/g, ""); // accent fix
452
- if (filter(p)) {
453
- filetools.fs.stat(filepath, function (er0, stats) {
454
- if (er0) callback(undefined, er0);
455
- if (stats.isFile()) {
456
- filetools.fs.readFile(filepath, function (er1, data) {
457
- if (er1) {
458
- callback(undefined, er1);
459
- } else {
460
- self.addFile(zipPath + p, data, "", stats);
461
- next();
462
- }
463
- });
464
- } else {
465
- self.addFile(zipPath + p + "/", Buffer.alloc(0), "", stats);
466
- next();
467
- }
468
- });
469
- } else {
470
- process.nextTick(() => {
471
- next();
472
- });
473
- }
474
- } else {
475
- callback(true, undefined);
476
- }
477
- };
478
-
479
- next();
480
- }
481
- });
482
- },
483
-
484
- /**
485
- * Adds a local directory and all its nested files and directories to the archive
486
- *
487
- * @param {object | string} options - options object, if it is string it us used as localPath.
488
- * @param {string} options.localPath - Local path to the folder.
489
- * @param {string} [options.zipPath] - optional path inside zip.
490
- * @param {RegExp|function} [options.filter] - optional RegExp or Function if files match will be included.
491
- * @param {function|string} [options.namefix] - optional function to help fix filename
492
- * @param {doneCallback} callback - The callback that handles the response.
493
- *
494
- */
495
- addLocalFolderAsync2: function (options, callback) {
496
- const self = this;
497
- options = typeof options === "object" ? options : { localPath: options };
498
- localPath = pth.resolve(fixPath(options.localPath));
499
- let { zipPath, filter, namefix } = options;
500
-
501
- if (filter instanceof RegExp) {
502
- filter = (function (rx) {
503
- return function (filename) {
504
- return rx.test(filename);
505
- };
506
- })(filter);
507
- } else if ("function" !== typeof filter) {
508
- filter = function () {
509
- return true;
510
- };
511
- }
512
-
513
- // fix ZipPath
514
- zipPath = zipPath ? fixPath(zipPath) : "";
515
-
516
- // Check Namefix function
517
- if (namefix == "latin1") {
518
- namefix = (str) =>
519
- str
520
- .normalize("NFD")
521
- .replace(/[\u0300-\u036f]/g, "")
522
- .replace(/[^\x20-\x7E]/g, ""); // accent fix (latin1 characers only)
523
- }
524
-
525
- if (typeof namefix !== "function") namefix = (str) => str;
526
-
527
- // internal, create relative path + fix the name
528
- const relPathFix = (entry) => pth.join(zipPath, namefix(relativePath(localPath, entry)));
529
- const fileNameFix = (entry) => pth.win32.basename(pth.win32.normalize(namefix(entry)));
530
-
531
- filetools.fs.open(localPath, "r", function (err) {
532
- if (err && err.code === "ENOENT") {
533
- callback(undefined, Utils.Errors.FILE_NOT_FOUND(localPath));
534
- } else if (err) {
535
- callback(undefined, err);
536
- } else {
537
- filetools.findFilesAsync(localPath, function (err, fileEntries) {
538
- if (err) return callback(err);
539
- fileEntries = fileEntries.filter((dir) => filter(relPathFix(dir)));
540
- if (!fileEntries.length) callback(undefined, false);
541
-
542
- setImmediate(
543
- fileEntries.reverse().reduce(function (next, entry) {
544
- return function (err, done) {
545
- if (err || done === false) return setImmediate(next, err, false);
546
-
547
- self.addLocalFileAsync(
548
- {
549
- localPath: entry,
550
- zipPath: pth.dirname(relPathFix(entry)),
551
- zipName: fileNameFix(entry)
552
- },
553
- next
554
- );
555
- };
556
- }, callback)
557
- );
558
- });
559
- }
560
- });
561
- },
562
-
563
- /**
564
- * Adds a local directory and all its nested files and directories to the archive
565
- *
566
- * @param {string} localPath - path where files will be extracted
567
- * @param {object} props - optional properties
568
- * @param {string} [props.zipPath] - optional path inside zip
569
- * @param {RegExp|function} [props.filter] - optional RegExp or Function if files match will be included.
570
- * @param {function|string} [props.namefix] - optional function to help fix filename
571
- */
572
- addLocalFolderPromise: function (localPath, props) {
573
- return new Promise((resolve, reject) => {
574
- this.addLocalFolderAsync2(Object.assign({ localPath }, props), (err, done) => {
575
- if (err) reject(err);
576
- if (done) resolve(this);
577
- });
578
- });
579
- },
580
-
581
- /**
582
- * Allows you to create a entry (file or directory) in the zip file.
583
- * If you want to create a directory the entryName must end in / and a null buffer should be provided.
584
- * Comment and attributes are optional
585
- *
586
- * @param {string} entryName
587
- * @param {Buffer | string} content - file content as buffer or utf8 coded string
588
- * @param {string} [comment] - file comment
589
- * @param {number | object} [attr] - number as unix file permissions, object as filesystem Stats object
590
- */
591
- addFile: function (entryName, content, comment, attr) {
592
- entryName = zipnamefix(entryName);
593
- let entry = getEntry(entryName);
594
- const update = entry != null;
595
-
596
- // prepare new entry
597
- if (!update) {
598
- entry = new ZipEntry(opts);
599
- entry.entryName = entryName;
600
- }
601
- entry.comment = comment || "";
602
-
603
- const isStat = "object" === typeof attr && attr instanceof filetools.fs.Stats;
604
-
605
- // last modification time from file stats
606
- if (isStat) {
607
- entry.header.time = attr.mtime;
608
- }
609
-
610
- // Set file attribute
611
- var fileattr = entry.isDirectory ? 0x10 : 0; // (MS-DOS directory flag)
612
-
613
- // extended attributes field for Unix
614
- // set file type either S_IFDIR / S_IFREG
615
- let unix = entry.isDirectory ? 0x4000 : 0x8000;
616
-
617
- if (isStat) {
618
- // File attributes from file stats
619
- unix |= 0xfff & attr.mode;
620
- } else if ("number" === typeof attr) {
621
- // attr from given attr values
622
- unix |= 0xfff & attr;
623
- } else {
624
- // Default values:
625
- unix |= entry.isDirectory ? 0o755 : 0o644; // permissions (drwxr-xr-x) or (-r-wr--r--)
626
- }
627
-
628
- fileattr = (fileattr | (unix << 16)) >>> 0; // add attributes
629
-
630
- entry.attr = fileattr;
631
-
632
- entry.setData(content);
633
- if (!update) _zip.setEntry(entry);
634
-
635
- return entry;
636
- },
637
-
638
- /**
639
- * Returns an array of ZipEntry objects representing the files and folders inside the archive
640
- *
641
- * @param {string} [password]
642
- * @returns Array
643
- */
644
- getEntries: function (password) {
645
- _zip.password = password;
646
- return _zip ? _zip.entries : [];
647
- },
648
-
649
- /**
650
- * Returns a ZipEntry object representing the file or folder specified by ``name``.
651
- *
652
- * @param {string} name
653
- * @return ZipEntry
654
- */
655
- getEntry: function (/**String*/ name) {
656
- return getEntry(name);
657
- },
658
-
659
- getEntryCount: function () {
660
- return _zip.getEntryCount();
661
- },
662
-
663
- forEach: function (callback) {
664
- return _zip.forEach(callback);
665
- },
666
-
667
- /**
668
- * Extracts the given entry to the given targetPath
669
- * If the entry is a directory inside the archive, the entire directory and it's subdirectories will be extracted
670
- *
671
- * @param {string|ZipEntry} entry - ZipEntry object or String with the full path of the entry
672
- * @param {string} targetPath - Target folder where to write the file
673
- * @param {boolean} [maintainEntryPath=true] - If maintainEntryPath is true and the entry is inside a folder, the entry folder will be created in targetPath as well. Default is TRUE
674
- * @param {boolean} [overwrite=false] - If the file already exists at the target path, the file will be overwriten if this is true.
675
- * @param {boolean} [keepOriginalPermission=false] - The file will be set as the permission from the entry if this is true.
676
- * @param {string} [outFileName] - String If set will override the filename of the extracted file (Only works if the entry is a file)
677
- *
678
- * @return Boolean
679
- */
680
- extractEntryTo: function (entry, targetPath, maintainEntryPath, overwrite, keepOriginalPermission, outFileName) {
681
- overwrite = get_Bool(false, overwrite);
682
- keepOriginalPermission = get_Bool(false, keepOriginalPermission);
683
- maintainEntryPath = get_Bool(true, maintainEntryPath);
684
- outFileName = get_Str(keepOriginalPermission, outFileName);
685
-
686
- var item = getEntry(entry);
687
- if (!item) {
688
- throw Utils.Errors.NO_ENTRY();
689
- }
690
-
691
- var entryName = canonical(item.entryName);
692
-
693
- var target = sanitize(targetPath, outFileName && !item.isDirectory ? outFileName : maintainEntryPath ? entryName : pth.basename(entryName));
694
-
695
- if (item.isDirectory) {
696
- var children = _zip.getEntryChildren(item);
697
- children.forEach(function (child) {
698
- if (child.isDirectory) return;
699
- var content = child.getData();
700
- if (!content) {
701
- throw Utils.Errors.CANT_EXTRACT_FILE();
702
- }
703
- var name = canonical(child.entryName);
704
- var childName = sanitize(targetPath, maintainEntryPath ? name : pth.basename(name));
705
- // The reverse operation for attr depend on method addFile()
706
- const fileAttr = keepOriginalPermission ? child.header.fileAttr : undefined;
707
- filetools.writeFileTo(childName, content, overwrite, fileAttr);
708
- });
709
- return true;
710
- }
711
-
712
- var content = item.getData(_zip.password);
713
- if (!content) throw Utils.Errors.CANT_EXTRACT_FILE();
714
-
715
- if (filetools.fs.existsSync(target) && !overwrite) {
716
- throw Utils.Errors.CANT_OVERRIDE();
717
- }
718
- // The reverse operation for attr depend on method addFile()
719
- const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
720
- filetools.writeFileTo(target, content, overwrite, fileAttr);
721
-
722
- return true;
723
- },
724
-
725
- /**
726
- * Test the archive
727
- * @param {string} [pass]
728
- */
729
- test: function (pass) {
730
- if (!_zip) {
731
- return false;
732
- }
733
-
734
- for (var entry in _zip.entries) {
735
- try {
736
- if (entry.isDirectory) {
737
- continue;
738
- }
739
- var content = _zip.entries[entry].getData(pass);
740
- if (!content) {
741
- return false;
742
- }
743
- } catch (err) {
744
- return false;
745
- }
746
- }
747
- return true;
748
- },
749
-
750
- /**
751
- * Extracts the entire archive to the given location
752
- *
753
- * @param {string} targetPath Target location
754
- * @param {boolean} [overwrite=false] If the file already exists at the target path, the file will be overwriten if this is true.
755
- * Default is FALSE
756
- * @param {boolean} [keepOriginalPermission=false] The file will be set as the permission from the entry if this is true.
757
- * Default is FALSE
758
- * @param {string|Buffer} [pass] password
759
- */
760
- extractAllTo: function (targetPath, overwrite, keepOriginalPermission, pass) {
761
- keepOriginalPermission = get_Bool(false, keepOriginalPermission);
762
- pass = get_Str(keepOriginalPermission, pass);
763
- overwrite = get_Bool(false, overwrite);
764
- if (!_zip) throw Utils.Errors.NO_ZIP();
765
-
766
- _zip.entries.forEach(function (entry) {
767
- var entryName = sanitize(targetPath, canonical(entry.entryName));
768
- if (entry.isDirectory) {
769
- filetools.makeDir(entryName);
770
- return;
771
- }
772
- var content = entry.getData(pass);
773
- if (!content) {
774
- throw Utils.Errors.CANT_EXTRACT_FILE();
775
- }
776
- // The reverse operation for attr depend on method addFile()
777
- const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
778
- filetools.writeFileTo(entryName, content, overwrite, fileAttr);
779
- try {
780
- filetools.fs.utimesSync(entryName, entry.header.time, entry.header.time);
781
- } catch (err) {
782
- throw Utils.Errors.CANT_EXTRACT_FILE();
783
- }
784
- });
785
- },
786
-
787
- /**
788
- * Asynchronous extractAllTo
789
- *
790
- * @param {string} targetPath Target location
791
- * @param {boolean} [overwrite=false] If the file already exists at the target path, the file will be overwriten if this is true.
792
- * Default is FALSE
793
- * @param {boolean} [keepOriginalPermission=false] The file will be set as the permission from the entry if this is true.
794
- * Default is FALSE
795
- * @param {function} callback The callback will be executed when all entries are extracted successfully or any error is thrown.
796
- */
797
- extractAllToAsync: function (targetPath, overwrite, keepOriginalPermission, callback) {
798
- callback = get_Fun(overwrite, keepOriginalPermission, callback);
799
- keepOriginalPermission = get_Bool(false, keepOriginalPermission);
800
- overwrite = get_Bool(false, overwrite);
801
- if (!callback) {
802
- return new Promise((resolve, reject) => {
803
- this.extractAllToAsync(targetPath, overwrite, keepOriginalPermission, function (err) {
804
- if (err) {
805
- reject(err);
806
- } else {
807
- resolve(this);
808
- }
809
- });
810
- });
811
- }
812
- if (!_zip) {
813
- callback(Utils.Errors.NO_ZIP());
814
- return;
815
- }
816
-
817
- targetPath = pth.resolve(targetPath);
818
- // convert entryName to
819
- const getPath = (entry) => sanitize(targetPath, pth.normalize(canonical(entry.entryName)));
820
- const getError = (msg, file) => new Error(msg + ': "' + file + '"');
821
-
822
- // separate directories from files
823
- const dirEntries = [];
824
- const fileEntries = [];
825
- _zip.entries.forEach((e) => {
826
- if (e.isDirectory) {
827
- dirEntries.push(e);
828
- } else {
829
- fileEntries.push(e);
830
- }
831
- });
832
-
833
- // Create directory entries first synchronously
834
- // this prevents race condition and assures folders are there before writing files
835
- for (const entry of dirEntries) {
836
- const dirPath = getPath(entry);
837
- // The reverse operation for attr depend on method addFile()
838
- const dirAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
839
- try {
840
- filetools.makeDir(dirPath);
841
- if (dirAttr) filetools.fs.chmodSync(dirPath, dirAttr);
842
- // in unix timestamp will change if files are later added to folder, but still
843
- filetools.fs.utimesSync(dirPath, entry.header.time, entry.header.time);
844
- } catch (er) {
845
- callback(getError("Unable to create folder", dirPath));
846
- }
847
- }
848
-
849
- fileEntries.reverse().reduce(function (next, entry) {
850
- return function (err) {
851
- if (err) {
852
- next(err);
853
- } else {
854
- const entryName = pth.normalize(canonical(entry.entryName));
855
- const filePath = sanitize(targetPath, entryName);
856
- entry.getDataAsync(function (content, err_1) {
857
- if (err_1) {
858
- next(err_1);
859
- } else if (!content) {
860
- next(Utils.Errors.CANT_EXTRACT_FILE());
861
- } else {
862
- // The reverse operation for attr depend on method addFile()
863
- const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
864
- filetools.writeFileToAsync(filePath, content, overwrite, fileAttr, function (succ) {
865
- if (!succ) {
866
- next(getError("Unable to write file", filePath));
867
- }
868
- filetools.fs.utimes(filePath, entry.header.time, entry.header.time, function (err_2) {
869
- if (err_2) {
870
- next(getError("Unable to set times", filePath));
871
- } else {
872
- next();
873
- }
874
- });
875
- });
876
- }
877
- });
878
- }
879
- };
880
- }, callback)();
881
- },
882
-
883
- /**
884
- * Writes the newly created zip file to disk at the specified location or if a zip was opened and no ``targetFileName`` is provided, it will overwrite the opened zip
885
- *
886
- * @param {string} targetFileName
887
- * @param {function} callback
888
- */
889
- writeZip: function (targetFileName, callback) {
890
- if (arguments.length === 1) {
891
- if (typeof targetFileName === "function") {
892
- callback = targetFileName;
893
- targetFileName = "";
894
- }
895
- }
896
-
897
- if (!targetFileName && opts.filename) {
898
- targetFileName = opts.filename;
899
- }
900
- if (!targetFileName) return;
901
-
902
- var zipData = _zip.compressToBuffer();
903
- if (zipData) {
904
- var ok = filetools.writeFileTo(targetFileName, zipData, true);
905
- if (typeof callback === "function") callback(!ok ? new Error("failed") : null, "");
906
- }
907
- },
908
-
909
- /**
910
- *
911
- * @param {string} targetFileName
912
- * @param {object} [props]
913
- * @param {boolean} [props.overwrite=true] If the file already exists at the target path, the file will be overwriten if this is true.
914
- * @param {boolean} [props.perm] The file will be set as the permission from the entry if this is true.
915
-
916
- * @returns {Promise<void>}
917
- */
918
- writeZipPromise: function (/**String*/ targetFileName, /* object */ props) {
919
- const { overwrite, perm } = Object.assign({ overwrite: true }, props);
920
-
921
- return new Promise((resolve, reject) => {
922
- // find file name
923
- if (!targetFileName && opts.filename) targetFileName = opts.filename;
924
- if (!targetFileName) reject("ADM-ZIP: ZIP File Name Missing");
925
-
926
- this.toBufferPromise().then((zipData) => {
927
- const ret = (done) => (done ? resolve(done) : reject("ADM-ZIP: Wasn't able to write zip file"));
928
- filetools.writeFileToAsync(targetFileName, zipData, overwrite, perm, ret);
929
- }, reject);
930
- });
931
- },
932
-
933
- /**
934
- * @returns {Promise<Buffer>} A promise to the Buffer.
935
- */
936
- toBufferPromise: function () {
937
- return new Promise((resolve, reject) => {
938
- _zip.toAsyncBuffer(resolve, reject);
939
- });
940
- },
941
-
942
- /**
943
- * Returns the content of the entire zip file as a Buffer object
944
- *
945
- * @prop {function} [onSuccess]
946
- * @prop {function} [onFail]
947
- * @prop {function} [onItemStart]
948
- * @prop {function} [onItemEnd]
949
- * @returns {Buffer}
950
- */
951
- toBuffer: function (onSuccess, onFail, onItemStart, onItemEnd) {
952
- if (typeof onSuccess === "function") {
953
- _zip.toAsyncBuffer(onSuccess, onFail, onItemStart, onItemEnd);
954
- return null;
955
- }
956
- return _zip.compressToBuffer();
957
- }
958
- };
959
- };