-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
example-callbacks.js
47 lines (40 loc) · 1.35 KB
/
example-callbacks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* eslint-disable no-console,no-unused-vars */
const StreamZip = require('./');
const zip = new StreamZip({ file: './test/ok/normal.zip' });
zip.on('error', (err) => {
console.error('ERROR: ' + err);
});
zip.on('ready', () => {
const entriesCount = zip.entriesCount;
console.log(`Done in ${process.uptime()}s. Entries read: ${entriesCount}`);
const entry = zip.entry('README.md');
console.log('Entry for README.md:', entry);
const data = zip.entryDataSync('README.md');
const firstLine = data.toString().split('\n')[0].trim();
console.log(`First line of README.md: "${firstLine}"`);
zip.close();
function streamDataToStdOut() {
zip.stream('README.md', (err, stm) => {
if (err) {
return console.error(err);
}
console.log('README.md contents streamed:\n');
stm.pipe(process.stdout);
});
}
function extractEntry() {
zip.extract('README.md', './tmp', (err) => {
console.log(err ? err : 'Entry extracted');
zip.close();
});
}
function extractAll() {
zip.extract(null, './tmp', (err, count) => {
console.log(err ? err : `Extracted ${count} entries`);
zip.close();
});
}
});
zip.on('extract', (entry, file) => {
console.log('extract', entry.name, file);
});