zip-lib 1.0.4 → 1.0.5
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.
- package/README.md +14 -0
- package/lib/fs.js +6 -7
- package/lib/index.js +3 -4
- package/lib/util.js +11 -12
- package/lib/zip.d.ts +15 -0
- package/lib/zip.js +24 -13
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -16,6 +16,7 @@ npm install zip-lib
|
|
|
16
16
|
- [Zip single folder](#zip-single-folder)
|
|
17
17
|
* [Unzip](#unzip)
|
|
18
18
|
* [Advanced usage](#advance-usage)
|
|
19
|
+
- [Sets the compression level](#sets-the-compression-level)
|
|
19
20
|
- [Zip multiple files and folders](#zip-multiple-files-and-folders)
|
|
20
21
|
- [Zip with metadata](#zip-with-metadata)
|
|
21
22
|
- [Unzip with entry callback](#unzip-with-entry-callback)
|
|
@@ -72,6 +73,18 @@ zl.extract("path/to/target.zip", "path/to/target").then(function () {
|
|
|
72
73
|
|
|
73
74
|
## Advanced usage
|
|
74
75
|
|
|
76
|
+
### Sets the compression level
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
const zl = require("zip-lib");
|
|
80
|
+
|
|
81
|
+
zl.archiveFolder("path/to/folder", "path/to/target.zip", { compressionLevel: 9 }).then(function () {
|
|
82
|
+
console.log("done");
|
|
83
|
+
}, function (err) {
|
|
84
|
+
console.log(err);
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
75
88
|
### Zip multiple files and folders
|
|
76
89
|
|
|
77
90
|
```js
|
|
@@ -345,6 +358,7 @@ Returns: `void`
|
|
|
345
358
|
|
|
346
359
|
Object
|
|
347
360
|
- `followSymlinks?`: Boolean (optional) - Indicates how to handle when the given path is a symbolic link. The default value is `false`.<br>`true`: add the target of the symbolic link to the zip.<br>`false`: add symbolic link itself to the zip.
|
|
361
|
+
- `compressionLevel?`: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 - Sets the compression level. The default value is `6`.<br>`0`: the file data will be stored.<br>`1-9`: the file data will be deflated.
|
|
348
362
|
|
|
349
363
|
### Options: IExtractOptions <a id="iextractoptions"></a>
|
|
350
364
|
|
package/lib/fs.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.readdirp = readdirp;
|
|
4
|
+
exports.getFileEntry = getFileEntry;
|
|
5
|
+
exports.ensureFolder = ensureFolder;
|
|
6
|
+
exports.pathExists = pathExists;
|
|
7
|
+
exports.rimraf = rimraf;
|
|
8
|
+
exports.isRootPath = isRootPath;
|
|
4
9
|
const path = require("path");
|
|
5
10
|
const util = require("./util");
|
|
6
11
|
async function readdirp(folder) {
|
|
@@ -22,7 +27,6 @@ async function readdirp(folder) {
|
|
|
22
27
|
}
|
|
23
28
|
return result;
|
|
24
29
|
}
|
|
25
|
-
exports.readdirp = readdirp;
|
|
26
30
|
async function getFileEntry(target) {
|
|
27
31
|
const stat = await util.lstat(target);
|
|
28
32
|
let isSymbolicLink = false;
|
|
@@ -50,7 +54,6 @@ async function getFileEntry(target) {
|
|
|
50
54
|
mode: stat.mode
|
|
51
55
|
};
|
|
52
56
|
}
|
|
53
|
-
exports.getFileEntry = getFileEntry;
|
|
54
57
|
async function ensureFolder(folder) {
|
|
55
58
|
// stop at root
|
|
56
59
|
if (folder === path.dirname(folder)) {
|
|
@@ -70,7 +73,6 @@ async function ensureFolder(folder) {
|
|
|
70
73
|
return Promise.reject(error);
|
|
71
74
|
}
|
|
72
75
|
}
|
|
73
|
-
exports.ensureFolder = ensureFolder;
|
|
74
76
|
async function pathExists(target) {
|
|
75
77
|
try {
|
|
76
78
|
await util.access(target);
|
|
@@ -80,7 +82,6 @@ async function pathExists(target) {
|
|
|
80
82
|
return false;
|
|
81
83
|
}
|
|
82
84
|
}
|
|
83
|
-
exports.pathExists = pathExists;
|
|
84
85
|
async function rimraf(target) {
|
|
85
86
|
if (isRootPath(target)) {
|
|
86
87
|
// refuse to recursively delete root
|
|
@@ -112,7 +113,6 @@ async function rimraf(target) {
|
|
|
112
113
|
}
|
|
113
114
|
}
|
|
114
115
|
}
|
|
115
|
-
exports.rimraf = rimraf;
|
|
116
116
|
async function mkdir(folder) {
|
|
117
117
|
try {
|
|
118
118
|
await util.mkdir(folder, 0o777);
|
|
@@ -172,4 +172,3 @@ function isRootPath(target) {
|
|
|
172
172
|
}
|
|
173
173
|
return false;
|
|
174
174
|
}
|
|
175
|
-
exports.isRootPath = isRootPath;
|
package/lib/index.js
CHANGED
|
@@ -14,7 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.archiveFile = archiveFile;
|
|
18
|
+
exports.archiveFolder = archiveFolder;
|
|
19
|
+
exports.extract = extract;
|
|
18
20
|
const zip_1 = require("./zip");
|
|
19
21
|
const unzip_1 = require("./unzip");
|
|
20
22
|
__exportStar(require("./zip"), exports);
|
|
@@ -30,7 +32,6 @@ function archiveFile(file, zipFile, options) {
|
|
|
30
32
|
zip.addFile(file);
|
|
31
33
|
return zip.archive(zipFile);
|
|
32
34
|
}
|
|
33
|
-
exports.archiveFile = archiveFile;
|
|
34
35
|
/**
|
|
35
36
|
* Compress all the contents of the specified folder to zip.
|
|
36
37
|
* @param folder
|
|
@@ -42,7 +43,6 @@ function archiveFolder(folder, zipFile, options) {
|
|
|
42
43
|
zip.addFolder(folder);
|
|
43
44
|
return zip.archive(zipFile);
|
|
44
45
|
}
|
|
45
|
-
exports.archiveFolder = archiveFolder;
|
|
46
46
|
/**
|
|
47
47
|
* Extract the zip file to the specified location.
|
|
48
48
|
* @param zipFile
|
|
@@ -53,4 +53,3 @@ function extract(zipFile, targetFolder, options) {
|
|
|
53
53
|
const unzip = new unzip_1.Unzip(options);
|
|
54
54
|
return unzip.extract(zipFile, targetFolder);
|
|
55
55
|
}
|
|
56
|
-
exports.extract = extract;
|
package/lib/util.js
CHANGED
|
@@ -1,49 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.unlink = unlink;
|
|
4
|
+
exports.mkdir = mkdir;
|
|
5
|
+
exports.realpath = realpath;
|
|
6
|
+
exports.stat = stat;
|
|
7
|
+
exports.lstat = lstat;
|
|
8
|
+
exports.chmod = chmod;
|
|
9
|
+
exports.readdir = readdir;
|
|
10
|
+
exports.access = access;
|
|
11
|
+
exports.rmdir = rmdir;
|
|
12
|
+
exports.symlink = symlink;
|
|
13
|
+
exports.readlink = readlink;
|
|
4
14
|
const fs = require("fs");
|
|
5
15
|
const util = require("util");
|
|
6
16
|
function unlink(path) {
|
|
7
17
|
return util.promisify(fs.unlink)(path);
|
|
8
18
|
}
|
|
9
|
-
exports.unlink = unlink;
|
|
10
19
|
function mkdir(path, mode) {
|
|
11
20
|
return util.promisify(fs.mkdir)(path, mode);
|
|
12
21
|
}
|
|
13
|
-
exports.mkdir = mkdir;
|
|
14
22
|
function realpath(path) {
|
|
15
23
|
return util.promisify(fs.realpath)(path);
|
|
16
24
|
}
|
|
17
|
-
exports.realpath = realpath;
|
|
18
25
|
function stat(path) {
|
|
19
26
|
return util.promisify(fs.stat)(path);
|
|
20
27
|
}
|
|
21
|
-
exports.stat = stat;
|
|
22
28
|
function lstat(path) {
|
|
23
29
|
return util.promisify(fs.lstat)(path);
|
|
24
30
|
}
|
|
25
|
-
exports.lstat = lstat;
|
|
26
31
|
function chmod(path, mode) {
|
|
27
32
|
return util.promisify(fs.chmod)(path, mode);
|
|
28
33
|
}
|
|
29
|
-
exports.chmod = chmod;
|
|
30
34
|
function readdir(path) {
|
|
31
35
|
return util.promisify(fs.readdir)(path);
|
|
32
36
|
}
|
|
33
|
-
exports.readdir = readdir;
|
|
34
37
|
function access(path, mode) {
|
|
35
38
|
return util.promisify(fs.access)(path, mode);
|
|
36
39
|
}
|
|
37
|
-
exports.access = access;
|
|
38
40
|
function rmdir(path) {
|
|
39
41
|
return util.promisify(fs.rmdir)(path);
|
|
40
42
|
}
|
|
41
|
-
exports.rmdir = rmdir;
|
|
42
43
|
function symlink(target, path, type) {
|
|
43
44
|
return util.promisify(fs.symlink)(target, path, type);
|
|
44
45
|
}
|
|
45
|
-
exports.symlink = symlink;
|
|
46
46
|
function readlink(path) {
|
|
47
47
|
return util.promisify(fs.readlink)(path);
|
|
48
48
|
}
|
|
49
|
-
exports.readlink = readlink;
|
package/lib/zip.d.ts
CHANGED
|
@@ -10,6 +10,14 @@ export interface IZipOptions {
|
|
|
10
10
|
* The default value is `false`.
|
|
11
11
|
*/
|
|
12
12
|
followSymlinks?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Sets the compression level.
|
|
15
|
+
*
|
|
16
|
+
* 0: the file data will be stored, otherwise, the file data will be deflated.
|
|
17
|
+
*
|
|
18
|
+
* The default value is `6`.
|
|
19
|
+
*/
|
|
20
|
+
compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
13
21
|
}
|
|
14
22
|
/**
|
|
15
23
|
* Compress files or folders to a zip file.
|
|
@@ -56,4 +64,11 @@ export declare class Zip extends Cancelable {
|
|
|
56
64
|
private walkDir;
|
|
57
65
|
private stopPipe;
|
|
58
66
|
private followSymlink;
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves the yazl options based on the current settings.
|
|
69
|
+
*
|
|
70
|
+
* @returns The yazl options with the specified compression level,
|
|
71
|
+
* or undefined if options or compressionLevel are not properly set.
|
|
72
|
+
*/
|
|
73
|
+
private getYazlOption;
|
|
59
74
|
}
|
package/lib/zip.js
CHANGED
|
@@ -28,13 +28,13 @@ class Zip extends cancelable_1.Cancelable {
|
|
|
28
28
|
* A valid metadataPath must not start with "/" or /[A-Za-z]:\//, and must not contain "..".
|
|
29
29
|
*/
|
|
30
30
|
addFile(file, metadataPath) {
|
|
31
|
-
let
|
|
32
|
-
if (!
|
|
33
|
-
|
|
31
|
+
let mPath = metadataPath;
|
|
32
|
+
if (!mPath) {
|
|
33
|
+
mPath = path.basename(file);
|
|
34
34
|
}
|
|
35
35
|
this.zipFiles.push({
|
|
36
36
|
path: file,
|
|
37
|
-
metadataPath:
|
|
37
|
+
metadataPath: mPath
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
@@ -124,7 +124,7 @@ class Zip extends cancelable_1.Cancelable {
|
|
|
124
124
|
await this.walkDir([{ path: realPath, metadataPath: file.metadataPath }], token);
|
|
125
125
|
}
|
|
126
126
|
else {
|
|
127
|
-
zip.addFile(file.path, file.metadataPath);
|
|
127
|
+
zip.addFile(file.path, file.metadataPath, this.getYazlOption());
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
else {
|
|
@@ -156,18 +156,12 @@ class Zip extends cancelable_1.Cancelable {
|
|
|
156
156
|
});
|
|
157
157
|
// If the file attribute is known, add the entry using `addReadStream`,
|
|
158
158
|
// this can reduce the number of calls to the `fs.stat` method.
|
|
159
|
-
zip.addReadStream(fileStream, metadataPath, {
|
|
160
|
-
mode: file.mode,
|
|
161
|
-
mtime: file.mtime
|
|
162
|
-
});
|
|
159
|
+
zip.addReadStream(fileStream, metadataPath, Object.assign(Object.assign({}, this.getYazlOption()), { mode: file.mode, mtime: file.mtime }));
|
|
163
160
|
});
|
|
164
161
|
}
|
|
165
162
|
async addSymlink(zip, file, metadataPath) {
|
|
166
163
|
const linkTarget = await util.readlink(file.path);
|
|
167
|
-
zip.addBuffer(Buffer.from(linkTarget), metadataPath, {
|
|
168
|
-
mtime: file.mtime,
|
|
169
|
-
mode: file.mode
|
|
170
|
-
});
|
|
164
|
+
zip.addBuffer(Buffer.from(linkTarget), metadataPath, Object.assign(Object.assign({}, this.getYazlOption()), { mtime: file.mtime, mode: file.mode }));
|
|
171
165
|
}
|
|
172
166
|
async walkDir(folders, token) {
|
|
173
167
|
for (const folder of folders) {
|
|
@@ -209,5 +203,22 @@ class Zip extends cancelable_1.Cancelable {
|
|
|
209
203
|
}
|
|
210
204
|
return followSymlink;
|
|
211
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Retrieves the yazl options based on the current settings.
|
|
208
|
+
*
|
|
209
|
+
* @returns The yazl options with the specified compression level,
|
|
210
|
+
* or undefined if options or compressionLevel are not properly set.
|
|
211
|
+
*/
|
|
212
|
+
getYazlOption() {
|
|
213
|
+
if (!this.options) {
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
if (typeof this.options.compressionLevel !== "number") {
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
compressionLevel: this.options.compressionLevel,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
212
223
|
}
|
|
213
224
|
exports.Zip = Zip;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zip-lib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "zip and unzip library for node",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -30,16 +30,16 @@
|
|
|
30
30
|
"author": "fpsqdb",
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"yauzl": "^3.
|
|
34
|
-
"yazl": "^2.
|
|
33
|
+
"yauzl": "^3.2.0",
|
|
34
|
+
"yazl": "^3.2.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@types/mocha": "^10.0.
|
|
37
|
+
"@types/mocha": "^10.0.9",
|
|
38
38
|
"@types/node": "^12.20.55",
|
|
39
|
-
"@types/yauzl": "^2.
|
|
40
|
-
"@types/yazl": "^2.4.
|
|
41
|
-
"mocha": "^10.
|
|
42
|
-
"rimraf": "^
|
|
43
|
-
"typescript": "^5.
|
|
39
|
+
"@types/yauzl": "^2.10.3",
|
|
40
|
+
"@types/yazl": "^2.4.5",
|
|
41
|
+
"mocha": "^10.8.2",
|
|
42
|
+
"rimraf": "^6.0.1",
|
|
43
|
+
"typescript": "^5.6.3"
|
|
44
44
|
}
|
|
45
45
|
}
|