zip-lib 1.2.3 → 1.3.0
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 +295 -204
- package/lib/fs.d.ts +1 -0
- package/lib/fs.js +74 -61
- package/lib/index.d.ts +21 -2
- package/lib/index.js +27 -24
- package/lib/unzip.d.ts +22 -10
- package/lib/unzip.js +189 -129
- package/lib/zip.d.ts +25 -10
- package/lib/zip.js +143 -60
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# zip-lib
|
|
2
|
-
zip and unzip library for
|
|
2
|
+
A zip and unzip library for Node.js.
|
|
3
3
|
|
|
4
4
|
[](https://www.npmjs.org/package/zip-lib)
|
|
5
5
|
[](https://github.com/fpsqdb/zip-lib/blob/master/LICENSE)
|
|
@@ -34,76 +34,121 @@ npm install zip-lib
|
|
|
34
34
|
- Class: [Unzip](#class-unzip)
|
|
35
35
|
- Options: [IZipOptions](#izipoptions)
|
|
36
36
|
- Options: [IExtractOptions](#iextractoptions)
|
|
37
|
+
- Event: [IEntryEvent](#event-ientryevent)
|
|
37
38
|
|
|
38
39
|
## Zip
|
|
39
40
|
You can use **zip-lib** to compress files or folders.
|
|
40
41
|
|
|
41
42
|
### Zip single file
|
|
42
43
|
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
})
|
|
44
|
+
```ts
|
|
45
|
+
import * as zl from "zip-lib";
|
|
46
|
+
|
|
47
|
+
async function zipToFile() {
|
|
48
|
+
try {
|
|
49
|
+
await zl.archiveFile("path/to/file.txt", "path/to/target.zip");
|
|
50
|
+
console.log("done");
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error(error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function zipToBuffer() {
|
|
57
|
+
try {
|
|
58
|
+
const buffer = await zl.archiveFile("path/to/file.txt");
|
|
59
|
+
console.log("done", buffer.byteLength);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.error(error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
51
64
|
```
|
|
52
65
|
|
|
53
66
|
### Zip single folder
|
|
54
67
|
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
})
|
|
68
|
+
```ts
|
|
69
|
+
import * as zl from "zip-lib";
|
|
70
|
+
|
|
71
|
+
async function zipToFile() {
|
|
72
|
+
try {
|
|
73
|
+
await zl.archiveFolder("path/to/folder", "path/to/target.zip");
|
|
74
|
+
console.log("done");
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error(error);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function zipToBuffer() {
|
|
81
|
+
try {
|
|
82
|
+
const buffer = await zl.archiveFolder("path/to/folder");
|
|
83
|
+
console.log("done", buffer.byteLength);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error(error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
63
88
|
```
|
|
64
89
|
|
|
65
90
|
## Unzip
|
|
66
91
|
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
})
|
|
92
|
+
```ts
|
|
93
|
+
import * as zl from "zip-lib";
|
|
94
|
+
|
|
95
|
+
async function unzipFromFile() {
|
|
96
|
+
try {
|
|
97
|
+
await zl.extract("path/to/target.zip", "path/to/target");
|
|
98
|
+
console.log("done");
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error(error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function unzipFromBuffer(zipBuffer: Buffer) {
|
|
105
|
+
try {
|
|
106
|
+
await zl.extract(zipBuffer, "path/to/target");
|
|
107
|
+
console.log("done");
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.error(error);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
75
112
|
```
|
|
76
113
|
|
|
77
114
|
## Advanced usage
|
|
78
115
|
|
|
79
|
-
###
|
|
80
|
-
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
});
|
|
116
|
+
### Set the compression level
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import * as zl from "zip-lib";
|
|
120
|
+
|
|
121
|
+
async function zipToFile() {
|
|
122
|
+
try {
|
|
123
|
+
await zl.archiveFolder("path/to/folder", "path/to/target.zip", {
|
|
124
|
+
compressionLevel: 9,
|
|
125
|
+
});
|
|
126
|
+
console.log("done");
|
|
127
|
+
} catch (error) {
|
|
128
|
+
console.error(error);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
89
131
|
```
|
|
90
132
|
|
|
91
133
|
### Zip multiple files and folders
|
|
92
134
|
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
zip.
|
|
99
|
-
// Adds a
|
|
100
|
-
zip.
|
|
101
|
-
//
|
|
102
|
-
zip.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
})
|
|
135
|
+
```ts
|
|
136
|
+
import * as zl from "zip-lib";
|
|
137
|
+
|
|
138
|
+
async function zipToFile() {
|
|
139
|
+
try {
|
|
140
|
+
const zip = new zl.Zip();
|
|
141
|
+
// Adds a file from the file system
|
|
142
|
+
zip.addFile("path/to/file.txt");
|
|
143
|
+
// Adds a folder from the file system, putting its contents at the root of the archive
|
|
144
|
+
zip.addFolder("path/to/folder");
|
|
145
|
+
// Generate the zip file.
|
|
146
|
+
await zip.archive("path/to/target.zip");
|
|
147
|
+
console.log("done");
|
|
148
|
+
} catch (error) {
|
|
149
|
+
console.error(error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
107
152
|
```
|
|
108
153
|
|
|
109
154
|
The `path/to/folder` directory is as follows:
|
|
@@ -117,7 +162,7 @@ path/to/folder
|
|
|
117
162
|
└── file_in_root.ext
|
|
118
163
|
```
|
|
119
164
|
|
|
120
|
-
And the generated `path/to/target.zip` archive
|
|
165
|
+
And the contents of the generated `path/to/target.zip` archive will be as follows:
|
|
121
166
|
|
|
122
167
|
```
|
|
123
168
|
path/to/target.zip
|
|
@@ -131,21 +176,24 @@ path/to/target.zip
|
|
|
131
176
|
|
|
132
177
|
### Zip with metadata
|
|
133
178
|
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
zip.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
zip.
|
|
143
|
-
//
|
|
144
|
-
zip.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
})
|
|
179
|
+
```ts
|
|
180
|
+
import * as zl from "zip-lib";
|
|
181
|
+
|
|
182
|
+
async function zipToFile() {
|
|
183
|
+
try {
|
|
184
|
+
const zip = new zl.Zip();
|
|
185
|
+
// Adds a file from the file system
|
|
186
|
+
zip.addFile("path/to/file.txt", "renamedFile.txt");
|
|
187
|
+
zip.addFile("path/to/file2.txt", "folder/file.txt");
|
|
188
|
+
// Adds a folder from the file system and names it `new folder` within the archive
|
|
189
|
+
zip.addFolder("path/to/folder", "new folder");
|
|
190
|
+
// Generate the zip file.
|
|
191
|
+
zip.archive("path/to/target.zip");
|
|
192
|
+
console.log("done");
|
|
193
|
+
} catch (error) {
|
|
194
|
+
console.error(error);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
149
197
|
```
|
|
150
198
|
|
|
151
199
|
The `path/to/folder` directory is as follows:
|
|
@@ -159,7 +207,7 @@ path/to/folder
|
|
|
159
207
|
└── file_in_root.ext
|
|
160
208
|
```
|
|
161
209
|
|
|
162
|
-
And the generated `path/to/target.zip` archive
|
|
210
|
+
And the contents of the generated `path/to/target.zip` archive will be as follows:
|
|
163
211
|
|
|
164
212
|
```
|
|
165
213
|
path/to/target.zip
|
|
@@ -167,7 +215,7 @@ path/to/target.zip
|
|
|
167
215
|
├── renamedFile.txt
|
|
168
216
|
├── folder
|
|
169
217
|
│ ├── file.txt
|
|
170
|
-
|
|
218
|
+
├── new folder
|
|
171
219
|
├── dir1
|
|
172
220
|
│ ├── file.ext
|
|
173
221
|
├── dir2
|
|
@@ -175,208 +223,251 @@ path/to/target.zip
|
|
|
175
223
|
```
|
|
176
224
|
|
|
177
225
|
### Unzip with entry callback
|
|
178
|
-
Using `onEntry` callback we can
|
|
179
|
-
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
})
|
|
226
|
+
Using the `onEntry` callback, we can track extraction progress and control the extraction process. See [IExtractOptions](#iextractoptions).
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
import * as zl from "zip-lib";
|
|
230
|
+
|
|
231
|
+
async function unzipFromFile() {
|
|
232
|
+
try {
|
|
233
|
+
const unzip = new zl.Unzip({
|
|
234
|
+
// Called before an item is extracted.
|
|
235
|
+
onEntry: (event) => {
|
|
236
|
+
console.log(event.entryCount, event.entryName);
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
await unzip.extract("path/to/target.zip", "path/to/target");
|
|
240
|
+
console.log("done");
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.error(error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
194
245
|
```
|
|
195
246
|
|
|
196
247
|
### Unzip and exclude specified entries
|
|
197
248
|
The following code shows how to exclude the `__MACOSX` folder in the zip file when extracting. See [IExtractOptions](#iextractoptions).
|
|
198
249
|
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
})
|
|
250
|
+
```ts
|
|
251
|
+
import * as zl from "zip-lib";
|
|
252
|
+
|
|
253
|
+
async function unzipFromFile() {
|
|
254
|
+
try {
|
|
255
|
+
const unzip = new zl.Unzip({
|
|
256
|
+
// Called before an item is extracted.
|
|
257
|
+
onEntry: (event) => {
|
|
258
|
+
if (/^__MACOSX\//.test(event.entryName)) {
|
|
259
|
+
// entry name starts with __MACOSX/
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
await unzip.extract("path/to/target.zip", "path/to/target");
|
|
265
|
+
console.log("done");
|
|
266
|
+
} catch (error) {
|
|
267
|
+
console.error(error);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
216
270
|
```
|
|
217
271
|
|
|
218
272
|
### Cancel zip
|
|
219
273
|
If the `cancel` method is called after the archive is complete, nothing will happen.
|
|
220
274
|
|
|
221
|
-
```
|
|
222
|
-
|
|
275
|
+
```ts
|
|
276
|
+
import * as zl from "zip-lib";
|
|
223
277
|
|
|
224
278
|
const zip = new zl.Zip();
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
279
|
+
async function zipToFile() {
|
|
280
|
+
try {
|
|
281
|
+
zip.addFile("path/to/file.txt");
|
|
282
|
+
await zip.archive("path/to/target.zip");
|
|
283
|
+
console.log("done");
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (error instanceof Error && error.name === "Canceled") {
|
|
286
|
+
console.log("cancel");
|
|
287
|
+
} else {
|
|
288
|
+
console.log(error);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function cancel() {
|
|
294
|
+
zip.cancel();
|
|
295
|
+
}
|
|
238
296
|
```
|
|
239
297
|
|
|
240
298
|
### Cancel unzip
|
|
241
299
|
If the `cancel` method is called after the extract is complete, nothing will happen.
|
|
242
300
|
|
|
243
|
-
```
|
|
244
|
-
|
|
301
|
+
```ts
|
|
302
|
+
import * as zl from "zip-lib";
|
|
245
303
|
|
|
246
304
|
const unzip = new zl.Unzip();
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
305
|
+
async function unzipFromFile() {
|
|
306
|
+
try {
|
|
307
|
+
await unzip.extract("path/to/target.zip", "path/to/target");
|
|
308
|
+
console.log("done");
|
|
309
|
+
} catch (error) {
|
|
310
|
+
if (error instanceof Error && error.name === "Canceled") {
|
|
311
|
+
console.log("cancel");
|
|
312
|
+
} else {
|
|
313
|
+
console.log(error);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function cancel() {
|
|
319
|
+
unzip.cancel();
|
|
320
|
+
}
|
|
259
321
|
```
|
|
260
322
|
|
|
261
323
|
## API
|
|
262
324
|
|
|
325
|
+
Choose the API based on how much control you need:
|
|
326
|
+
|
|
327
|
+
- Use [`archiveFile`](#archivefile), [`archiveFolder`](#archivefolder), and [`extract`](#extract) for simple one-shot operations.
|
|
328
|
+
- Use [`Zip`](#class-zip) when you need to add multiple files or folders, rename entries, or cancel compression.
|
|
329
|
+
- Use [`Unzip`](#class-unzip) when you need to filter entries with `onEntry` or cancel extraction.
|
|
330
|
+
|
|
263
331
|
### Method: archiveFile <a id="archivefile"></a>
|
|
264
332
|
|
|
265
|
-
|
|
333
|
+
Compress a single file.
|
|
266
334
|
|
|
267
|
-
|
|
335
|
+
```ts
|
|
336
|
+
archiveFile(file: string, options?: IZipOptions): Promise<Buffer>
|
|
337
|
+
archiveFile(file: string, zipFile: string, options?: IZipOptions): Promise<void>
|
|
338
|
+
```
|
|
268
339
|
|
|
269
|
-
- `file`:
|
|
270
|
-
- `zipFile`:
|
|
271
|
-
- `options
|
|
340
|
+
- `file`: Path to the source file.
|
|
341
|
+
- `zipFile`: Optional output zip file path.
|
|
342
|
+
- `options`: Optional [IZipOptions](#izipoptions).
|
|
272
343
|
|
|
273
|
-
|
|
344
|
+
If `zipFile` is provided, the archive is written to disk and the method returns `Promise<void>`. Otherwise it returns the generated zip as a `Buffer`.
|
|
274
345
|
|
|
275
346
|
### Method: archiveFolder <a id="archivefolder"></a>
|
|
276
347
|
|
|
277
|
-
|
|
348
|
+
Compress all contents of a folder.
|
|
278
349
|
|
|
279
|
-
|
|
350
|
+
```ts
|
|
351
|
+
archiveFolder(folder: string, options?: IZipOptions): Promise<Buffer>
|
|
352
|
+
archiveFolder(folder: string, zipFile: string, options?: IZipOptions): Promise<void>
|
|
353
|
+
```
|
|
280
354
|
|
|
281
|
-
- `folder`:
|
|
282
|
-
- `zipFile`:
|
|
283
|
-
- `options
|
|
355
|
+
- `folder`: Path to the source folder.
|
|
356
|
+
- `zipFile`: Optional output zip file path.
|
|
357
|
+
- `options`: Optional [IZipOptions](#izipoptions).
|
|
284
358
|
|
|
285
|
-
|
|
359
|
+
If `zipFile` is provided, the archive is written to disk and the method returns `Promise<void>`. Otherwise it returns the generated zip as a `Buffer`.
|
|
286
360
|
|
|
287
361
|
### Method: extract <a id="extract"></a>
|
|
288
362
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
Extract the zip file to the specified location.
|
|
292
|
-
|
|
293
|
-
- `zipFile`: String
|
|
294
|
-
- `targetFolder`: String
|
|
295
|
-
- `options?`: [IExtractOptions](#iextractoptions) (optional)
|
|
296
|
-
|
|
297
|
-
Returns: `Promise<void>`
|
|
298
|
-
|
|
299
|
-
### Class: Zip<a id="class-zip"></a>
|
|
300
|
-
Compress files or folders to a zip file.
|
|
301
|
-
|
|
302
|
-
**Constructor: new Zip([options])**
|
|
363
|
+
Extract a zip file or zip buffer to a target folder.
|
|
303
364
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
Adds a file from the file system at realPath into the zipfile as metadataPath.
|
|
309
|
-
|
|
310
|
-
- `file`: String
|
|
311
|
-
- `metadataPath?`: String (optional) - Typically metadataPath would be calculated as path.relative(root, realPath). A valid metadataPath must not start with `/` or `/[A-Za-z]:\//`, and must not contain `..`.
|
|
312
|
-
|
|
313
|
-
Returns: `void`
|
|
365
|
+
```ts
|
|
366
|
+
extract(zipFile: string, targetFolder: string, options?: IExtractOptions): Promise<void>
|
|
367
|
+
extract(zipBuffer: Buffer, targetFolder: string, options?: IExtractOptions): Promise<void>
|
|
368
|
+
```
|
|
314
369
|
|
|
315
|
-
|
|
370
|
+
- `zipFile`: Path to a zip file on disk.
|
|
371
|
+
- `zipBuffer`: A zip archive in memory.
|
|
372
|
+
- `targetFolder`: Destination folder.
|
|
373
|
+
- `options`: Optional [IExtractOptions](#iextractoptions).
|
|
316
374
|
|
|
317
|
-
|
|
375
|
+
### Class: Zip <a id="class-zip"></a>
|
|
318
376
|
|
|
319
|
-
-
|
|
320
|
-
- `metadataPath?`: String (optional) - Typically metadataPath would be calculated as path.relative(root, realPath). A valid metadataPath must not start with `/` or `/[A-Za-z]:\//`, and must not contain `..`.
|
|
377
|
+
Build an archive incrementally when the top-level helpers are not enough.
|
|
321
378
|
|
|
322
|
-
|
|
379
|
+
**Constructor**
|
|
323
380
|
|
|
324
|
-
|
|
381
|
+
```ts
|
|
382
|
+
new Zip(options?: IZipOptions)
|
|
383
|
+
```
|
|
325
384
|
|
|
326
|
-
|
|
385
|
+
- `options`: Optional [IZipOptions](#izipoptions).
|
|
327
386
|
|
|
328
|
-
|
|
387
|
+
**Methods**
|
|
329
388
|
|
|
330
|
-
|
|
389
|
+
```ts
|
|
390
|
+
addFile(file: string, metadataPath?: string): void
|
|
391
|
+
addFolder(folder: string, metadataPath?: string): void
|
|
392
|
+
archive(): Promise<Buffer>
|
|
393
|
+
archive(zipFile: string): Promise<void>
|
|
394
|
+
cancel(): void
|
|
395
|
+
```
|
|
331
396
|
|
|
332
|
-
|
|
397
|
+
- `addFile`: Adds one file to the archive.
|
|
398
|
+
- `addFolder`: Adds one folder to the archive.
|
|
399
|
+
- `metadataPath`: Optional path stored inside the zip. A valid `metadataPath` must not start with `/` or `/[A-Za-z]:\//`, and must not contain `..`.
|
|
400
|
+
- `archive()`: Returns the zip as a `Buffer`.
|
|
401
|
+
- `archive(zipFile)`: Writes the zip directly to disk.
|
|
402
|
+
- `cancel()`: Cancels compression. Calling it after completion has no effect.
|
|
333
403
|
|
|
334
|
-
|
|
404
|
+
Use `metadataPath` to rename files or folders inside the archive. It is typically computed with `path.relative(root, realPath)`.
|
|
335
405
|
|
|
336
|
-
|
|
406
|
+
### Class: Unzip <a id="class-unzip"></a>
|
|
337
407
|
|
|
338
|
-
|
|
339
|
-
Extract the zip file.
|
|
408
|
+
Extract with entry filtering and cancellation support.
|
|
340
409
|
|
|
341
|
-
**Constructor
|
|
410
|
+
**Constructor**
|
|
342
411
|
|
|
343
|
-
|
|
412
|
+
```ts
|
|
413
|
+
new Unzip(options?: IExtractOptions)
|
|
414
|
+
```
|
|
344
415
|
|
|
345
|
-
|
|
416
|
+
- `options`: Optional [IExtractOptions](#iextractoptions).
|
|
346
417
|
|
|
347
|
-
|
|
418
|
+
**Methods**
|
|
348
419
|
|
|
349
|
-
|
|
350
|
-
|
|
420
|
+
```ts
|
|
421
|
+
extract(zipFile: string, targetFolder: string): Promise<void>
|
|
422
|
+
extract(zipBuffer: Buffer, targetFolder: string): Promise<void>
|
|
423
|
+
cancel(): void
|
|
424
|
+
```
|
|
351
425
|
|
|
352
|
-
|
|
426
|
+
- `extract(...)`: Extracts from a zip file path or a `Buffer`.
|
|
427
|
+
- `cancel()`: Cancels extraction. Calling it after completion has no effect.
|
|
353
428
|
|
|
354
|
-
|
|
429
|
+
### Options: IZipOptions <a id="izipoptions"></a>
|
|
355
430
|
|
|
356
|
-
|
|
431
|
+
```ts
|
|
432
|
+
interface IZipOptions {
|
|
433
|
+
followSymlinks?: boolean;
|
|
434
|
+
compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
435
|
+
}
|
|
436
|
+
```
|
|
357
437
|
|
|
358
|
-
|
|
438
|
+
- `followSymlinks`: Default `false`. If `true`, adds the target of a symbolic link. If `false`, adds the symbolic link itself.
|
|
439
|
+
- `compressionLevel`: Default `6`. Use `0` to store file data without compression, or `1`-`9` to deflate file data.
|
|
359
440
|
|
|
360
|
-
### Options:
|
|
441
|
+
### Options: IExtractOptions <a id="iextractoptions"></a>
|
|
361
442
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
443
|
+
```ts
|
|
444
|
+
interface IExtractOptions {
|
|
445
|
+
overwrite?: boolean;
|
|
446
|
+
safeSymlinksOnly?: boolean;
|
|
447
|
+
symlinkAsFileOnWindows?: boolean;
|
|
448
|
+
onEntry?: (event: IEntryEvent) => void;
|
|
449
|
+
}
|
|
450
|
+
```
|
|
365
451
|
|
|
366
|
-
|
|
452
|
+
- `overwrite`: Default `false`. If `true`, deletes the target directory before extraction.
|
|
453
|
+
- `safeSymlinksOnly`: Default `false`. If `true`, refuses to create symlinks whose targets are outside the extraction root.
|
|
454
|
+
- `symlinkAsFileOnWindows`: Default `true`. On Windows, if `true`, symbolic links are extracted as regular files. If `false`, the library tries to create real symbolic links instead. This may fail with `EPERM` when the current process is not allowed to create symlinks. Keep the default for better compatibility; set this to `false` only if you want to preserve symlinks as symlinks.
|
|
455
|
+
- `onEntry`: Called before an entry is extracted. Use it to inspect or skip entries.
|
|
367
456
|
|
|
368
|
-
|
|
369
|
-
- `overwrite?`: Boolean (optional) - If it is true, the target directory will be deleted before extract. The default value is `false`.
|
|
370
|
-
- `safeSymlinksOnly`: Boolean (optional) - Controls the creation phase of symlinks. The default value is `false`.<br>`true`: Refuses to create any symlink whose target is outside the extraction root.<br>`false`: Allows creating external symlinks. **Note:** Subsequent write operations to these links will still be intercepted by the separate AFWRITE security layer.
|
|
371
|
-
- `symlinkAsFileOnWindows?`: Boolean (optional) - Extract symbolic links as files on Windows. This value is only available on Windows and ignored on other platforms. The default value is `true`.<br>If `true`, the symlink in the zip will be extracted as a normal file on Windows.<br>If `false`, the symlink in the zip will be extracted as a symlink correctly on Windows, but an `EPERM` error will be thrown under non-administrators.
|
|
457
|
+
> WARNING: On Windows, creating symbolic links may require administrator privileges, depending on system policy. If Windows Developer Mode is enabled, non-administrator processes can usually create symlinks as well. If `symlinkAsFileOnWindows` is `false` and the current process is not allowed to create symlinks, extraction may fail with `EPERM`.
|
|
372
458
|
|
|
373
|
-
|
|
459
|
+
### Event: IEntryEvent
|
|
374
460
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
461
|
+
```ts
|
|
462
|
+
interface IEntryEvent {
|
|
463
|
+
readonly entryName: string;
|
|
464
|
+
readonly entryCount: number;
|
|
465
|
+
preventDefault(): void;
|
|
466
|
+
}
|
|
467
|
+
```
|
|
380
468
|
|
|
469
|
+
- `entryName`: The current entry name.
|
|
470
|
+
- `entryCount`: The total number of entries in the archive.
|
|
471
|
+
- `preventDefault()`: Skips the current entry.
|
|
381
472
|
# License
|
|
382
473
|
Licensed under the [MIT](https://github.com/fpsqdb/zip-lib/blob/master/LICENSE) license.
|
package/lib/fs.d.ts
CHANGED
|
@@ -20,5 +20,6 @@ export declare function readdirp(folder: string): Promise<FileEntry[]>;
|
|
|
20
20
|
export declare function getFileEntry(target: string): Promise<FileEntry>;
|
|
21
21
|
export declare function ensureFolder(folder: string): Promise<FolderStat>;
|
|
22
22
|
export declare function pathExists(target: string): Promise<boolean>;
|
|
23
|
+
export declare function statFolder(folder: string): Promise<FolderStat | undefined>;
|
|
23
24
|
export declare function rimraf(target: string): Promise<void>;
|
|
24
25
|
export declare function isRootPath(target: string): boolean;
|