node-stream-zip 1.12.0 → 1.13.3
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/LICENSE +1 -1
- package/README.md +97 -14
- package/node_stream_zip.d.ts +66 -35
- package/node_stream_zip.js +778 -636
- package/package.json +6 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# node-stream-zip
|
|
1
|
+
# node-stream-zip 
|
|
2
2
|
|
|
3
3
|
node.js library for reading and extraction of ZIP archives.
|
|
4
4
|
Features:
|
|
@@ -12,19 +12,100 @@ Features:
|
|
|
12
12
|
- deflate, sfx, macosx/windows built-in archives
|
|
13
13
|
- ZIP64 support
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
## Installation
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
```sh
|
|
18
|
+
npm i node-stream-zip
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
There are two APIs provided:
|
|
24
|
+
1. [promise-based / async](#async-api)
|
|
25
|
+
2. [callbacks](#callback-api)
|
|
26
|
+
|
|
27
|
+
It's recommended to use the new, promise API, however the legacy callback API
|
|
28
|
+
may be more flexible for certain operations.
|
|
18
29
|
|
|
19
|
-
|
|
30
|
+
### Async API
|
|
20
31
|
|
|
21
32
|
Open a zip file
|
|
22
33
|
```javascript
|
|
23
34
|
const StreamZip = require('node-stream-zip');
|
|
24
|
-
const zip = new StreamZip({
|
|
25
|
-
|
|
26
|
-
|
|
35
|
+
const zip = new StreamZip.async({ file: 'archive.zip' });
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Stream one entry to stdout
|
|
39
|
+
```javascript
|
|
40
|
+
const stm = await zip.stream('path/inside/zip.txt');
|
|
41
|
+
stm.pipe(process.stdout);
|
|
42
|
+
stm.on('end', () => zip.close());
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Read a file as buffer
|
|
46
|
+
```javascript
|
|
47
|
+
const data = await zip.entryData('path/inside/zip.txt');
|
|
48
|
+
await zip.close();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Extract one file to disk
|
|
52
|
+
```javascript
|
|
53
|
+
await zip.extract('path/inside/zip.txt', './extracted.txt');
|
|
54
|
+
await zip.close();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
List entries
|
|
58
|
+
```javascript
|
|
59
|
+
const entriesCount = await zip.entriesCount;
|
|
60
|
+
console.log(`Entries read: ${entriesCount}`);
|
|
61
|
+
|
|
62
|
+
const entries = await zip.entries();
|
|
63
|
+
for (const entry of Object.values(entries)) {
|
|
64
|
+
const desc = entry.isDirectory ? 'directory' : `${entry.size} bytes`;
|
|
65
|
+
console.log(`Entry ${entry.name}: ${desc}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Do not forget to close the file once you're done
|
|
69
|
+
await zip.close();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Extract a folder from archive to disk
|
|
73
|
+
```javascript
|
|
74
|
+
fs.mkdirSync('extracted');
|
|
75
|
+
await zip.extract('path/inside/zip/', './extracted');
|
|
76
|
+
await zip.close();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Extract everything
|
|
80
|
+
```javascript
|
|
81
|
+
fs.mkdirSync('extracted');
|
|
82
|
+
const count = await zip.extract(null, './extracted');
|
|
83
|
+
console.log(`Extracted ${count} entries`);
|
|
84
|
+
await zip.close();
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
When extracting a folder, you can listen to `extract` event
|
|
88
|
+
```javascript
|
|
89
|
+
zip.on('extract', (entry, file) => {
|
|
90
|
+
console.log(`Extracted ${entry.name} to ${file}`);
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`entry` event is generated for every entry during loading
|
|
95
|
+
```javascript
|
|
96
|
+
zip.on('entry', entry => {
|
|
97
|
+
// you can already stream this entry,
|
|
98
|
+
// without waiting until all entry descriptions are read (suitable for very large archives)
|
|
99
|
+
console.log(`Read entry ${entry.name}`);
|
|
27
100
|
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Callback API
|
|
104
|
+
|
|
105
|
+
Open a zip file
|
|
106
|
+
```javascript
|
|
107
|
+
const StreamZip = require('node-stream-zip');
|
|
108
|
+
const zip = new StreamZip({ file: 'archive.zip' });
|
|
28
109
|
|
|
29
110
|
// Handle errors
|
|
30
111
|
zip.on('error', err => { /*...*/ });
|
|
@@ -39,7 +120,7 @@ zip.on('ready', () => {
|
|
|
39
120
|
console.log(`Entry ${entry.name}: ${desc}`);
|
|
40
121
|
}
|
|
41
122
|
// Do not forget to close the file once you're done
|
|
42
|
-
zip.close()
|
|
123
|
+
zip.close();
|
|
43
124
|
});
|
|
44
125
|
```
|
|
45
126
|
|
|
@@ -109,13 +190,13 @@ zip.on('entry', entry => {
|
|
|
109
190
|
});
|
|
110
191
|
```
|
|
111
192
|
|
|
112
|
-
|
|
193
|
+
## Options
|
|
113
194
|
|
|
114
195
|
You can pass these options to the constructor
|
|
115
196
|
- `storeEntries: true` - you will be able to work with entries inside zip archive, otherwise the only way to access them is `entry` event
|
|
116
197
|
- `skipEntryNameValidation: true` - by default, entry name is checked for malicious characters, like `../` or `c:\123`, pass this flag to disable validation errors
|
|
117
198
|
|
|
118
|
-
|
|
199
|
+
## Methods
|
|
119
200
|
|
|
120
201
|
- `zip.entries()` - get all entries description
|
|
121
202
|
- `zip.entry(name)` - get entry description by name
|
|
@@ -123,16 +204,18 @@ You can pass these options to the constructor
|
|
|
123
204
|
- `zip.entryDataSync(entry)` - get entry data in sync way
|
|
124
205
|
- `zip.close()` - cleanup after all entries have been read, streamed, extracted, and you don't need the archive
|
|
125
206
|
|
|
126
|
-
|
|
207
|
+
## Building
|
|
127
208
|
|
|
128
209
|
The project doesn't require building. To run unit tests with [nodeunit](https://github.com/caolan/nodeunit):
|
|
129
|
-
|
|
210
|
+
```sh
|
|
211
|
+
npm test
|
|
212
|
+
```
|
|
130
213
|
|
|
131
|
-
|
|
214
|
+
## Known issues
|
|
132
215
|
|
|
133
216
|
- [utf8](https://github.com/rubyzip/rubyzip/wiki/Files-with-non-ascii-filenames) file names
|
|
134
217
|
- AES encrypted files
|
|
135
218
|
|
|
136
|
-
|
|
219
|
+
## Contributors
|
|
137
220
|
|
|
138
221
|
ZIP parsing code has been partially forked from [cthackers/adm-zip](https://github.com/cthackers/adm-zip) (MIT license).
|
package/node_stream_zip.d.ts
CHANGED
|
@@ -6,157 +6,188 @@ declare namespace StreamZip {
|
|
|
6
6
|
* File to read
|
|
7
7
|
* @default undefined
|
|
8
8
|
*/
|
|
9
|
-
file?: string
|
|
9
|
+
file?: string;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Alternatively, you can pass fd here
|
|
13
13
|
* @default undefined
|
|
14
14
|
*/
|
|
15
|
-
fd?: number
|
|
15
|
+
fd?: number;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* You will be able to work with entries inside zip archive,
|
|
19
19
|
* otherwise the only way to access them is entry event
|
|
20
20
|
* @default true
|
|
21
21
|
*/
|
|
22
|
-
storeEntries?: boolean
|
|
22
|
+
storeEntries?: boolean;
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* By default, entry name is checked for malicious characters, like ../ or c:\123,
|
|
26
26
|
* pass this flag to disable validation error
|
|
27
27
|
* @default false
|
|
28
28
|
*/
|
|
29
|
-
skipEntryNameValidation?: boolean
|
|
29
|
+
skipEntryNameValidation?: boolean;
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* Filesystem read chunk size
|
|
33
33
|
* @default automatic based on file size
|
|
34
34
|
*/
|
|
35
|
-
chunkSize?: number
|
|
35
|
+
chunkSize?: number;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
interface ZipEntry {
|
|
39
39
|
/**
|
|
40
40
|
* file name
|
|
41
41
|
*/
|
|
42
|
-
name: string
|
|
42
|
+
name: string;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* true if it's a directory entry
|
|
46
46
|
*/
|
|
47
|
-
isDirectory: boolean
|
|
47
|
+
isDirectory: boolean;
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
* true if it's a file entry, see also isDirectory
|
|
51
51
|
*/
|
|
52
|
-
isFile: boolean
|
|
52
|
+
isFile: boolean;
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* file comment
|
|
56
56
|
*/
|
|
57
|
-
comment: string
|
|
57
|
+
comment: string;
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
60
|
* if the file is encrypted
|
|
61
61
|
*/
|
|
62
|
-
encrypted: boolean
|
|
62
|
+
encrypted: boolean;
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
65
|
* version made by
|
|
66
66
|
*/
|
|
67
|
-
verMade: number
|
|
67
|
+
verMade: number;
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* version needed to extract
|
|
71
71
|
*/
|
|
72
|
-
version: number
|
|
72
|
+
version: number;
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
75
|
* encrypt, decrypt flags
|
|
76
76
|
*/
|
|
77
|
-
flags: number
|
|
77
|
+
flags: number;
|
|
78
78
|
|
|
79
79
|
/**
|
|
80
80
|
* compression method
|
|
81
81
|
*/
|
|
82
|
-
method: number
|
|
82
|
+
method: number;
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
85
|
* modification time
|
|
86
86
|
*/
|
|
87
|
-
time: number
|
|
87
|
+
time: number;
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
90
|
* uncompressed file crc-32 value
|
|
91
91
|
*/
|
|
92
|
-
crc: number
|
|
92
|
+
crc: number;
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
95
|
* compressed size
|
|
96
96
|
*/
|
|
97
|
-
compressedSize: number
|
|
97
|
+
compressedSize: number;
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* uncompressed size
|
|
101
101
|
*/
|
|
102
|
-
size: number
|
|
102
|
+
size: number;
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
105
|
* volume number start
|
|
106
106
|
*/
|
|
107
|
-
diskStart: number
|
|
107
|
+
diskStart: number;
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
110
|
* internal file attributes
|
|
111
111
|
*/
|
|
112
|
-
inattr: number
|
|
112
|
+
inattr: number;
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* external file attributes
|
|
116
116
|
*/
|
|
117
|
-
attr: number
|
|
117
|
+
attr: number;
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
120
|
* LOC header offset
|
|
121
121
|
*/
|
|
122
|
-
offset: number
|
|
122
|
+
offset: number;
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
type StreamZipOptions = StreamZip.StreamZipOptions;
|
|
127
127
|
type ZipEntry = StreamZip.ZipEntry;
|
|
128
128
|
|
|
129
|
+
declare class StreamZipAsync {
|
|
130
|
+
constructor(config: StreamZipOptions);
|
|
131
|
+
|
|
132
|
+
entriesCount: Promise<number>;
|
|
133
|
+
comment: Promise<string>;
|
|
134
|
+
|
|
135
|
+
entry(name: string): Promise<ZipEntry | undefined>;
|
|
136
|
+
entries(): Promise<{ [name: string]: ZipEntry }>;
|
|
137
|
+
entryData(entry: string | ZipEntry): Promise<Buffer>;
|
|
138
|
+
stream(entry: string | ZipEntry): Promise<NodeJS.ReadableStream>;
|
|
139
|
+
extract(entry: string | ZipEntry | null, outPath: string): Promise<number | undefined>;
|
|
140
|
+
|
|
141
|
+
on(event: 'entry', handler: (entry: ZipEntry) => void): void;
|
|
142
|
+
on(event: 'extract', handler: (entry: ZipEntry, outPath: string) => void): void;
|
|
143
|
+
|
|
144
|
+
close(): Promise<void>;
|
|
145
|
+
}
|
|
146
|
+
|
|
129
147
|
declare class StreamZip {
|
|
130
148
|
constructor(config: StreamZipOptions);
|
|
131
149
|
|
|
132
150
|
/**
|
|
133
151
|
* number of entries in the archive
|
|
134
152
|
*/
|
|
135
|
-
entriesCount: number
|
|
153
|
+
entriesCount: number;
|
|
136
154
|
|
|
137
155
|
/**
|
|
138
156
|
* archive comment
|
|
139
157
|
*/
|
|
140
|
-
comment: string
|
|
158
|
+
comment: string;
|
|
159
|
+
|
|
160
|
+
on(event: 'error', handler: (error: any) => void): void;
|
|
161
|
+
on(event: 'entry', handler: (entry: ZipEntry) => void): void;
|
|
162
|
+
on(event: 'ready', handler: () => void): void;
|
|
163
|
+
on(event: 'extract', handler: (entry: ZipEntry, outPath: string) => void): void;
|
|
141
164
|
|
|
142
|
-
|
|
143
|
-
on(event: 'entry', handler: (entry: ZipEntry) => void): void
|
|
144
|
-
on(event: 'ready', handler: () => void): void
|
|
145
|
-
on(event: 'extract', handler: (entry: ZipEntry, outPath: string) => void): void
|
|
165
|
+
entry(name: string): ZipEntry | undefined;
|
|
146
166
|
|
|
147
|
-
|
|
167
|
+
entries(): { [name: string]: ZipEntry };
|
|
148
168
|
|
|
149
|
-
|
|
169
|
+
stream(
|
|
170
|
+
entry: string | ZipEntry,
|
|
171
|
+
callback: (err: any | null, stream?: NodeJS.ReadableStream) => void
|
|
172
|
+
): void;
|
|
150
173
|
|
|
151
|
-
|
|
174
|
+
entryDataSync(entry: string | ZipEntry): Buffer;
|
|
152
175
|
|
|
153
|
-
|
|
176
|
+
openEntry(
|
|
177
|
+
entry: string | ZipEntry,
|
|
178
|
+
callback: (err: any | null, entry?: ZipEntry) => void,
|
|
179
|
+
sync: boolean
|
|
180
|
+
): void;
|
|
154
181
|
|
|
155
|
-
|
|
182
|
+
extract(
|
|
183
|
+
entry: string | ZipEntry | null,
|
|
184
|
+
outPath: string,
|
|
185
|
+
callback: (err?: any, res?: number) => void
|
|
186
|
+
): void;
|
|
156
187
|
|
|
157
|
-
|
|
188
|
+
close(callback?: (err?: any) => void): void;
|
|
158
189
|
|
|
159
|
-
|
|
190
|
+
static async: typeof StreamZipAsync;
|
|
160
191
|
}
|
|
161
192
|
|
|
162
193
|
export = StreamZip;
|