What is @types/yauzl?
The @types/yauzl package provides TypeScript type definitions for the yauzl package, which is a library for unzipping files. These type definitions allow TypeScript developers to use yauzl in their projects with the benefits of TypeScript's static type checking. The main functionalities of yauzl, and thus @types/yauzl, revolve around reading and extracting zip files.
What are @types/yauzl's main functionalities?
Opening a zip file
This code demonstrates how to open a zip file using yauzl. It uses the `open` method, specifying the path to the zip file and options, then reads the first entry in the zip file.
import * as yauzl from 'yauzl';
yauzl.open('path/to/file.zip', {lazyEntries: true}, (err, zipfile) => {
if (err) throw err;
zipfile.readEntry();
});
Reading entries from a zip file
This code snippet is used within the context of an open zipfile object. It listens for 'entry' events, which occur each time a new entry in the zip file is ready to be read, logging the file name of each entry and then reading the next entry.
zipfile.on('entry', (entry) => {
console.log(`Entry: ${entry.fileName}`);
zipfile.readEntry();
});
Extracting file entries
This example shows how to extract files from a zip archive. It checks if the entry is a file (not a directory) and then creates a read stream for that entry. The stream is piped to a write stream that writes the file to a specified output path.
zipfile.on('entry', (entry) => {
if (/\/.test(entry.fileName)) {
zipfile.readEntry();
} else {
zipfile.openReadStream(entry, (err, readStream) => {
if (err) throw err;
readStream.on('end', () => {
zipfile.readEntry();
});
readStream.pipe(fs.createWriteStream('output/path/' + entry.fileName));
});
}
});
Other packages similar to @types/yauzl
adm-zip
adm-zip is a JavaScript implementation for zip data compression for NodeJS. It provides functionalities similar to yauzl, such as reading and extracting zip files, but also includes the ability to create zip files. Compared to yauzl, adm-zip offers a broader feature set but might not be as efficient for large zip files.
jszip
jszip is another popular library for working with zip files in JavaScript. It supports both Node.js and browser environments, making it more versatile than yauzl. jszip allows reading, creating, and modifying zip files. While it provides a more comprehensive API for handling zip files, it might be overkill for simple unzipping tasks.
unzipper
unzipper is a fork of the decompress-zip package and provides an easy way to extract zip files. It is designed to handle both small and large zip files efficiently. Compared to yauzl, unzipper offers a simpler API and automatic detection of the zip file encoding.