When extracting a folder, you can listen to extract event
zip.on('extract', (entry, file) => { console.log(`Extracted ${entry.name} to ${file}`);});
entry event is generated for every entry during loading
zip.on('entry', entry => { // you can already stream this entry, // without waiting until all entry descriptions are read (suitable for very large archives) console.log(`Read entry ${entry.name}`);});
Callback API
Open a zip file
const StreamZip = require('node-stream-zip');const zip = new StreamZip({ file: 'archive.zip' });// Handle errorszip.on('error', err => { /*...*/ });
List entries
zip.on('ready', () => { console.log('Entries read: ' + zip.entriesCount); for (const entry of Object.values(zip.entries())) { const desc = entry.isDirectory ? 'directory' : `${entry.size} bytes`; console.log(`Entry ${entry.name}: ${desc}`); } // Do not forget to close the file once you're done zip.close();});
zip.on('ready', () => { const data = zip.entryDataSync('path/inside/zip.txt'); zip.close();});
When extracting a folder, you can listen to extract event
zip.on('extract', (entry, file) => { console.log(`Extracted ${entry.name} to ${file}`);});
entry event is generated for every entry during loading
zip.on('entry', entry => { // you can already stream this entry, // without waiting until all entry descriptions are read (suitable for very large archives) console.log(`Read entry ${entry.name}`);});
Options
You can pass these options to the constructor
storeEntries: true - you will be able to work with entries inside zip archive, otherwise the only way to access them is entry event
skipEntryNameValidation: true - by default, entry name is checked for malicious characters, like ../ or c:\123, pass this flag to disable validation errors
nameEncoding: 'utf8' - encoding used to decode file names, UTF8 by default
Methods
zip.entries() - get all entries description
zip.entry(name) - get entry description by name
zip.stream(entry, function(err, stm) { }) - get entry data reader stream
zip.entryDataSync(entry) - get entry data in sync way
zip.close() - cleanup after all entries have been read, streamed, extracted, and you don't need the archive
Building
The project doesn't require building. To run unit tests with nodeunit: